Batch update of multiple rows implemented by the gridview Layout

Source: Internet
Author: User

<% @ Page Language = "C #" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<% -- Http://topic.csdn.net/u/20080521/15/a2da7051-4c1b-490d-ab28-1f01323aba47.html? Seed = 1952824859 -- %>
<SCRIPT runat = "server">
Void showproductdata (gridview grid)
{
Datatable dt = createsampledata ();
Grid. datasource = DT;
Grid. databind ();
}
# Region sample data
Static datatable createsampleemptydatatable ()
{
Datatable TBL = new datatable ("student ");
TBL. Columns. Add ("studentno", typeof (string ));
TBL. Columns. Add ("firstname", typeof (string ));
TBL. Columns. Add ("lastname", typeof (string ));
TBL. Columns. Add ("Age", typeof (INT ));
TBL. Columns. Add ("gender", typeof (string ));
Return TBL;
}
Static datatable createsampledata ()
{
Datatable TBL = createsampleemptydatatable ();
TBL. Rows. Add ("20021342", "Jack", "Wu", 25, "M ");
TBL. Rows. Add ("20025341", "Jue", "you", 23, "F ");
TBL. Rows. Add ("20022254", "Viky", "Huang", 24, "F ");
TBL. Rows. Add ("20022231", "Leo", "Wong", 24, "M ");
Return TBL;
}
# Endregion
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Showproductdata (gridview1 );
}
}
Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
Datarowview DRV = E. Row. dataitem as datarowview;
// Synchronize ddlgender selected items
If (DRV! = NULL)
{
Dropdownlist ddlgender = E. Row. findcontrol ("ddlgender") as dropdownlist;
If (ddlgender! = NULL)
{
Ddlgender. selectedvalue = (string) DRV ["gender"];
}
}
}
Protected void gridviewincluprerender (Object sender, eventargs E)
{
// Maintain the client status
Bool B;
String M = "", n = "NONE ";
Foreach (gridviewrow row in gridview1.rows)
{
Checkbox chk = row. findcontrol ("chkedit") as checkbox;
// If (chk! = NULL)
//{
B = chk. checked;
// The following code does not perform the null check to ensure that controls with corresponding IDs exist.
// Establish a robust program and check non-null references.
//
(Row. findcontrol ("lblfirstname") as webcontrol). Style [htmltextwriterstyle. Display] = B? N: m;
(Row. findcontrol ("txtfirstname") as webcontrol). Style [htmltextwriterstyle. Display] = B? M: N;
//
(Row. findcontrol ("lbllastname") as webcontrol). Style [htmltextwriterstyle. Display] = B? N: m;
(Row. findcontrol ("txtlastname") as webcontrol). Style [htmltextwriterstyle. Display] = B? M: N;
//
(Row. findcontrol ("lblage") as webcontrol). Style [htmltextwriterstyle. Display] = B? N: m;
(Row. findcontrol ("txtage") as webcontrol). Style [htmltextwriterstyle. Display] = B? M: N;
//
(Row. findcontrol ("lblgender") as webcontrol). Style [htmltextwriterstyle. Display] = B? N: m;
(Row. findcontrol ("ddlgender") as webcontrol). Style [htmltextwriterstyle. Display] = B? M: N;
//}
}
}
Protected void btnupdate_click (Object sender, eventargs E)
{
Bool updated = false;
Foreach (gridviewrow row in gridview1.rows)
{
Checkbox chk = row. findcontrol ("chkedit") as checkbox;
If (chk! = NULL & chk. Checked)
{
// The following code does not perform the null check to ensure that controls with corresponding IDs exist.
// Establish a robust program and check non-null references.
String newfirstname = (row. findcontrol ("txtfirstname") as textbox). Text. Trim ();
String newlastname = (row. findcontrol ("txtlastname") as textbox). Text. Trim ();
Int newage = int. parse (row. findcontrol ("txtage") as textbox). Text. Trim ());
String newgender = (row. findcontrol ("ddlgender") as dropdownlist). selectedvalue;
//************************************** **************************************** ************/
// Perform the real database update operation here
//....
Response. write ("<SCRIPT> alert ('implement your database update code in [btnupdate_click (Object sender, eventargs E)] ') <" + "/SCRIPT> ");
//....
//************************************** **************************************** ************/
Updated = true;
}
}
//// If there is any data update, load the new data from the database and refresh the page
//// Of course, if you care about the performance, you can copy the textbox value to the label, but it takes time to implement it.
// If (updated)
//{
// Showproductdata ();
//}
}
</SCRIPT>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> ASP. NET demo16_gridviewbatchedit </title>
<SCRIPT type = "text/JavaScript">
VaR $ = function (ID) {return document. getelementbyid (ID );}
Function chkedit_click (sender, argS)
{
// Debugger;
// Switch the Editable status
VaR B = sender. checked;
VaR M = "", n = "NONE ";
//
$ (ARGs. containerclientid + "_" + "lblfirstname"). style. Display = B? N: m;
$ (ARGs. containerclientid + "_" + "txtfirstname"). style. Display = B? M: N ;;
//
$ (ARGs. containerclientid + "_" + "lbllastname"). style. Display = B? N: m;
$ (ARGs. containerclientid + "_" + "txtlastname"). style. Display = B? M: N ;;
//
$ (ARGs. containerclientid + "_" + "lblage"). style. Display = B? N: m;
$ (ARGs. containerclientid + "_" + "txtage"). style. Display = B? M: N ;;
//
$ (ARGs. containerclientid + "_" + "lblage"). style. Display = B? N: m;
$ (ARGs. containerclientid + "_" + "txtage"). style. Display = B? M: N ;;
//
$ (ARGs. containerclientid + "_" + "lblgender"). style. Display = B? N: m;
$ (ARGs. containerclientid + "_" + "ddlgender"). style. Display = B? M: N ;;
}
Function shouldupdate ()
{
VaR GRD = $ ('<% = gridview1.clientid %> ');
VaR inputs = GRD. getelementsbytagname ('input ');
For (VAR I = 0; I <inputs. length; I ++ ){
If (inputs [I]. type = "checkbox" & inputs [I]. Id. indexof ("chkedit")> 0 & inputs [I]. Checked ){
Return true;
}
}
Return false;
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<H3> batch update implemented through the gridview layout <Em> Please implement your database update code in [btnupdate_click (Object sender, eventargs E)] </em>
<Br/>
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" onrowdatabound = "gridview1_rowdatabound" onprerender = "gridview1_prerender">
<Columns>
<Asp: templatefield headertext = "edit? ">
<Itemtemplate>
<Asp: checkbox id = "chkedit" onclick = '<% # string. format (@ "chkedit_click (this, {containerclientid:" "{0}" "})", (container as control ). clientid) %>'
Tooltip = "Enable Edit? "Runat =" server "/>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield headertext = "studentno">
<Itemtemplate>
<% # (Container. dataitem as datarowview) ["studentno"] %> <! -- Avoid reflection and improve performance -->
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield headertext = "firstname">
<Itemtemplate>
<Asp: Label id = "lblfirstname" runat = "server" text = '<% # (container. dataitem as datarowview) ["firstname"] %>'/>
<Asp: textbox id = "txtfirstname" runat = "server" text = '<% # (container as control ). findcontrol ("lblfirstname") as label ). text %> 'style = "display: none;"/>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield headertext = "lastname">
<Itemtemplate>
<Asp: Label id = "lbllastname" runat = "server" text = '<% # eval ("lastname") %>'/>
<Asp: textbox id = "txtlastname" runat = "server" text = '<% # (container as control ). findcontrol ("lbllastname") as label ). text %> 'style = "display: none;"/>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield headertext = "Age">
<Itemtemplate>
<Asp: Label id = "lblage" runat = "server" text = '<% # eval ("Age") %>'/>
<Asp: textbox id = "txtage" runat = "server" text = '<% # (container as control ). findcontrol ("lblage") as label ). text %> 'style = "display: none;"/>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield headertext = "gender">
<Itemtemplate>
<Asp: Label id = "lblgender" runat = "server" text = '<% # eval ("gender"). tostring () = "M "? "Male": "female" %> '/>
<Asp: dropdownlist id = "ddlgender" runat = "server" style = "display: none">
<Asp: listitem value = "M" text = "male"/>
<Asp: listitem value = "F" text = "female"/>
</ASP: dropdownlist>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>
<Input id = "button2" type = "button" value = "reload" onclick = "location. href = location. href;"/>
<Asp: button id = "btnupdate" runat = "server" text = "Update" onclick = "btnupdate_click" onclientclick = "If (! Shouldupdate () Return !! Alert ('No data can be updated! '); "/>
</Div>
</Form>
</Body>
</Html>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.