Hamamelis Hamamelis - 3 months ago 54
ASP.NET (C#) Question

Enable Disable Checkbox in Asp.net Gridview Based on DropDownList in C#

Enable Disable Checkbox in Asp.net Gridview Based on DropDownlist in C#

I have a problem with my C# code.

I need enable or disable checkboxes in asp.net gridview based on DropDownList.

If in the DropDownList no have selected any values the checkboxes in GridView are disabled.

When in the DropDownList have selected a value the checkboxes in GridView are enabled for the variable selected in the DropDownList.

In the GridView the output is correct when in DropDownList have selected a value, but Checkboxes are always enabled.

I've tried using these solution without success.

Here is my code:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkSelect = (CheckBox)e.Row.FindControl("chkSelect");

if (DDL.SelectedIndex != 0)
{
chkSelect.Enabled = true;
}
}
}
catch (Exception)
{
}
}


edit #1

<asp:DropDownList ID="DDL" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDL_SelectedIndexChanged"
CssClass="ddl_Class" Enabled="false">
<asp:ListItem Text="-------" Value=""></asp:ListItem>
</asp:DropDownList>

<asp:TemplateField>
<ItemTemplate>
<center>
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged"
AutoPostBack="true" Enabled="false" /></center>
</ItemTemplate>
</asp:TemplateField>


protected void DDL_SelectedIndexChanged(object sender, EventArgs e)
{

for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkSelect = ((CheckBox)GridView1.Rows[i].FindControl("chkSelect"));

if (DDL.SelectedIndex != 0)
{
chkSelect.Enabled = true;
}
}

GridViewBind();
}

Answer

you set check box Enable OR Disable On

    protected void DDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkSelect = ((CheckBox)GridView1.Rows[i].FindControl("chkSelect"));

            if (DDL.SelectedIndex != 0)
            {
                chkSelect.Enabled = true;
            }
        }
    }
Comments