Reputation: 1689
I have a GridView with a CheckBox and a DropDownList as template field columns. I am trying to enable the row's DropDownList when the CheckBox of that row is checked.
This is my aspx:
<asp:GridView runat="server" ID="GdvCPRetailerMap" AutoGenerateColumns="False" Font-Size="Small" CssClass="grid" BackColor="White" BorderWidth="0px" CellPadding="4" Width="100%" AllowSorting="True" SkinID="GVSalesManager" GridLines="none" AllowPaging="true" PageSize="10" PagerStyle-ForeColor="#0066cc" PagerStyle-CssClass="gvPagerCss" PagerStyle-Font-Underline="true" PagerStyle-Wrap="true" OnPageIndexChanging="GdvCPRetailerMap_PageIndexChanging" OnRowDataBound="GdvCPRetailerMap_RowDataBound">
<Columns>
<asp:BoundField ReadOnly="true" HeaderText="S.No" DataField="S.No." SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="2%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="2%"/>
</asp:BoundField>
<asp:TemplateField ItemStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="chkRow_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Channel Partner-1" HeaderStyle-Font-Bold="true" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="ddlCP1" Enabled="false" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCP1_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Channel Partner-2" HeaderStyle-Font-Bold="true" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="ddlCP2" Enabled="false" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCP2_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have tried doing this in my CheckBox OnCheckedChanged event:
protected void chkRow_CheckedChanged(object sender, EventArgs e)
{
CheckBox ck1 = (CheckBox)sender;
GridViewRow grow =(GridViewRow)ck1.NamingContainer;
DropDownList ddlR = (DropDownList)grow.Cells[6].FindControl("ddlCP1");
if (ck1.Checked == true)
ddlR.Enabled = true;
else
ddlR.Enabled = false;
}
But nothing seems to happen with this code. The breakpoint doesn't even get hit.
Can some one guide me in the right direction on how to enable a DropDownList of a GridViewRow whose CheckBox is checked?And also if there is any jquery or javascript code to achieve the same output?
Upvotes: 1
Views: 919
Reputation: 3949
Looks like all you may need to do is make sure the CheckBox causes a PostBack. You can do this by setting the property AutoPostBack
to true.
<asp:CheckBox ID="chkRow" runat="server"
OnCheckedChanged="chkRow_CheckedChanged"
AutoPostBack="true" />
As the documentation for AutoPostBack states:
Gets or sets a value indicating whether the CheckBox state automatically posts back to the server when clicked.
Also:
Property Value
Type: System.Boolean
true to automatically post the state of the CheckBox control to the server when it is clicked; otherwise, false. The default is false.
Afterwards, there is no need to do the FindControl call on a specific cell. You can just simply call that on the GridViewRow itself.
protected void chkRow_CheckedChanged(object sender, EventArgs e)
{
CheckBox ck1 = (CheckBox)sender;
GridViewRow grow =(GridViewRow)ck1.NamingContainer;
DropDownList ddlR = (DropDownList)grow.FindControl("ddlCP1");
if (ck1.Checked == true)
ddlR.Enabled = true;
else
ddlR.Enabled = false;
}
Upvotes: 1