当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET实例: GridView删除时弹出确认对话框

ASP.NET
.Net中使用com组件后发生System.ArithmeticException异常的解决办法
SQL Server.net 和 OLE DB.net连接数据库的比较
后台更新DataTable行内容的方法
敏捷软件开发(原则,模式与实践)笔记1
确保文本框输入值为数值的代码
XML和数据库之间相互的映射
让你的.NET程序兼容不同版本的Dll文件。
.NET 的数据访问应用程序块(Data Access Application Block)
用控件仅一条指令实现界面换肤和多语言版本(YFSkins)
Microsoft User Interface Process Application Block 研究(3)
分享:处理Excel方法小结
基于ASP.NET实现全球化
.net 里面 protected private 的变量也可以访问(新发现)。
关于C#中{0}和{1}的问题初次在此发贴,问题对你易对我难,求救了
使用C#代码实现增加用户帐号
全世界都在关注-微软重大产品发布
教你做Rational Rose(UML Design)
OLE DB取得数据库的架构信息
VB 从零开始编外挂(三)
XPath序列之四

ASP.NET实例: GridView删除时弹出确认对话框


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-04   浏览: 49 ::
收藏到网摘: n/a

效果图:

 

html代码

<table align="center" bgcolor="#c0de98" border="0" cellpadding="0" cellspacing="1" width="99%">
<tr>
<th colspan="2">
GridView演示</th>
</tr>
<tr>
<td colspan="2" style="width: 100%;" >
<asp:GridView ID="GridView" runat="server" Width="100%" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="GridView_PageIndexChanging" PageSize="12" OnRowDeleting="GridView_RowDeleting" OnRowDataBound="GridView_RowDataBound" >
<Columns>
<asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True" />
<asp:BoundField DataField="C_Name" HeaderText="中文名字" ReadOnly="True" />
<asp:BoundField DataField="E_Name" HeaderText="英文名字" ReadOnly="True" />
<asp:BoundField DataField="QQ" HeaderText="QQ帐号" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<RowStyle HorizontalAlign="Center" />
<PagerStyle HorizontalAlign="Right" />
</asp:GridView>
</td>
</tr>
</table>

C#代码

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" };//主键
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('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
}
}
}
}