c# - ASP.NET I don't get a value from textbox field on GridView RowUpdating event -


so problem don't get value textbox field on gridview rowupdating event.

here html asp.net code:

<asp:gridview     id="gridview3"     datakeynames="anperiodid,acworkerid"     visible ="true"     runat="server"     autogeneratecolumns="false"     showheaderwhenempty="true"      onrowupdating="gridview3_rowupdating"       onrowediting="gridview3_rowediting"     onrowcancelingedit="gridview3_rowcancelingedit"              cssclass="display table table-striped table-bordered">     <columns>         <asp:boundfield datafield="acperiod" headertext="obdobje" itemstyle-width="5%" readonly="true" itemstyle-wrap="false" />         <asp:boundfield datafield="acworker" headertext="zaposleni" itemstyle-width="5%" readonly="true" itemstyle-wrap="false" />         <asp:boundfield datafield="acreason" headertext="vzrok za +/- 15 ur" itemstyle-width="85%" controlstyle-width="100%"/>         <asp:templatefield showheader="false" itemstyle-width="5%" itemstyle-wrap="false">             <itemtemplate>                 <asp:linkbutton id="button_edit" runat="server" causesvalidation="false" commandname="edit" cssclass="btn btn-xs btn-warning myconfirm" tooltip="uredi"><i class="ace-icon fa fa-pencil bigger-120"></i></asp:linkbutton>                 <%-- <asp:linkbutton id="button_delete" runat="server" causesvalidation="false" commandname="delete" cssclass="btn btn-xs btn-danger" tooltip="izbriši"><i class="ace-icon fa fa-trash bigger-120"></i></asp:linkbutton> --%>             </itemtemplate>             <edititemtemplate>                 <asp:linkbutton id="buttonupdate" runat="server" commandname="update" text="update" cssclass="btn btn-xs btn-success" tooltip="potrdi"><i class="ace-icon fa fa-check bigger-120"></i></asp:linkbutton>                 <asp:linkbutton id="buttoncancel" runat="server" commandname="cancel" text="cancel" cssclass="btn btn-xs btn-danger" tooltip="prekliči"><i class="ace-icon fa fa-reply bigger-120"></i></asp:linkbutton>             </edititemtemplate>         </asp:templatefield>     </columns> </asp:gridview> <asp:sqldatasource id="sqldatasource3" runat="server" selectcommand="_upjn_getperioddata" selectcommandtype="storedprocedure">     <selectparameters>         <asp:sessionparameter name="acuserid" sessionfield="userid" type="string" />         <asp:controlparameter controlid="checkbox1" name="all" propertyname="checked" />         <asp:controlparameter controlid="checkbox2" name="curr" propertyname="checked" />         <asp:parameter name="type" dbtype="int16" defaultvalue="2" />      </selectparameters> </asp:sqldatasource> 

and here code behind:

protected void grid_reload(int i) {     db db = new db();      if (i == 0 || == 1)     {         sqldatasource1.connectionstring = db.getconnstring();         gridview1.databind();     }     if (i == 0 || == 2)     {         sqldatasource2.connectionstring = db.getconnstring();         gridview2.databind();     }     if (i == 0 || == 3)     {         sqldatasource3.connectionstring = db.getconnstring();         gridview3.datasource = sqldatasource3;         gridview3.databind();     }     }  protected void gridview3_rowupdating(object sender, gridviewupdateeventargs e) {     string warning_msg = "", error_msg = "", info_msg = "", success_msg = "";     int = 0;     commons cm = new commons();     try     {         gridviewrow row = (gridviewrow)gridview3.rows[e.rowindex];         int periodid = convert.toint32(gridview3.datakeys[e.rowindex].values[0]);         string workerid = gridview3.datakeys[e.rowindex].values[1].tostring();          textbox field1 = (textbox)row.cells[2].controls[0];          string sql = "update _utjn_evidencadcsum set acreason = '" + field1.text + "' anperiodid = '" + periodid.tostring() + "' , acworkerid = '" + workerid + "'";          db db = new db();         db.execsql(sql);          gridview3.editindex = -1;         grid_reload(3);     }     catch (exception ex)     {         error_msg = ex.message;     }         {         = cm.setmessage(labelerror, error, error_msg, labelwarning, warning, warning_msg, labelinfo, info, info_msg, labelsuccess, success, success_msg);     }      if (i == 1)     {         clientscript.registerstartupscript(this.gettype(), "hash", "location.hash = '#msg';", true);     } }  protected void gridview3_rowediting(object sender, gridviewediteventargs e) {     gridview3.editindex = e.neweditindex;     grid_reload(3); }  protected void gridview3_rowcancelingedit(object sender, gridviewcancelediteventargs e) {     gridview1.editindex = -1;     grid_reload(3); } 

the problem assume here:

textbox field1 = (textbox)row.cells[2].controls[0]; 

but cannot figure out. have identical example, it's working perfectly.

other method (that 1 working fine):

protected void gridview1_rowupdating(object sender, gridviewupdateeventargs e) {     string warning_msg = "", error_msg = "", info_msg = "", success_msg = "";     int = 0;     commons cm = new commons();     try     {         gridviewrow row = (gridviewrow)gridview1.rows[e.rowindex];         int id = convert.toint32(gridview1.datakeys[e.rowindex].values[0]);          textbox field1 = (textbox)row.cells[1].controls[0];          string sql = "update _utjn_holidays set actext = '" + field1.text + "' anid = '" + id.tostring()+ "'";          db db = new db();         db.execsql(sql);          gridview1.editindex = -1;         grid_reload();     }     catch (exception ex)     {         error_msg = ex.message;     }         {         = cm.setmessage(labelerror, error, error_msg, labelwarning, warning, warning_msg, labelinfo, info, info_msg, labelsuccess, success, success_msg);     }      if (i == 1)     {         clientscript.registerstartupscript(this.gettype(), "hash", "location.hash = '#msg';", true);     } } 

i tried this:

//loop through cells in row. int index = 0; foreach (tablecell cell in row.cells) {     if (cell.controls.count > 0)     {         //check whether cell has textbox.         if (cell.controls[0] textbox)         {             //reference textbox.             textbox textbox = cell.controls[0] textbox;             textbox.id = "txtreason";             //warning_msg += "xx" + gridview3.headerrow.cells[index].text + "xx" + index.tostring();         }     }     index++; }  //textbox field1 = (textbox)row.cells[2].controls[0]; textbox field1 = (textbox)row.findcontrol("txtreason");  string sql = "update _utjn_evidencadcsum set acreason = '" + field1.text + "' anperiodid = '" + periodid.tostring() + "' , acworkerid = '" + workerid + "'"; 

and still empty..

you way put textbox textname = (textbox)row.findcontrol("your textbox id"); , use value want code behind


Comments