当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net GridView 删除时弹出确认对话框(包括内容提示)

ASP.NET
谈谈ASP.net2.0中App_GlobalResources的用途
.Net Framework 4.0 功能介绍
ASP.NET教程:浅谈Asp.net实现的邮件发送引擎
ASP.NET教程:绝对路径与相对路径的拼合方法
ASP.NET开发电子商务网站学习经验
ASP.NET与PHP构建web程序的方法的优缺点
ASp.NET教程:页面传值的五种方法
ASP.NET教程:数据缓存和输出缓存
ASP.NET比拼PHP的测试环境
ASP.NET MVC 2的客户端验证扩展
ASP.NET实例:无刷新的文件上传
ASP.NET实例: GridView删除时弹出确认对话框
ASP.NET获取不到js写的cookie解决方法
ASP.NET MVC教程:数据库表的增删改
ASP.NET教程:form验证用户登录的Cookie
如何让.NET程序脱离.NET框架
ASP.NET开发网站程序常见错误汇总
C#教程:匿名类型和隐式类型变量的区别
C#3.0教程:自动属性和扩展方法
使用XmlDocument读取XML节点所有数据

ASP.NET 中的 asp.net GridView 删除时弹出确认对话框(包括内容提示)


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

GridView 删除时弹出确认对话框(包括内容提示) 效果图:

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 + "\"吗?')");
}
}
}
}