Asked by:
gridview command filed button not firing inside update panel

Question
-
User181930479 posted
im trying to fire an event on button click inside gridview , im using updatepanel , but nothing is fired . below is my code .
aspx:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <div> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always"> <ContentTemplate> <div> <asp:Label ID="theid" runat="server" Visible="false"></asp:Label> <asp:Label ID="txpath" runat ="server" Visible="false"></asp:Label> <asp:GridView CssClass="myGridClass" ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit" OnRowDeleting="OnRowDeleting" OnRowDataBound = "OnRowDataBound" OnSelectedIndexChanged = "OnSelectedIndexChanged" OnRowCommand="GridView1_RowCommand" > <Columns> <asp:TemplateField HeaderText="Choose"> <ItemTemplate> <asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="GetSelectedRecords" /> </ItemTemplate> <ItemStyle Width="20px"></ItemStyle> </asp:TemplateField> <asp:TemplateField HeaderText="File Name" ItemStyle-Width="50"> <ItemTemplate> <asp:Label ID="Number" runat="server" Text='<%# Eval("Name") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="Number" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox> </EditItemTemplate> <ItemStyle Width="250px"></ItemStyle> </asp:TemplateField> <asp:TemplateField HeaderText="Page number" ItemStyle-Width="100"> <ItemTemplate> <asp:Label ID="IssueDate" runat="server" Text='<%# Eval("PagesNumber") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="IssueDate" runat="server" Text='<%# Eval("PagesNumber") %>'></asp:TextBox> </EditItemTemplate> <ItemStyle Width="100px"></ItemStyle> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:Button ID="Button1" runat="server" Text="Button" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
</ContentTemplate>
</asp:UpdatePanel><asp:Label ID="lbl111" runat="server"></asp:Label> </div>
aspx.cs:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); lbl111.Text = GridView1.Rows[index].Cells[1].Text; }
Any help please ?
Wednesday, June 20, 2018 5:25 AM
All replies
-
User632428103 posted
Hello naf,
i've copy paste your code and delete some piece of code and now i have well the click on button who is redirect to the code behind with the command argument ..
delete perhpas the onRowCommand into the design and code behind and create again ...
here is it my code :
design
<form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:UpdatePanel ID="upn" runat="server" UpdateMode="Always"> <ContentTemplate> <asp:GridView CssClass="myGridClass" ID="GridView1" OnRowCommand="GridView1_RowCommand" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnRowDataBound= "GridView1_RowDataBound"> <Columns> <asp:TemplateField HeaderText="Choose"> <ItemTemplate> <asp:TextBox ID="txtNumber" runat="server"></asp:TextBox> </ItemTemplate> <ItemStyle Width="20px"></ItemStyle> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:Button ID="Button1" runat="server" Text="Button click" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </ContentTemplate> </asp:UpdatePanel> </form>
code behind
public partial class gvRowCommand : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bindGrid(); } } void bindGrid() { var person = Person.GetPersons(); GridView1.DataSource = person; GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { Person p = (Person)e.Row.DataItem; // if (e.Row.RowType == DataControlRowType.DataRow) { TextBox txt = (TextBox)e.Row.FindControl("txtNumber"); txt.Text = p.Name; } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { //debugger stop well }
Wednesday, June 20, 2018 9:07 AM -
User-1716253493 posted
Try register the button
Wednesday, June 20, 2018 12:06 PM -
User-330142929 posted
Hi Naf,
According to your description, I copied your code and found that the event has already fired. However, the effect of the event execution is not, that is, the label control is not been assigned. The reason why it doesn't work is page doesn't refresh.
I suggest you could put the label into another updatepanel. and the updatepanelmode property is always by default. So the label could be assigned correctly. Also you could put he label inside the first updatepanel
Here is my code, wish it is useful to you.
Aspx.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <div> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:TemplateField HeaderText="Id"> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Eval("Id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Button"> <ItemTemplate> <asp:Button ID="Button1" runat="server" Text="Button" CommandArgument="<%# ((GridViewRow)Container).RowIndex %> " /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <%--<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>--%> </ContentTemplate> </asp:UpdatePanel> </div> <asp:UpdatePanel ID="UpdatePanel2" runat="server"> <ContentTemplate> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> </ContentTemplate> </asp:UpdatePanel>
Code behind.
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlConnection connection = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DataStore;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"); SqlCommand command = new SqlCommand(); command.CommandText = "select * from products"; command.CommandType = System.Data.CommandType.Text; command.Connection = connection; SqlDataAdapter sda = new SqlDataAdapter(command); DataTable dt = new DataTable(); sda.Fill(dt); this.GridView1.DataSource = dt; this.GridView1.DataBind(); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { GridViewRow drv = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent)); Label labelName = (Label)drv.FindControl("Label1"); Label2.Text = labelName.Text; }
Please feel free to let me know, if you have any question.
Best Regards,
Abraham
Thursday, June 21, 2018 10:08 AM