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

 更新时间:2009年12月04日 15:00:24   作者:  
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 + "\"吗?')");
}
}
}
}

相关文章

  • 详解mvc使用JsonResult返回Json数据

    详解mvc使用JsonResult返回Json数据

    这篇文章主要介绍了详解mvc使用JsonResult返回Json数据,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-01-01
  • 微信JS-SDK分享功能的.Net实现代码

    微信JS-SDK分享功能的.Net实现代码

    这篇文章主要介绍了微信JS-SDK分享功能的.Net实现代码的相关资料,需要的朋友可以参考下
    2017-09-09
  • Datatable删除行的Delete和Remove方法的区别介绍

    Datatable删除行的Delete和Remove方法的区别介绍

    Datatable删除行的Delete和Remove方法的区别介绍,需要的朋友可以参考一下
    2013-03-03
  • ASP.NET 通过拦截器记录错误日志的示例代码

    ASP.NET 通过拦截器记录错误日志的示例代码

    这篇文章主要介绍了ASP.NET 通过拦截器记录错误日志的示例代码,帮助大家更好的理解和学习使用.NET技术,感兴趣的朋友可以了解下
    2021-04-04
  • asp.net利用google的api做翻译

    asp.net利用google的api做翻译

    google提供了一组API可以给我们很方便的实现语言翻译功能,对于我们(中国人)而言,常用的是中文与英文的互译。
    2009-05-05
  • asp.net 预防SQL注入攻击之我见

    asp.net 预防SQL注入攻击之我见

    说起防止SQL注入攻击,感觉很郁闷,这么多年了大家一直在讨论,也一直在争论,可是到了现在似乎还是没有定论。当不知道注入原理的时候会觉得很神奇,怎么就被注入了呢?会觉得很难预防。但是当知道了注入原理之后预防不就是很简单的事情了吗?
    2009-11-11
  • 利用委托把用户控件的值显示于网页案例应用

    利用委托把用户控件的值显示于网页案例应用

    用户控件(UserControl)是集成一个功能,需要处理好的数据,然后存数据库中并显示于网页上,让用户能检测到处理的数据情况,接下来将介绍利用委托把用户控件的值显示于网页上,感兴趣的朋友可以了解下
    2013-02-02
  • asp.net Coolite TablePanel使用

    asp.net Coolite TablePanel使用

    TabPanel控件使用非常简单,但是功能却非常强大,它同MenuPanel、TreePanel一样提供了很多的集合属性,可以定制出丰富的应用。
    2010-03-03
  • .NET8实现PDF合并的示例代码

    .NET8实现PDF合并的示例代码

    这篇文章主要为大家详细介绍了如何使用.NET8实现PDF合并的效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-12-12
  • ASP.NET Core依赖注入系列教程之服务的注册与提供

    ASP.NET Core依赖注入系列教程之服务的注册与提供

    这篇文章主要给大家介绍了关于ASP.NET Core依赖注入系列教程之服务的注册与提供的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
    2018-11-11

最新评论