Today i was come to an issue when i was deleting a record on basis of id from grid view and i used OnRowCommand event for this.
here is my gridview code :
<asp:GridView ID="gridShow" runat="server" AutoGenerateColumns="False" PageSize="5"
AllowPaging="true" ShowHeader="false" OnRowCommand="s_index" OnRowDeleting="gridShow_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtID" runat="server" Text='<%#Eval("ID") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%">
<ItemTemplate>
<asp:TextBox ID="txtDescription" runat="server" Text='<%#Eval("RollNumber") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtFname" runat="server" Text='<%#Eval("FirstName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%">
<ItemTemplate>
<asp:TextBox ID="txtLname" runat="server" Text='<%#Eval("LastName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%">
<ItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text='<%#Eval("Email") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"></asp:Label>
<asp:HiddenField ID="hdnStatus" runat="server" Value='<%#Eval("UserName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb" runat="server" Text="Delete" CommandName="delete" CommandArgument='<%#Eval("ID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is my C# code :
protected void s_index(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
oSRegisterBLL.BLLdelete(Convert.ToInt32(e.CommandArgument));
gview();
}
}
protected void gridShow_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
I successfully did this task by adding OnRowDeleting event on my grid view and definition of that event on my page behind but when i removed this first time i have come to known and issue " ASP.datashow_aspx' does not contain a definition for 'gridShow_RowDeleting' and no extension method 'gridShow_RowDeleting' accepting a first argument of type 'ASP.datashow_aspx' could be found (are you missing a using directive or an assembly reference?) "
I am confused with that why to add OnRowDeleting event on grid view with onrowcommand event ?
why i am confused because if i did not do any work with this event then why to use this event?
is there any way to work with only onrowcommand event ? or adding onrowdeleting event is essential for deletion of records from gridview?
I want to clear my mind for this ?
Your aspx markup of the GridView has declared the event handler here:
OnRowDeleting="gridShow_RowDeleting"
So when you try to remove it from the coedebehind you'll get that exception. So simply remove the event-handler and you can remove it from the codebehind.
Edit
Having a Delete-button, or even a regular button in a GridView with a CommandName of delete(which is the case here), will automatically try to fire OnRowDeleting.
So you have to add the event handler even if you don't use it or you have to rename the CommandName to e.g. "DeleteUser", otherwise you get exceptions like "The GridView 'gridShow' fired event RowDeleting which wasn't handled".
Related
Having an asp:GridView, when I put it into edit mode by setting EditIndex, I also need to update its data using the DataSource property and call DataBind like this:
protected void GrdFoo_OnRowEditing(object sender, GridViewEditEventArgs e)
{
grdFoo.EditIndex = e.NewEditIndex;
grdFoo.DataSource = DataBase.GetFoos();
grdFoo.DataBind();
}
Now, if the database contents was changed since I was populating the gridview first, how can I be sure I will edit the correct row?
edit
I got the suggestion to use OnRowDataBound. I am using a command field to trigger GrdFoo_OnRowEditing. Is the use of OnRowDataBound still a good idea?
Here is how I use the CommandField (aspx):
<asp:GridView ID="grdFoo" runat="server"
OnRowEditing="GrdFoo_OnRowEditing"
OnRowCancelingEdit="GrdFoo_OnRowCancelingEdit"
OnRowUpdating="GrdFoo_OnRowUpdating"
OnRowDeleting="GrdFoo_OnRowDeleting"
DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label Text='<%#Eval("Name") %>' runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" Text='<%#Eval("Name") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label Text='<%#Eval("Email") %>' runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmail" Text='<%#Eval("Email") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True"
ShowDeleteButton="True" buttontype="Image"
DeleteImageUrl="delete.png"
EditImageUrl="edit.png"
CancelImageUrl="cancel.png"
UpdateImageUrl="save.png"/>
</Columns>
</asp:GridView>
I have a GridView that is made up of two database fields populated via a stored procedure and then three fields for user input (two checkbox controls and one textbox) when I click on the save button I can get the information from the three controls but nothing from the two that were populated via the database. How can I get the first two fields?
<asp:GridView ID="gvA1" runat="server" AutoGenerateColumns="false" DataKeyNames="CodeNo" AutoGenerateSelectButton="false" EnablePersistedSelection="True" Visible="false">
<Columns>
<asp:TemplateField Visible="false" >
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "CodeNo")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Wrap="true" ItemStyle-Width="400px" HeaderText="Violation">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "CodeViolationCited") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="A1Accordion_cbPool" runat="server" Text="Pool:" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="A1Accordion_cbSpa" runat="server" Text="Spa:" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Additional Comments" Visible="true">
<ItemTemplate>
<asp:TextBox ID="A1Accordion_tb" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
protected void SaveAndCollapseA1(object sender, EventArgs e)
{
//good stuff
CheckBox myCheckBox_1 = gvA1.Rows[0].FindControl("A1Accordion_cbPool") as CheckBox;
CheckBox myCheckBox_2 = gvA1.Rows[0].FindControl("A1Accordion_cbSpa") as CheckBox;
TextBox myTextBox = gvA1.Rows[0].FindControl("A1Accordion_tb") as TextBox;
//not so good stuff
String myString1 = gvA1.Rows[0].Cells[0].ToString();
String myString2 = gvA1.Rows[0].Cells[1].ToString();
}
I figured it out but I haven't been hear long enough to post the solution via the links but if you change the columns as so:
<asp:label ID="lblA1CodeNo" runat="server" text='<%# Eval("CodeNo") %>'></asp:label>
then the values are available...
Try:
<%# Bind("CodeViolationCited") %>
Instead of
<%#DataBinder.Eval(Container.DataItem, "CodeViolationCited") %>
Eval is one way, bind is two-way.
See Microsoft's documentation for data-binding expressions
<asp:GridView ID="GridView1" runat="server"
style="height: 121px; width: 175px" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
onrowediting="GridView1_RowEditing" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ButtonType="Button" SelectText="Edit" ShowEditButton="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Id") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCpuName" runat="server" Text='<%# Eval("cpuname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("cpuname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# Eval("status") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("status") %>'>></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
GridViewRow row = GridView1.Rows[e.NewEditIndex];
TextBox txt = (TextBox)row.FindControl("TextBox2");
SqlDataSource1.UpdateParameters["CpuName"].DefaultValue = txt.Text;
SqlDataSource1.UpdateParameters["cpuid"].DefaultValue = "3";
SqlDataSource1.Update();
BindGridView();
}
I am getting a object reference not set to an object on the textbox txt. I am not able to get the textbox2 in my gridview.
Can you try this
GridViewRow row = GridView1.Rows[GridView1.NewEditIndex];
TextBox txt = row.Cells[1].Controls[1] as TextBox;
If (txt!= null)
{
SqlDataSource1.UpdateParameters["CpuName"].DefaultValue = txt.Text;
SqlDataSource1.UpdateParameters["cpuid"].DefaultValue = "3";
SqlDataSource1.Update();
BindGridView();
}
Why you just don't get directly the textbox value? is this a great problem?
...
GridViewRow row = GridView1.Rows[e.NewEditIndex];
SqlDataSource1.UpdateParameters["CpuName"].DefaultValue = row.Columns["CpuName"].Value;
...
I don't know that this works, because I'm a C#-er, and basically work on winform not on web apps
First, here is the snippet from MSDN, RowEditing topic:
The RowEditing event is raised when a
row's Edit button is clicked, but
before the GridView control enters
edit mode. This enables you to provide
an event-handling method that performs
a custom routine, such as canceling
the edit operation, whenever this
event occurs.
To resolve the problem, handle the RowUpdating event for this purpose. There is an example which should be helpful to you at:
GridView.RowEditing event
Also, I believe you should use the Bind word to bind your TextBox to a field:
'>
For more details, please refer to the Data-Binding Expressions Overview
I have a field in a details view shown below
<asp:BoundField DataField="DTMON_F" HeaderText="Monday Hours From: " InsertVisible="False"
ReadOnly="True" SortExpression="HOURS" Visible="false" />
<asp:TemplateField HeaderText="Monday Hours From: " SortExpression="HOURS">
<EditItemTemplate>
<uc1:TimePicker ID="tpMondayHours" runat="server"/>
</EditItemTemplate>
<InsertItemTemplate>
<%-- <uc1:TimePicker runat="server" ID="tpMondayHours" />--%>
<asp:TextBox ID="txtMondayHours" runat="server" Text='<%# Bind("DTMON_F") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblMondayHours" runat="server" Text='<%# Bind("DTMON_F") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Before "DTMON_F" is binded to the view I want to cut the string that is returned...Where and how might I do this?
You can implement the OnDataBinding event for each control instead of doing the bind inline. This will give you the ability to do whatever you like with the data before assigning it to the control.
Example using your Label. The same could be applied to the TextBox:
<asp:Label ID="lblMondayHours" runat="server"
OnDataBinding="lblMondayHours_DataBinding"></asp:Label>
protected void lblMondayHours_DataBinding(object sender, System.EventArgs e)
{
Label lbl = (Label)(sender);
string yourValue = (int)(Eval("DTMON_F"));
// *** Do whatever you want with the value now
lbl.Text = yourValue;
}
I have a grid control. And I want to access gridview child control from other controls.
Ex:
<asp:UpdatePanel ID="upPersonelAssignment" runat="server">
<ContentTemplate>
<asp:Label ID="lblPersonelName" runat="server" ></asp:Label>
<asp:Label ID="lblUpdatedDateTime" runat="server" ></asp:Label>
<div id="divPersonelAssignmentSearch">
<asp:GridView ID="gvPersonelAssignment" runat="server" DataSourceID="odsPersonelBLL"
AllowSorting="True" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:CommandField CancelText="Vazgeç" EditText="Amir Ata" ShowEditButton="True"
UpdateText="Amir Kaydet" />
<asp:TemplateField HeaderText="Sicil No">
<ItemTemplate>
<asp:Label ID="lblPersonelSicilNo" runat="server" Text='<%# Eval("personelSicil") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="İsim">
<ItemTemplate>
<asp:Label ID="lblPesonelFirstName" runat="server" Text='<%# Eval("personelAdi") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField> ...
...........
<asp:TemplateField HeaderText="Amir Liste">
<ItemTemplate>
<asp:Label ID="lblAmirList" runat="server" Text="Amir Liste"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
...........
<div id="divDropDownExtender" runat="server">
<act:DropDownExtender ID="ddeAmirList" runat="server"
TargetControlID="lblAmirList" DropDownControlID="">
</act:DropDownExtender>
</div>
But I have error : The TargetControlID of 'ddeAmirList' is not valid. A control with ID 'lblAmirList' could not be found
How to access grid view child control from other control?
using the ondatabound even of the gridview
you can access the control inside your gridview by writing the following:
protected void YourGridView_DataBound(object sender, EventArgs e)
{
Label label1 = (Label)e.Row.Cells[0].FindControl("lblAmirList");
// access your control
label1.Text = "sdfs";
// do this for other controls inside gridview
}
You have to put extenders within the GridView control for this to work, within the template of the control.