c# - Problems with if (!IsPostBack) in gridview -


i have gridview , columns editable text fields , column button in template field command name(ed).

i have issue postbacks because when type next text textboxes need able click save button , save new data gridview , in order need if (!ispostback) in page load around populategridview stop overwriting changed data.

this work fine when clicking button not hit command name , button disappears instead of doing anything. if remove if (!ispostback) button works fine can't newly entered text data.

here of code:

protected void page_load(object sender, eventargs e) {     getuserinfo();     constpageid = convert.toint16(request.querystring["pageid"]);     if (!ispostback)     {         populategridview();     } }   private void populategridview() {     try     {         using (sqlconnection conn = new sqlconnection(getconnection.getconnectionstring()))         {             sqlcommand sqlcomm = new sqlcommand("pl_usercolumns_get", conn);             sqlcomm.parameters.addwithvalue("@pageid", constpageid);             sqlcomm.parameters.addwithvalue("@userid", struserid);              sqlcomm.commandtype = commandtype.storedprocedure;              sqldataadapter da = new sqldataadapter();             da.selectcommand = sqlcomm;              da.fill(ds);             if (ds.tables[0].rows.count != 0)             {                 gvorder.datasource = ds.tables[0];                 gvorder.databind();             }         }     }     catch (sqlexception ex)     {         exceptionhandling.sqlexception(ex, constpageid, constissitespecific);     } }   protected void gvorder_rowcommand(object sender, gridviewcommandeventargs e) {     if (e.commandname == "ed")     {         gridviewrow gvr = (gridviewrow)((control)e.commandsource).namingcontainer;         int rowindex = gvr.rowindex;         label id = gvr.findcontrol("lblid") label;         string id = id.text;          try         {             sqlconnection dbconnection = new sqlconnection();             dbconnection.connectionstring = getconnection.getconnectionstring();             sqlcommand dbcommand = new sqlcommand("pl_usercolumns_ed", dbconnection);             dbcommand.commandtype = commandtype.storedprocedure;             dbcommand.parameters.add("@pageid", sqldbtype.int).value = convert.toint16(constpageid);             dbcommand.parameters.add("@userid", sqldbtype.int).value = convert.toint16(struserid);             dbcommand.parameters.add("@columnid", sqldbtype.int).value = convert.toint16(id);             dbconnection.open();             dbcommand.executenonquery();             dbconnection.close();         }         catch (sqlexception ex)         {             exceptionhandling.sqlexception(ex, constpageid, constissitespecific);         }          response.redirect("modifycolumns.aspx?&pageid=" + constpageid);      } } } 

row databound:

