Common Operations on the page for reading data table lists by using gridview in ASP. NET-sequence number, edit, delete, and delete confirmation

Source: Internet
Author: User

Common Operations on the page for reading data table lists by using gridview in ASP. NET-sequence number, edit, delete, and delete confirmation

Front-end aspx code:

Feature: auto-increment sequence number column, edit and delete button

Gridview1_rowdatabound event: the delete confirmation dialog box when the delete button is added during initialization. For details, see the CS code.

Gridview1_rowcommand event: Click Edit and delete in the list to perform the operation. For details, see CS code.

Key settings 0. Set gridview1 Sequence Number Column: text = '<% # This. gridview1.pageindex * This. gridview1.pagesize + this. gridview1.rows. Count + 1%>

Key settings 1. Set the datakeynames attribute of gridview1 to the primary key of the data table in the business logic layer.

Key Settings 2. Set the commandname attribute of gridview1 to edit or delete

Key settings 3. Set commandargument = '<% # databinder. eval (container, "rowindex") %>' (get row number) or

Commandargument = '<% # eval ("ID") %>' (ID is the primary key of the data table)

 

<Asp: gridview id = "gridview1" runat = "server" cellpadding = "4" performanceid = "objectperformance1"
Forecolor = "#333333" gridlines = "NONE" autogeneratecolumns = "false" width = "778px" datakeynames = "ID" onrowcommand = "gridview1_rowcommand" onrowdatabound = "gridview1_rowdatabound">
<Footerstyle backcolor = "#507cd1" font-bold = "true" forecolor = "white"/>
<Columns>
<Asp: templatefield headertext = "no." insertvisible = "false">
<Itemstyle horizontalalign = "center"/>
<Headerstyle horizontalalign = "center" width = "5%"/>
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # This. gridview1.pageindex * This. gridview1.pagesize + this. gridview1.rows. Count + 1%>'/>
</Itemtemplate>
</ASP: templatefield>
<Asp: boundfield datafield = "tradeway" headertext = "transaction method"/>
<Asp: boundfield datafield = "cityname" headertext = ""/>
<Asp: boundfield datafield = "townname" headertext = "township"/>
<Asp: templatefield headertext = "update operation" insertvisible = "false" showheader = "false">
<Itemtemplate>
<Asp: linkbutton id = "editbutton" runat = "server" commandname = "edit" commandargument = '<% # databinder. eval (container, "rowindex") %> 'causesvalidation = "false" text = "Update"> </ASP: linkbutton>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield headertext = "delete operation" insertvisible = "false" showheader = "false">
<Itemtemplate>
<Asp: linkbutton id = "delbutton" runat = "server" commandname = "delete" commandargument = '<% # eval ("ID ") %> 'causesvalidation = "false" text = "delete"> </ASP: linkbutton>
</Itemtemplate>
</ASP: templatefield>
</Columns>
<Rowstyle backcolor = "# eff3fb"/>
<Editrowstyle backcolor = "# 2461bf"/>
<Selectedrowstyle backcolor = "# d1ddf1" font-bold = "true" forecolor = "#333333"/>
<Pagerstyle backcolor = "# 2461bf" forecolor = "white" horizontalalign = "center"/>
<Headerstyle backcolor = "#507cd1" font-bold = "true" forecolor = "white"/>
<Alternatingrowstyle backcolor = "white"/>
</ASP: gridview>
<Asp: objectdatasource id = "objectperformance1" runat = "server"
Selectmethod = "getbuildinginfolist" typename = "BLL. shopinfo">
</ASP: objectdatasource>

 

Backend CS code

Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
/* The Code cannot be run correctly after the code test page is displayed. The Code has been corrected.

Linkbutton lkbdel = (linkbutton) E. Row. findcontrol ("delbutton ");
Lkbdel. Attributes. Add ("onclick", "javascript: Return confirm ('Do you want to delete this record? "+ This. gridview1.datakeys [E. Row. dataitemindex]. Value + "')");

*/

// The following is the script code of the confirmation dialog box for binding and deletion after pagination.

Linkbutton lkbdel = (linkbutton) E. Row. findcontrol ("delbutton ");
Int currentpageindex = This. gridview1.pageindex;
Int currentpagesize = This. gridview1.pagesize;
Int currentdateitem = E. Row. dataitemindex-currentpageindex * currentpagesize;
Lkbdel. Attributes. Add ("onclick", "javascript: Return confirm ('Do you want to delete this record? "+ This. gridview1.datakeys [currentdateitem]. Value + "')");

}
}
Protected void gridview1_rowcommand (Object sender, gridviewcommandeventargs E)
{
Response. Write ("<SCRIPT> alert ('" + E. commandname + "'); </SCRIPT> ");

If (E. commandname = "edit") // If the input operation name is edit, edit the record.
{

// Method 1 for obtaining the primary key of the current record
Int mykeyid1 = convert. toint32 (gridview1.datakeys [int32.parse (E. commandargument. tostring ()]. Value); // obtain the primary key value based on the row number
Response. Write ("<SCRIPT> alert ('" + mykeyid1.tostring () + "'); </SCRIPT> ");
// Int mykeyid1 = (INT) gridview1.datakeys ["ID"]. value;
// Response. Write (mykeyid. tostring ());
// Method 2 for obtaining the primary key of the current record
// Bind the row number commandargument = '<% # databinder. eval (container, "rowindex") %>'
String mykeyid2 = E. commandargument. tostring ();
Response. Write ("<SCRIPT> alert ('2" + mykeyid2 + "'); </SCRIPT> ");
Response. End ();
// Editrecordbyid (categoryid );

}
If (E. commandname = "delete") // if the name is Delete, delete the record.
{
// Method 1 for obtaining the primary key of the current record
// Int mykeyid1 = (INT) gridview1.datakeys ["ID"]. value;
// Response. Write (mykeyid1.tostring ());
// Method 2 for obtaining the primary key of the current record
String mykeyid2 = E. commandargument. tostring ();
Response. Write ("<SCRIPT> alert ('" + mykeyid2 + "'); </SCRIPT> ");
Response. End ();
// Editrecordbyid (categoryid );
}

}

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.