asp.net - How to set checkbox always checked when user redirect to another page? -
how implement checkbox stay check if user redirects page? example, in code behind when check checkbox system updates database , says "validated" , when press gobackteacher_click event redirect page. in page there button function there redirect current page code behind function implemented , checkbox checked.
aspx code:
<div class="container" style="text-align: center; margin: 0 auto;"> <br /> <h1>validation of subjects</h1> <br /> <asp:gridview runat="server" cssclass="table table-hover table-bordered" id="validatesubject" style="text-align: center"></asp:gridview> </div> <div style="float: right; padding-right: 75px;"> <button type="button" runat="server" class="btn btn-success" onserverclick="gobackteacher_click">go back</button> </div>
code behind:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data.sqlclient; using system.data; namespace softwareanalysisanddesign.sad { public partial class validatesubjectteacher : system.web.ui.page { checkbox check = new checkbox(); protected void page_load(object sender, eventargs e) { if (session["validatesubject"] == null) { response.redirect("teacherpage.aspx", true); } if (!ispostback) { validatesubject.datasource = session["validatesubject"]; validatesubject.databind(); } //add checkbox in last row of gridview progmatically foreach (gridviewrow row in validatesubject.rows) { check = row.cells[row.cells.count - 1].controls[0] checkbox; //position check column on last row in gridview check.enabled = true; check.checkedchanged += validatesubject_click; //bind event on button check.autopostback = true; //set autopostback property true } } protected void validatesubject_click(object sender, eventargs e) { checkbox chk = (checkbox)sender; gridviewrow grvrow = (gridviewrow)chk.namingcontainer;//this give row string validated = "validated"; string notyetvalidated = "not yet validated"; string studid = grvrow.cells[0].text; string coursenum = grvrow.cells[1].text; if (chk.checked) { grvrow.cells[10].text = validated; //open connection using (sqlconnection conn = new sqlconnection("data source=keith;initial catalog=sad;integrated security=true")) { //open connection database try { conn.open(); } catch (exception e) { console.writeline(e.tostring()); } using (sqlcommand cmd = new sqlcommand("update assessmentform set status = @validated studentid = @studentid , courseno = @coursenumber" ,conn)) { cmd.parameters.addwithvalue("@validated", validated); cmd.parameters.addwithvalue("@studentid", studid); cmd.parameters.addwithvalue("@coursenumber", coursenum); cmd.executenonquery(); } //close connection database try { conn.close(); } catch (exception e) { console.writeline(e.tostring()); } } } else { grvrow.cells[10].text = notyetvalidated; //open connection using (sqlconnection conn = new sqlconnection("data source=keith;initial catalog=sad;integrated security=true")) { //open connection database try { conn.open(); } catch (exception e) { console.writeline(e.tostring()); } //query database update status using (sqlcommand cmd = new sqlcommand("update assessmentform set status = @validated studentid = @studentid , courseno = @coursenumber", conn)) { cmd.parameters.addwithvalue("@validated", notyetvalidated); cmd.parameters.addwithvalue("@studentid", studid); cmd.parameters.addwithvalue("@coursenumber", coursenum); cmd.executenonquery(); } //close connection database try { conn.close(); } catch (exception e) { console.writeline(e.tostring()); } } } } protected void gobackteacher_click(object sender, eventargs e) { response.redirect("teacherpage.aspx"); } } }
to further understand question, here image further explain it.
this when check checkbox without pressing go button
and go pressed go button , in page there proceed button redirect current page gridview located.
there checkbox unchecked, , status says validated. how implement code checkbox stay checked?
is postback? please help.
update
i've tried this, stay checked redirecting page, when checkbox clicked status not change "validated" "not yet validated" not change on postback when clicked.
foreach (gridviewrow row in validatesubject.rows) { bool ischecked = default(bool); if (row.cells[row.cells.count - 2].text.equals("validated")) // here please assign position of **status** column. { ischecked = true; check = row.cells[row.cells.count - 1].controls[0] checkbox; //position check column on last row in gridview check.enabled = true; check.checkedchanged += validatesubject_click; //bind event on button check.autopostback = true; //set autopostback property true check.checked = ischecked; //set checkbox checked based on status ; } else if (row.cells[row.cells.count - 2].text.equals("not yet validated")) { ischecked = false; check = row.cells[row.cells.count - 1].controls[0] checkbox; //position check column on last row in gridview check.enabled = true; check.checkedchanged += validatesubject_click; //bind event on button check.autopostback = true; //set autopostback property true check.checked = ischecked; //set checkbox checked based on status ; } }
in function, condition above not changed database, instead refresh page when checkbox clicked.
protected void validatesubject_click(object sender, eventargs e) { checkbox chk = (checkbox)sender; gridviewrow grvrow = (gridviewrow)chk.namingcontainer;//this give row string validated = "validated"; string notyetvalidated = "not yet validated"; string studid = grvrow.cells[0].text; string coursenum = grvrow.cells[1].text; if (chk.checked) { grvrow.cells[10].text = validated; //open connection using (sqlconnection conn = new sqlconnection("data source=keith;initial catalog=sad;integrated security=true")) { //open connection database try { conn.open(); } catch (exception e) { console.writeline(e.tostring()); } using (sqlcommand cmd = new sqlcommand("update assessmentform set status = @validated studentid = @studentid , courseno = @coursenumber" ,conn)) { cmd.parameters.addwithvalue("@validated", validated); cmd.parameters.addwithvalue("@studentid", studid); cmd.parameters.addwithvalue("@coursenumber", coursenum); cmd.executenonquery(); } //close connection database try { conn.close(); } catch (exception e) { console.writeline(e.tostring()); } } } else { grvrow.cells[10].text = notyetvalidated; //open connection using (sqlconnection conn = new sqlconnection("data source=keith;initial catalog=sad;integrated security=true")) { //open connection database try { conn.open(); } catch (exception e) { console.writeline(e.tostring()); } //query database update status using (sqlcommand cmd = new sqlcommand("update assessmentform set status = @validated studentid = @studentid , courseno = @coursenumber", conn)) { cmd.parameters.addwithvalue("@validated", notyetvalidated); cmd.parameters.addwithvalue("@studentid", studid); cmd.parameters.addwithvalue("@coursenumber", coursenum); cmd.executenonquery(); } //close connection database try { conn.close(); } catch (exception e) { console.writeline(e.tostring()); } } } }
you need modify page_load method in section iterating inside gridview rows , assigning handlers , setting checkbox property.
in gridview status column guess saving @ text not bit compared text. see following , implement same.
foreach (gridviewrow row in validatesubject.rows) { bool ischecked = default(bool); if (row.cells[row.cells.count - 1].text.equals("validated")) // here please assign position of **status** column. ischecked = true; check = row.cells[row.cells.count - 1].controls[0] checkbox; //position check column on last row in gridview check.enabled = true; check.checkedchanged += validatesubject_click; //bind event on button check.autopostback = true; //set autopostback property true check.checked = ischecked //set checkbox checked based on status ; }
Comments
Post a Comment