简体   繁体   中英

Unable to find checkbox by ID in a GridView based on HiddenField value

I have a GridView in ASP.NET, inside a column on this GridView I have the below controls:

<asp:TemplateField>       
    <ItemTemplate>
        <input id='<%#Eval("po_asn_number") %>' class="css-checkbox" type="checkbox" />                                                       
        <label for='<%#Eval("po_asn_number") %>' name="lbl_1" class="css-label"></label>        

        <asp:HiddenField ID="poid" runat="server" Value='<%#Eval("po_asn_number") %>' />
    </ItemTemplate>                     
</asp:TemplateField>

This is my OnClick event in the Code Behind.

protected void create_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvr in GridView1.Rows)
    {
        HiddenField poid = ((HiddenField)gvr.Cells[0].FindControl("poid"));

        if (((HtmlInputCheckBox)gvr.FindControl(poid.Value)).Checked == true)
        {
            Response.Redirect("ShipmentDetail.aspx?id=" + poid.Value);
        }
        else
        {
            //Do nothing
        }
    }
}

What I'm trying to do here first, I look for a HiddenField which the value is the ID for the <input type="checkbox" /> . I am then checking to see if the checkbox is checked. If it is then do something else do nothing.

When click the button I get an error:

Object reference not set to an instance of an object

Line 48:             if (((HtmlInputCheckBox)gvr.FindControl(checkbox)).Checked == true)
Line 49:             {
Line 50:                 Response.Redirect("ShipmentDetail.aspx?id=" + poid.Value);

Any help you can provide will appreciated.

Add runat attribute.

<input id='<%#Eval("po_asn_number") %>' class="css-checkbox" type="checkbox" runat="server"/>

Without this attribute, you cannot find the control in the code behind on the server side code.

Also put a break point where you get the Hidden field value to confirm that you are getting the expected value.

You also need to implement the change Karl suggested to make it work.

New addition: Change this line to add Cells[0] for the line below:

if (((HtmlInputCheckBox)gvr.Cells[0].FindControl(poid.Value)).Checked == true)

You need to only look in data rows when you are looping through all of the grid view rows, because when you do not specify only data rows, it starts with the header row. You are getting the exception, because it cannot cast the result of FindControl() to a type. Since there is no control in the header row with this name, FindControl() returns null and the cast blows up.

Instead do this:

protected void create_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvr in GridView1.Rows)
    {
        // Only deal with data rows, not header or footer rows, etc.
        if (gvr.RowType == DataControlRowType.DataRow)
        {
            HiddenField poid = ((HiddenField)gvr.FindControl("poid"));

            // Check if hidden field was found or not
            if(poid != null)
            {
                if (((HtmlInputCheckBox)gvr.FindControl(poid.Value)).Checked)
                {
                    Response.Redirect("ShipmentDetail.aspx?id=" + poid.Value);
                }
                else
                {
                    //Do nothing
                }
            }
        }
    }
}

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