0

I have a button within the ItemTemplate of my Listview:

<asp:Button ID="EditButton" runat="server" Visible="false" Enabled="false" class="btn btn-sm btn-success" CommandName="Edit" Text="Edit" />

Then with my ItemDataBound event for my Listview I have:

protected void listview_ItemDataBound(object sender, ListViewItemEventArgs e)
{
   var btn_edit = e.Item.FindControl("EditButton") as Button;

   if(isOwner == false)
   {
     // code here..
   }
   else
   {
     btn_edit.Enabled = true;
     btn_edit.Visible = true;
   }

}

The edit button shows when I load the page so it shows that the btn_edit.Visible = true; is working, however when I click on it, it breaks and gives me this error:

Object reference not set to an instance of an object.

I know about about these errors however I don't understand why I am getting this error? Especially when it's reaching the Visible = true; statement?

Does anyone know what I may be doing wrong?

PS: The button used to work before I set the Enabled and Visible in the code and in the XML

UPDATE: I have narrowed the error down by debugging on the line, within the ItemDataBound event it shows that the btn_edit is not null and shows the Text and CommandName property values, however when I click on it, it falls through the same event again and this time shows the btn_edit is null. So when I click on it thats when the error is showing

c0mrade
  • 87
  • 9
  • 1
    I suspect `btn_edit` is `null`, so `FindControl` returns nothing. Put a breakpoint right after the `FindControl` line and inspect if it is indeed `null`. – Frank J Apr 27 '15 at 15:19
  • If the error occurs on the `Button_Click` event, then the error is in the code of that event. – Josh Part Apr 27 '15 at 15:19
  • @JoshPart How though? It was working before the button was set to enabled and visible – c0mrade Apr 27 '15 at 15:24
  • @JoshPart Also I don't have a click event as I am using commandName etc – c0mrade Apr 27 '15 at 15:26
  • @FrankJ I have put a breakpoint and the result is correct, it shows the CommandName and the Text property values – c0mrade Apr 27 '15 at 15:26
  • @FrankJ Okay, I have debugged when it loads and it finds the button and shows the correct values, however when I click on it, it is the opposite. Why is it failing when I click on it? Also to be specific, when I click on it, it is falling into the ItemDataBound since I am using CommandName and not using a click event – c0mrade Apr 27 '15 at 15:30
  • I don't know enough about how ASP.net resolves 'FindControl' requests. If it is similar to WPF and your button is a node outside of the tree structure `e.Item` can search for in, it would simply not find it. Maybe somebody with ASP.net experience can chime in here. – Frank J Apr 27 '15 at 15:34
  • @mason I have updated my question to prove it is different to the duplicated question – c0mrade Apr 27 '15 at 15:42

1 Answers1

0

You should always check which item type is being bound to avoid this kind of errors.
For instance (MSDN reference):

protected void listview_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
       var btn_edit = e.Item.FindControl("EditButton") as Button;

       if (isOwner == false)
       {
         // code here..
       }
       else
       {
         btn_edit.Enabled = true;
         btn_edit.Visible = true;
       }

    }
}
zed
  • 2,298
  • 4
  • 27
  • 44
  • I have added this and it still does not work – c0mrade Apr 27 '15 at 15:37
  • Can you provide more information on what you are trying to achieve with that button? To have a better idea of the whole picture. For instance, your button have set a commandname; maybe it is changing the active item template of ListView and that's why it is "not existing" anymore after postback; because another template (the edit item template of the list view) is being shown. – zed Apr 27 '15 at 16:14
  • I think what you have just said could be the problem! Do you have any idea how I would solve it, if that was the case? – c0mrade Apr 27 '15 at 16:20