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