Asp. NET instance: Pop-up confirmation dialog box when GridView deletes

Source: Internet
Author: User

Effect Chart:

HTML code

<table align= "center" bgcolor= "#c0de98" border= "0" cellpadding= "0" cellspacing= "1" width= "99%" >
<tr>
<th colspan= "2" >
GridView Demo </th>
</tr>
<tr>
&LT;TD colspan= "2" style= "width:100%;" >
<asp:gridview id= "GridView" runat= "Server" width= "100%" autogeneratecolumns= "False" allowpaging= "True" onpageindexchanging= "gridview_pageindexchanging" pagesize= "onrowdeleting=" "gridview_rowdeleting" onrowdatabound= "Gridview_rowdatabound" >
<Columns>
<asp:boundfield datafield= "UserID" headertext= "UserID" readonly= "True"/>
<asp:boundfield datafield= "C_name" headertext= "Chinese name" readonly= "True"/>
<asp:boundfield datafield= "E_name" headertext= "English name" readonly= "True"/>
<asp:boundfield datafield= "QQ" headertext= "QQ account"/>
<asp:commandfield headertext= "Delete" showdeletebutton= "True"/>
</Columns>
<rowstyle horizontalalign= "Center"/>
<pagerstyle horizontalalign= "right"/>
</asp:GridView>
</td>
</tr>
</table>

C # code

Using System;
Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
public partial class Demo11:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (Page.IsPostBack = = False)
{
Binddata ();
}
}
public void Binddata ()
{
String strSQL = "Select Userid,c_name,e_name,qq from Demo_user";
DataTable dt = SqlHelper.ExecuteDataset (sqlhelper.conn_string, CommandType.Text, strSQL, null). Tables[0];
Gridview.datasource = DT;
Gridview.datakeynames = new string[] {"UserID"};//primary key
Gridview.databind ();
}
protected void Gridview_pageindexchanging (object sender, Gridviewpageeventargs e)
{
Gridview.pageindex = E.newpageindex;
Binddata ();
}
protected void Gridview_rowdeleting (object sender, Gridviewdeleteeventargs e)
{
int UserID = (int) Gridview.datakeys[e.rowindex]. Value;
String strSQL = "Delete demo_user where userid= @UserID";
Sqlparameter[] Para = {
New SqlParameter ("@UserID", UserID),
};
Sqlhelper.executenonquery (sqlhelper.conn_string, CommandType.Text, strSQL, para);
Binddata ();
}
protected void Gridview_rowdatabound (object sender, GridViewRowEventArgs e)
{
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
if (e.row.rowstate = = Datacontrolrowstate.normal E.row.rowstate = = datacontrolrowstate.alternate)
{
((LinkButton) e.row.cells[4]. Controls[0]). Attributes.Add ("onclick", "Javascript:return Confirm" ("You are sure to delete: \" "+ e.row.cells[1]. Text + "\"? ")"
}
}
}
}

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.