c# - ASP.NET DataGrid is empty -


i have asp.net datagrid on user control. have main page adds user control ( multiple copies of user control ) , restores them when post occurs.

the datagrid has insert / edit / delete links. can add multiple copies of user control page , insert / edit delete functionality works each separate control.

yesterday added property binding main page unrelated user control using format text='<%# documenttitle %>'. couldn't work until added page.databind(); main page's page_load method. @ point property binding started working correctly noticed insert functionality had stopped working in datagrid within each user control....i debugged , found when following line executes finds text fields in controls within datagrid empty or "":

einfo.ref = ((textbox)gveg.footerrow.findcontrol("txtemployeename")).text; 

if remove page.databind() method main page property binding stops working datagrid(s) in usercontrol start working.

my question 2 fold i) why seemingly unrelated change effect datagrid , ii) do working?

i've added relevant sections of code below...or @ least think relevant sections.

default.aspx

    <div class="general">             <asp:textbox width="488" runat="server" placeholder="document title"  text='<%# documenttitle %>'></asp:textbox>     </div> 

default.aspx.cs

protected void page_load(object sender, eventargs e)     {                    if (!ispostback)         {             // create empty user control first requirements section.             employeesectionusercontrol myusercontrol1 = (employeesectionusercontrol )loadcontrol("~/employeesectionusercontrol .ascx");               // add panel control holding user controls.             mainpanel.controls.add(myusercontrol1);              documenttitle = "i default document title , i'm bound.";         }         else         {             // nothing         }                     page.databind();     } 

employeesectionusercontrol.ascx

<asp:gridview id="gveg" runat="server" autogeneratecolumns="false" cssclass="grid" alternatingrowstyle-cssclass="gridaltrow" rowstyle-cssclass="gridrow" showfooter="true" editrowstyle-cssclass="grideditrow" footerstyle-cssclass="gridfooterrow" onrowcancelingedit="gveg_rowcancelingedit" onrowcommand="gveg_rowcommand" onrowdatabound="gveg_rowdatabound" onrowdeleting="gveg_rowdeleting" onrowediting="gveg_rowediting" onrowupdating="gveg_rowupdating" datakeynames="id" showheaderwhenempty="true"> <columns>     <asp:templatefield headertext="id" headerstyle-horizontalalign="left" controlstyle-width="50px">         <itemtemplate>             <%# eval("id")%>         </itemtemplate>     </asp:templatefield>      <asp:templatefield headertext="ref" headerstyle-horizontalalign="left" controlstyle-width="90px">         <edititemtemplate>             <asp:textbox id="txtemployeename" runat="server" text='<%# bind("ref") %>'                 width="90px"></asp:textbox>                         </edititemtemplate>         <footertemplate>             <asp:textbox id="txtemployeename" runat="server" width="90px"></asp:textbox>                         </footertemplate> 

employeesectionusercontrol.ascx.cs

protected void gveg_rowcommand(object sender, gridviewcommandeventargs e)     {         if (e.commandname.equals("insert"))         {             employeeinfo einfo = new employeeinfo();             einfo.id = 999;// convert.toint32(((textbox)gveg.footerrow.findcontrol("id")).text);              // if we're inserting emptydatatemplate( ie empty table ) of gridview need retreive data differently.             // perform check on gridview footerrow , if it's null can assume it's empty table.             if (gveg.footerrow == null)             {                 textbox referencetxtbox = (((control)e.commandsource).namingcontainer).findcontrol("txtemployeename") textbox;                 einfo.ref = referencetxtbox.text;                                 }             else             {                                     einfo.ref = ((textbox)gveg.footerrow.findcontrol("txtemployeename")).text;                 einfo.need =              }              // store update , re-bind data grid.         }                } 

page.databind() calls databind on it's children, updates documenttitle in text box databinds grid. didn't see datasource set in grid, entitydatasource, assuming doing smart retrieving (and preparation) of data in code , set datasource yourself:

gveg.datasource = somecollection; gveg.databind(); 

on loading user-control , call databind specifying datasource. binds , call page.databind() triggers databind gveg since datasource set shows same.

on post shouldn't databind() before handling events. call of page.databind() that. triggers databind() without datasource. rowcommand comes , checks textbox in footer cleared due databind no elements.

what should is: shouldn't use page.databind(). if so, need todo @ moment when datasources set correctly , shouldn't kicked of during post back. in general, not recommend using because makes more complex , it's helping bit if haven't set application correctly. in case it's not necessary. textbox server control that's not part of binding (gridview, repeater, listview). textbox available in code behind. should:

  1. give textbox id can use txtdocumenttitle

     <asp:textbox width="488" id="txtdocumenttitle" runat="server" placeholder="document title"></asp:textbox> 
  2. replace setting documenttitle (unless need else too) with:

     txtdocumenttitle.text = "i default document title , i'm bound."; 
  3. remove page.databind();

so access server controls have access since properties in page or control.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -