转载

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

<%@ 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] = "【孟子E章】" + 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>
原文地址:http://dotnet.aspx.cc/article/007ebf00-1a3a-4d26-b211-51e977fb8eb6/print.aspx

文章最后发布于: 2011-04-16 12:20:00
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 鲸 设计师: meimeiellie

分享到微信朋友圈

×

扫一扫,手机浏览