0

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.

jorame
  • 2,147
  • 12
  • 41
  • 58
  • Split that cluttered line up and tell us which object is causing the error please. – P.Brian.Mackey Aug 28 '13 at 18:46
  • Not sure what line are you refering to but the control giving me issues is `` I'm unable to find this control based on the value of the `HiddenField` – jorame Aug 28 '13 at 18:55

2 Answers2

1

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)
Gloria
  • 1,053
  • 8
  • 18
  • Not sure what you mean by break point. I added the `runat="server"` and I'm getting a different error. – jorame Aug 28 '13 at 18:59
  • By breakpoint I mean debug the code to see if you are getting a value at this line `HiddenField poid = ((HiddenField)gvr.Cells[0].FindControl("poid"));` – Gloria Aug 28 '13 at 19:00
  • New Error: `The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: ` Yes, I'm getting the `poid.value` I checked this already. – jorame Aug 28 '13 at 19:02
  • This error happens on `PageLoad` not when I fire the OnClick event. – jorame Aug 28 '13 at 19:07
  • The way you are assigning the ID is apparently not allowed as I read from here: http://stackoverflow.com/questions/1859206/eval-script-for-server-side-controls-id-property – Gloria Aug 28 '13 at 19:08
  • Is there any particular reaon why you want the `id` of the `checkbox` to be the value of your `eval`function. Why not assign it a static simple `id`? If you need the value of the `eval` for any further manipulations you can always access the `hiddenfield` value of each row. – Gloria Aug 28 '13 at 19:13
  • I need this for my CSS class. I'm looking for the `id` to style the `checkbox` – jorame Aug 28 '13 at 19:15
  • Is your styling indivdual to each checkbox on each row? I mean do you need each id to be unique or do all checkboxes for that gridiew have the same styling? If styling is same for all checkboxes of `gvr gridview`, assign the checkbox whatever simple id you would like, eg: `id="simpleid"` and then use it in ur css as `#simpleid{//your css}` – Gloria Aug 28 '13 at 19:18
0

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
                }
            }
        }
    }
}
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • I did your recommended change and I'm still getting the same error. – jorame Aug 28 '13 at 18:53
  • I am guessing it is not finding your hidden field then, I updated the code to put a guard clause against the `poid` hidden field variable. You do not need to go through the `Cells` collection to find the hidden field control either, just use the `FindControl()` method on the row. See updated answer. – Karl Anderson Aug 28 '13 at 19:07
  • Any particular reason you are using an HTML input instead of an ASP.NET server control? – Karl Anderson Aug 28 '13 at 19:10
  • My CSS styling does not work on the ASP.NET control unfortunately – jorame Aug 28 '13 at 19:13