Javascript Enable/Disable Dropdown in Gridview using Checkboxes
If you come up with some requirement in which you have a Checkbox and a DropDownList in GridView control and you are requried to enable disable the DropDownList on the basis of the CheckBox value you can use following Code.
-----------------------------------.aspx file ----------------------------------
<script type="text/javascript">
function StatusGridDropdown(drpdown)
{
document.getElementById(drpdown).disabled = !document.getElementById(drpdown).disabled;
}
</script>
.
.
.
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Width="400px">
<Columns>
<asp:TemplateField>
<AlternatingItemTemplate>
<asp:CheckBox ID="cbStatus" runat="server" />
</AlternatingItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbStatus" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<AlternatingItemTemplate>
<asp:DropDownList ID="ddl" runat="server" Enabled="False" Width="169px">
</asp:DropDownList>
</AlternatingItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddl" runat="server" Width="169px" Enabled="False">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
----------------------------------.vb file if VB.Net is the language ------------------------------------
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
DirectCast(e.Row.FindControl("cbStatus"), CheckBox).Attributes.Add("onclick", "javascript:StatusGridDropdown('" & DirectCast(e.Row.FindControl("ddl"), DropDownList).ClientID & "')")
End If
End Sub
----------------------------------.cs file if C#.Net is the language ------------------------------------
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.DataRow)) {
((CheckBox)e.Row.FindControl("cbStatus")).Attributes.Add("onclick", "javascript:StatusGridDropdown('" + ((DropDownList)e.Row.FindControl("ddl")).ClientID + "')");
}
}