asp.net - Enabling /Disabling ButtonField of GridView using Checkbox -
i have grid view
- one column itemtemplate column has checkbox field.
- other 2 columns databound columns. 1 column buttonfield of button type.
i want button set disabled mode
once checkbox checked should enabling particular row button field. in this?
my sample try
.aspx file
<asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:email_notificationconnection %>" selectcommand="select [customer_name] [customer]"></asp:sqldatasource> <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" datasourceid="sqldatasource1" enablemodelvalidation="true"> <columns> <asp:boundfield datafield="customer_name" headertext="customer_name" sortexpression="customer_name" /> <asp:templatefield> <itemtemplate> <asp:checkbox runat="server" id="non_prod_all_select" oncheckedchanged="checkbox2_checkedchanged1" /> </itemtemplate> <headerstyle width="30px" /></asp:templatefield> <asp:buttonfield buttontype="button" commandname="edit" text="button" /> </columns> </asp:gridview>
.aspx.cs file
protected void checkbox2_checkedchanged1(object sender, eventargs e) { checkbox chk = (checkbox)sender; gridviewrow gridrow = ((gridviewrow)(chk.parent)); if (chk.checked) { button btn = (button)(gridrow.findcontrol("button")); btn.enabled = true; } else { button btn = (button)(gridrow.findcontrol("button")); btn.enabled = false; } }
try using below code:
aspx code gridview1:
<asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:email_notificationconnection %>" selectcommand="select [customer_name] [customer]"></asp:sqldatasource> <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" datasourceid="sqldatasource1" enablemodelvalidation="true"> <columns> <asp:boundfield datafield="customer_name" headertext="customer_name" sortexpression="customer_name" /> <asp:templatefield> <itemtemplate> <asp:checkbox runat="server" autopostback="true" id="non_prod_all_select" oncheckedchanged="checkbox2_checkedchanged1" /> </itemtemplate> <headerstyle width="30px" /></asp:templatefield> <asp:templatefield> <itemtemplate> <asp:button id="button1" runat="server" text="button" enabled="false" /> </itemtemplate> </asp:templatefield> </columns> </asp:gridview>
code behind (for checkbox check changed event handler):
protected void checkbox2_checkedchanged1(object sender, eventargs e) { foreach (gridviewrow row in gridview3.rows) { ((button)row.findcontrol("button1")).enabled = ((checkbox)row.findcontrol("non_prod_all_select")).checked; } }
changes made:
1.set autopostback checkbox true.
2.removed button field , added template field button in third column of grid (so asp:button control read in code behind)
3.changed code behind code necessary.
note: have checked code locally , working expected. replace old code , let me know in case of issues.
Comments
Post a Comment