返回首页
当前位置: 主页 > 网络编程 > .Net实例教程 >

实现GridView的LinkButton 删除按钮添加弹出层的确认删除对话框

时间:2011-06-03 10:52来源:2018年最新注册送彩金www.zhixing123.cn 编辑:麦田守望者

ASPX 代码
 

<%@ Page Language="C#" EnableViewState="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// 创建示例数据绑定源
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("UserId", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(System.String)));

System.Random rd = new System.Random();
for (int i = 1; i < 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "【2018年最新注册送彩金】" + i.ToString();
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
GridView1.DataSource = dv;
GridView1.DataBind();
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton x = e.Row.FindControl("x") as LinkButton;
x.Attributes.Add("onclick", "return ShowDiv('" + x.UniqueID + "')");
}
}

protected void x_Click(object sender, EventArgs e)
{
LinkButton x = sender as LinkButton;
Response.Write("你要删除的 UserId = " + x.CommandArgument);
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>给 GridView 的 LinkButton 删除按钮添加弹出层的确认删除对话框</title>
<script>
var linkbutton = null;
function ShowDiv(o) {
linkbutton = o;
document.getElementById("d").style.display = "";
return false;
}
function Yes() {
if (linkbutton) {
__doPostBack(linkbutton, '');
}
}
function No() {
linkbutton = null;
document.getElementById("d").style.display = "none";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound"
DataKeyNames="UserId">
<Columns>
<asp:TemplateField HeaderText="UserId">
<ItemTemplate>
<%#Eval("UserId")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="名称">
<ItemTemplate>
<%#Eval("Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="x" runat="server" CommandArgument='<%#Eval("UserId") %>' Text="删除" OnClick="x_Click" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
<div id="d" style="background: red; display: none; width: 200px; height: 200px">
<h5>测试对话框,自己美化下吧</h5>
<input type="button" value="确认" onclick="Yes()" />
<input type="button" value="取消" onclick="No()" />
</div>
</body>
</html>

顶一下
(0)
0%
踩一下
(0)
0%
标签(Tag):C# C#实例教程 c#基础教程 C#源代码
------分隔线----------------------------
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片
博聚网