c# - Update textbox for each row in gridview on One Button Click -
in gridview have given textbox each row has corresponding button fetches value textbox , updates rows value in database.
but want add 1 button on top in header template fetch value of textboxes , update rows.
i have tried following code null reference exception @ "instance_id cell[0]"
my code below.
`
<asp:templatefield> <headertemplate> <asp:button id="button1" runat="server" text="button" onclick="button1_click" /> </headertemplate> <itemtemplate> <asp:textbox id="textbox1" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> <asp:buttonfield text="update attendance" commandname="select" /> </columns> </asp:gridview>`
and on button click fetch data textboxes:
protected void button1_click(object sender, eventargs e) { foreach (gridviewrow row in gridview1.rows) { vo.instance_id = gridview1.selectedrow.cells[0].text; vo.project_id = gridview1.selectedrow.cells[1].text; vo.volunteer_id = gridview1.selectedrow.cells[6].text; vo.campus_id = gridview1.selectedrow.cells[5].text; vo.attendance_hours = (gridview1.selectedrow.findcontrol("textbox1") textbox).text; convert.toint32(vo.attendance_hours); try { int success = bl.fn_updateattendance(vo); if (success == 2) { scriptmanager.registerclientscriptblock(this, this.gettype(), "alertmessage", "alert('attendance updated successfully')", true); // label2.text = "attendance updated successfully"; } else { scriptmanager.registerclientscriptblock(this, this.gettype(), "alertmessage", "alert('attendance not updated')", true); // label2.text = "attendance not updated"; } } catch (exception ex) { response.write(ex.message); } } }
my friend suggested using row.cells[0]; inside foreach , worked!
thank everyone.
Comments
Post a Comment