2

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();
}
1
  • try to use dropdown_SelectedIndexChanged event.First find the check button inside that event and then apply your condition. Commented Aug 22, 2014 at 7:16

2 Answers 2

2

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;
            }
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help. In edit #1 in my first post new version of my code, the checkboxes now are all disabled.
@Hamamelis not bind grid second time GridViewBind() remove
0

Making the string lowercase should help?

 Eval("Reason").ToString().ToLower().Equals("hybrid")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.