简体   繁体   中英

How to get the value of checkbox in gridview using C#?

I want to get the value of a CheckBox in a GridView . I'm able to bind the GridView using the event RowDataBound . But on a Click event of a Button the value of the CheckBox is always false even if I checked the CheckBox .

Code Behind:

    protected void GridView1_RowDatabound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList dddate = (DropDownList)e.Row.FindControl("DdlDate");
            DropDownList ddYear = (DropDownList)e.Row.FindControl("DdlYear");
            for (int i = System.DateTime.Now.Year; i > (System.DateTime.Now.Year) - 100; i--)
                ddYear.Items.Add(Convert.ToString(i));
            for (int i = 1; i < 32; i++)
                dddate.Items.Add(Convert.ToString(i)); 
        }
    }
    protected void btnRetrieveCheck_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("chkSel");
            if (cb != null && cb.Checked)
            {

                DropDownList dddate = (DropDownList)row.FindControl("DdlDate");              //Bind data to the GridView control.
                DropDownList ddYear = (DropDownList)row.FindControl("DdlYear");
                DropDownList ddmonth = (DropDownList)row.FindControl("DdlMonth");

            }
        }
    }

You should hook up to the Checked event on the checkbox instead of the click event.

Your question is not very clear. Can you post some more of the code. For example, what is btnRetrieveCheck - is it the actual checkbox? Please update the question with your XAML so we can see what binding is in place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM