locked
NullReferenceException on FindControl() RRS feed

  • Question

  • Something I have been struggling with here for a little while now. I have a Datalist and in that data list I have 2 buttons, 1 in the header of the list and 1 in the footer of the list.

    <asp:datalist id="list" runat="server" Width="100%" cellpadding="0" cellspacing="1">
      <HeaderTemplate>
        <tr>
          <td align="right" colspan="3">
    	<asp:Button id="btnCart2" cssclass="style4" runat="server" commandargument="checkout" text="Add to Cart"></asp:Button>
          </td>
        </tr>
      </HeaderTemplate>
      <FooterTemplate>
        <tr>
          <td align="right" colspan="3">
            <asp:Button id="btnCart1" cssclass="style4" runat="server" commandargument="checkout" tabindex="30" text="Add to Cart"></asp:Button>
          </td>
        </tr>
      </FooterTemplate>
      <ItemTemplate>

    When I attempt to use a findcontrol with these buttons I get a nullreferenceexception

    Here is the code I have for using the find control:

     foreach (DataListItem dli in list.Items)
    {
     ((Button)dli.FindControl("btnCart2")).Enabled = isQtyValid;
     ((Button)dli.FindControl("btnCart1")).Enabled = isQtyValid;
    }

    I have used this method of doing it on a similar page, the only difference being that the button wasn't in the headertemplate or footertemplate, but in the ItemTemplate. So from my viewpoint that is what must be causing this exception. And I ask for any advice you may have to get around this little bump

    One thing I have tried is the following:

    Object Button1 = list.FindControl("btnCart1");
    //Which did not cause the exception, but once I cast it to a button:
    Object Button1 = ((Button)list.FindControl("btnCart1"));
    //It gave me the same error

    Any advice or help anyone is able to give me will be greatly appreciated. Thank you for your time.

    Monday, April 29, 2013 1:33 PM

Answers

  • Use the ItemDataBound event to find the buttons. Here is an example on how to find the header button:

    asp:datalist id="list" runat="server" Width="100%" cellpadding="0" cellspacing="1"
                onitemdatabound="list_ItemDataBound">
    ....

     

    protected void list_ItemDataBound(object sender, DataListItemEventArgs e) {
          if (e.Item.ItemType == ListItemType.Header) {
            Button btnCart2 = e.Item.FindControl("btnCart2") as Button;
            if (btnCart2 != null) {
              btnCart2.Enabled = isQtyValid;
            }
          }
        }

    • Proposed as answer by Younes BENMOUSSA Monday, April 29, 2013 1:50 PM
    • Marked as answer by pbf98 Tuesday, April 30, 2013 6:28 PM
    Monday, April 29, 2013 1:49 PM

All replies

  •  foreach (DataListItem dli in list.Items)
    {
    var ctrl = dli.FindControl("btnCart2");
    if(ctrl != null){
        ctrl.Enable = isQtyValid;
        ctrl = null;
    }
    ctrl = dli.FindControl("btnCart1");
    if(ctrl != null){
        ctrl.Enable = isQtyValid;
    }
    }


    Faisal Ahmed Farooqui

    Monday, April 29, 2013 1:38 PM
  • Use the ItemDataBound event to find the buttons. Here is an example on how to find the header button:

    asp:datalist id="list" runat="server" Width="100%" cellpadding="0" cellspacing="1"
                onitemdatabound="list_ItemDataBound">
    ....

     

    protected void list_ItemDataBound(object sender, DataListItemEventArgs e) {
          if (e.Item.ItemType == ListItemType.Header) {
            Button btnCart2 = e.Item.FindControl("btnCart2") as Button;
            if (btnCart2 != null) {
              btnCart2.Enabled = isQtyValid;
            }
          }
        }

    • Proposed as answer by Younes BENMOUSSA Monday, April 29, 2013 1:50 PM
    • Marked as answer by pbf98 Tuesday, April 30, 2013 6:28 PM
    Monday, April 29, 2013 1:49 PM
  • Doing this just showed me the possibility of why I have been getting the NullReferenceException, it doesn't actually fire this event. I am trying to figure out the reason right now, but so far coming up with nothing, thanks for the advice.
    Monday, April 29, 2013 2:55 PM
  • Hi pbf98,

    You are more likely to get more efficient responses to ASP.NET issues at http://forums.asp.net where you can contact ASP.NET experts.

     

    Bob Shen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 30, 2013 5:12 AM
  • Doing this just showed me the possibility of why I have been getting the NullReferenceException, it doesn't actually fire this event. I am trying to figure out the reason right now, but so far coming up with nothing, thanks for the advice.

    Did you add onitemdatabound="list_ItemDataBound" to your markup? Set a breakpoint in  list_ItemDataBound to find out if it is fired or not. It should work.
    Tuesday, April 30, 2013 9:32 AM
  • After discussion with someone I found my problem with the way you suggested. I was able to get the event to fire, though I am not done with my problem you did have the solution to the nullreferenceexception. 

    And to be clear it was the fact that I needed to check for the item type which in this case was the ListItemHeader and the ListItemFooter.

    Again thank you for your help!


    • Edited by pbf98 Tuesday, April 30, 2013 6:32 PM
    Tuesday, April 30, 2013 6:31 PM