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

ASP.NET
一个无刷新效果定时自动更新页面的例子
ASP.NET2.0的控件状态和视图状态探讨
用好ASP.NET 2.0的URL映射
详解:如何在.NET中访问MySQL数据库?
如何实现Asp与Asp.Net共享Session
利用.net的强大功能发送email
.NET中加密与解密QueryString的方法
Asp.net生成htm静态文件的两种途径
C#定时器的使用
从XML文件中读取数据绑定到DropDownList
ASP.NET 2.0 里输出文本格式流
用.net动态创建类的实例
.Net下的MSMQ的同步异步调用
ASP.NET 2.0实现防止同一用户同时登陆
asp.NET自定义服务器控件内部细节
组合.NET数据控件构建强大用户接口
用ASP.NET 2.0 FormView控件控制显示
菜鸟也学习ASP.NET如何读取数据库内容
教你简单方便获取Web设计的免费资源
专家详解:复杂表达式的执行步骤

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 64 ::
收藏到网摘: 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 + "\"吗?')");
}
}
}
}