Click here to Skip to main content
15,940,921 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
HI,
I m trying to enable/disable a drowpdown in gridview row on a checkbox checked event in the same grdview. I am using the following html in aspx page as

and

<asp:TemplateField HeaderText="DNA" HeaderStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:CheckBox ID="imgbDNA" runat="server" OnCheckedChanged="chkDNAGridview_OnCheckedChanged" />
                           </ItemTemplate>

</asp:TemplateField>

and aspx.cs code is
protected void chkDNAGridview_OnCheckedChanged(object sender, EventArgs e)
       {
           CheckBox ck1 = (CheckBox)sender;
           GridViewRow grow = (GridViewRow)ck1.NamingContainer;


           foreach(GridViewRow rowitem in gvAttendance.Rows)
           {
               DropDownList ddlR = (DropDownList)rowitem.Cells[3].FindControl("ddlReason");

               if (ck1.Checked == true)
                   ddlR.Enabled = true;
               else
                   ddlR.Enabled = false;

           }



       }


The problem with that code is it disable/enable dropdown in the gridview in the first row. But when i check/uncheck second row it disable or enable drowpdown in all the rows. I want to enable/disable dropdown in the specific row only on check/uncheck of checkbox.
Thanks in advance
Posted

1 solution

But when i check/uncheck second row it disable or enable drowpdown in all the rows
Your code is doing that via foreach loop you wrote!

Remove foreach from the OnCheckChanged method. Instead use, something like:
C#
CheckBox ck1 = (CheckBox)sender;
GridViewRow grow = (GridViewRow)ck1.NamingContainer;
DropDownList ddlR = (DropDownList)grow.Cells[3].FindControl("ddlReason");

if (ck1.Checked == true)
    ddlR.Enabled = true;
else
    ddlR.Enabled = false;
 
Share this answer
 
Comments
touseef4pk 9-Mar-11 4:09am    
Thanks sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900