protected void gvorder_rowdatabound(object sender, gridviewroweventargs e)         {             datatable dt = ds.tables[0];             dropdownlist ddl = new dropdownlist();             textbox txt = new textbox();             int index = 1;             int indexenabled = 1;              if (e.row.rowtype == datacontrolrowtype.datarow)             {                 ddl = e.row.findcontrol("ddlnewo") dropdownlist;                 txt = e.row.findcontrol("txtnewt") textbox;             }             if (e.row.rowindex == 0)             {                 ddl.items.add("1");                 ddl.enabled = false;                 txt.enabled = false;              }             else if (e.row.rowindex != 0)             {                 ddl.items.remove("1");                 //create ed button                  if (e.row.rowtype == datacontrolrowtype.datarow)                 {                     button btned = new button();                     btned.id = "btned";                     btned.cssclass = "buttonsmall";                     btned.commandname = "ed";                     btned.enableviewstate = true;                      datarow r = dt.rows[e.row.rowindex];                     if (r.itemarray[3].tostring() == "1")                     {                         btned.text = "disable";                         e.row.cssclass = "rowenabled";                         foreach (datarow r2 in dt.rows)                         {                             if (r2.itemarray[3].tostring() == "1")                             {                                  string listitem = convert.tostring(indexenabled+1);                                 ddl.items.add(listitem);                                 indexenabled++;                                                            }                          }                         int itemtoremove = ddl.items.count+1;                         ddl.items.remove(itemtoremove.tostring());                          ddl.selectedindex = idxselected;                         idxselected++;                     }                     else if (r.itemarray[3].tostring() == "0")                     {                         btned.text = "enable";                         e.row.cssclass = "rowdisabled";                         ddl.enabled = false;                         txt.enabled = false;                          foreach (datarow r1 in dt.rows)                         {                             string listitem = convert.tostring(index);                             ddl.items.add(listitem);                             index++;                         }                         ddl.selectedindex = e.row.rowindex;                     }                      //add button grid                     e.row.cells[6].controls.add(btned);                 }             }         }         protected void btned_click(object sender, eventargs e)         {             // coding click event         }         protected void getuserinfo()         {             try             {                 if (userinfo == null)                 {                     //sorry...no cookie!                 }                 else                 {                     if (!string.isnullorempty(userinfo.values["userid"]))                     {                         struserid = userinfo.values["userid"];                     }                 }             }             catch (exception ex)             {                 exceptionhandling.netexception(ex, constpageid, constissitespecific);             }         } 

aspx:

<body>     <form id="form1" runat="server">     <div>          <asp:gridview id="gvorder" runat="server" width="100%" cssclass="tblbrowse" autogeneratecolumns="false" onrowdatabound="gvorder_rowdatabound" onrowcommand="gvorder_rowcommand">             <columns>                 <asp:boundfield datafield="currento" headertext="curr. order" />                 <asp:templatefield headertext="new order">                     <itemtemplate>                         <asp:dropdownlist id="ddlnewo" runat="server" width="99%"></asp:dropdownlist>                     </itemtemplate>                 </asp:templatefield>                 <asp:boundfield datafield="currentt" headertext="curr. text" />                 <asp:templatefield headertext="new text">                     <itemtemplate>                     <asp:textbox runat="server" width="98%" id="txtnewt" text='<%# bind("currentt") %>'></asp:textbox>                     </itemtemplate>                 </asp:templatefield>                 <asp:templatefield visible="false">                     <itemtemplate>                         <asp:label id="lblid" runat="server" text='<%# bind("id") %>'></asp:label>                     </itemtemplate>                 </asp:templatefield>                                 <asp:templatefield visible="false">                     <itemtemplate>                         <asp:label id="lbled" runat="server" text='<%# bind("enabled") %>'></asp:label>                     </itemtemplate>                 </asp:templatefield>                 <asp:templatefield headertext="enable/disable"></asp:templatefield>             </columns>         </asp:gridview>      </div>         <asp:button id="btnsave" runat="server" cssclass="smallbutton" onclick="btnsave_click" text="save" width="100px" onclientclick="close(); return true;" />         <br />         <asp:label id="lblmessage" runat="server" forecolor="red" visible="false"></asp:label>     </form> </body> 

the issue here trying add button dynamically every time have construct column separately. since using gvorder_rowdatabound(object sender, gridviewroweventargs e) method not called in post not doing gvorder databinding in post back, buttons missed added under rowdatabound event.

try below code , check if working fine:

page_load method:

protected void page_load(object sender, eventargs e) {     getuserinfo();     constpageid = convert.toint16(request.querystring["pageid"]);     if (!ispostback)     {         populategridview();     }     contructcolumn(); } 

row data bound method:

protected void gvorder_rowdatabound(object sender, gridviewroweventargs e)         {             contructcolumn();         } 

construct column method (newly added method):

private void contructcolumn()         {             datatable dt = ds.tables[0];             dropdownlist ddl = new dropdownlist();             textbox txt = new textbox();             int index = 1;             int indexenabled = 1;             foreach (gridviewrow row in gvorder.rows)             {                 if (row.rowtype == datacontrolrowtype.datarow)                 {                     ddl = row.findcontrol("ddlnewo") dropdownlist;                     txt = row.findcontrol("txtnewt") textbox;                 }                 if (row.rowindex == 0)                 {                     ddl.items.add("1");                     ddl.enabled = false;                     txt.enabled = false;                  }                 else if (row.rowindex != 0)                 {                     ddl.items.remove("1");                     //create ed button                      if (row.rowtype == datacontrolrowtype.datarow)                     {                         button btned = new button();                         btned.id = "btned"+row.rowindex;                         btned.cssclass = "buttonsmall";                         btned.commandname = "ed";                         btned.enableviewstate = true;                          datarow r = dt.rows[row.rowindex];                         if (r.itemarray[3].tostring() == "1")                         {                             btned.text = "disable";                             row.cssclass = "rowenabled";                             foreach (datarow r2 in dt.rows)                             {                                 if (r2.itemarray[3].tostring() == "1")                                 {                                      string listitem = convert.tostring(indexenabled + 1);                                     ddl.items.add(listitem);                                     indexenabled++;                                 }                              }                             int itemtoremove = ddl.items.count + 1;                             ddl.items.remove(itemtoremove.tostring());                              ddl.selectedindex = idxselected;                             idxselected++;                         }                         else if (r.itemarray[3].tostring() == "0")                         {                             btned.text = "enable";                             row.cssclass = "rowdisabled";                             ddl.enabled = false;                             txt.enabled = false;                              foreach (datarow r1 in dt.rows)                             {                                 string listitem = convert.tostring(index);                                 ddl.items.add(listitem);                                 index++;                             }                             ddl.selectedindex = row.rowindex;                         }                          //add button grid                         row.cells[6].controls.add(btned);                     }                 }             }         } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -