Answered by:
Error while editing gridview

Question
-
User314352500 posted
hi
I have a gridview with edit and delete.
When I click Edit in the row of gridview, I get Object Reference not set to an instance of object in this line of code-behind:
Object reference not set to an instance of an object. imgbtnEdit.Enabled = true;
Can anyone please help me with this?
Thank you in advance.
My code below
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { ImageButton imgbtnEdit = (ImageButton)e.Row.FindControl("imgbtnEdit"); Label testing = (Label)e.Row.FindControl("testing"); if (!string.IsNullOrEmpty(testing.Text.ToString())) { imgbtnEdit.Enabled = true; } } } <asp:TemplateField> <ItemTemplate> <center> <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="/Images/icon.gif" Enabled="false" /> </center> </ItemTemplate> <EditItemTemplate> <center> <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="/Images/update.gif" /> <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="/Images/delete.gif" /> </EditItemTemplate> </asp:TemplateField>
Can anyone please help me with this?
Thank you in advance.
My code below
Friday, August 21, 2020 11:55 AM
Answers
-
User-943250815 posted
You are using template so try e.Row.Cells[n] where n is column number of gridview
ImageButton imgbtnEdit = (ImageButton)e.Row.Cells[0].FindControl("imgbtnEdit")
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 21, 2020 12:31 PM -
User-939850651 posted
Hi Uncle Vince,
Error while editing gridview [duplicate]
Based on your description, I think you may have encountered the same problem as in this case.
Have you tried the solutions provided in this case?
If I misunderstood what you mean, could you provide more details?
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, August 24, 2020 7:02 AM
All replies
-
User-943250815 posted
You are using template so try e.Row.Cells[n] where n is column number of gridview
ImageButton imgbtnEdit = (ImageButton)e.Row.Cells[0].FindControl("imgbtnEdit")
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 21, 2020 12:31 PM -
User-939850651 posted
Hi Uncle Vince,
Error while editing gridview [duplicate]
Based on your description, I think you may have encountered the same problem as in this case.
Have you tried the solutions provided in this case?
If I misunderstood what you mean, could you provide more details?
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, August 24, 2020 7:02 AM