c# - Error while editing gridview -


this question has answer here:

i have gridview edit , delete.

when click edit in row of gridview, object reference not set instance of object in line of code-behind:

object reference not set instance of object. imgbtnedit.enabled = true; 

can please me this? thank in advance.

my aspx gridview is:

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> 

<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> 

your image button imgbtnedit in itemtemplate. when click edit, gridview1_rowediting called , row in gridview1 goes 'edit mode'. when happens in edititemtemplate rendered, imgbtnedit not exist - why getting null reference exception.

edit: tend have variable on page called isineditmode , set false when page loads. when rowediting called write like:

protected void gridview1_rowediting(object sender, gridviewediteventargs e) { isineditmode = true; gridview1.editindex = e.neweditindex; //rebind gridview1 } 

and in rowdatabound ...

protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { if (isineditmode == true)     {     //get references controls in edititemtemplates     }     else     {     //get references controls in itemtemplates     } } 

that way won't trying reference controls don't exist - depending on whether viewing or editing gridview.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -