DataGrid中表格中删除按钮操作

前台:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Admin_Guest.aspx.cs" Inherits="Admin_Admin_Guest" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>无标题页</title>
    <STYLE type=text/css>BODY {
 MARGIN-TOP: 0px;
 SCROLLBAR-FACE-COLOR: #799ae1;
 FONT-SIZE: 12px;
 BACKGROUND: #cad7f7;
 SCROLLBAR-HIGHLIGHT-COLOR: #799ae1;
 SCROLLBAR-SHADOW-COLOR: #799ae1;
 SCROLLBAR-3DLIGHT-COLOR: #799ae1;
 SCROLLBAR-ARROW-COLOR: #ffffff;
 SCROLLBAR-TRACK-COLOR: #aabfec;
 SCROLLBAR-DARKSHADOW-COLOR: #799ae1;
 margin-left: 0px;
 margin-right: 0px;
 margin-bottom: 0px;
 
}
TD {
 FONT-SIZE: 12px
}
INPUT {
 BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; FONT-SIZE: 12px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px
}
TEXTAREA {
 BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; FONT-SIZE: 12px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px
}
SELECT {
 BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; FONT-SIZE: 12px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px
}
SPAN {
 FONT-SIZE: 12px; POSITION: static
}
A {
 COLOR: #000000; TEXT-DECORATION: none
}
A:hover {
 COLOR: #428eff; TEXT-DECORATION: underline
}
A.highlight {
 COLOR: red; TEXT-DECORATION: none
}
A.highlight:hover {
 COLOR: red
}
A.thisclass {
 FONT-WEIGHT: bold; TEXT-DECORATION: none
}
A.thisclass:hover {
 FONT-WEIGHT: bold
}
A.navlink {
 COLOR: #000000; TEXT-DECORATION: none
}
A.navlink:hover {
 COLOR: #003399; TEXT-DECORATION: none
}
.twidth {
 WIDTH: 500px
}
.content {
 FONT-SIZE: 14px; MARGIN: 5px 20px; LINE-HEIGHT: 140%; FONT-FAMILY: Tahoma,宋体
}
.aTitle {
 FONT-WEIGHT: bold; FONT-SIZE: 15px
}
TD.forumHeaderBackgroundAlternate {
 BACKGROUND-IMAGE: url(images/admin_top_bg.gif); COLOR: #000000; BACKGROUND-COLOR: #799ae1
}
#TableTitleLink A:link {
 COLOR: #ffffff; TEXT-DECORATION: none
}
#TableTitleLink A:visited {
 COLOR: #ffffff; TEXT-DECORATION: none
}
#TableTitleLink A:active {
 COLOR: #ffffff; TEXT-DECORATION: none
}
#TableTitleLink A:hover {
 COLOR: #ffffff; TEXT-DECORATION: underline
}
TD.forumRow {
 PADDING-RIGHT: 3px; PADDING-LEFT: 3px; BACKGROUND: #f1f3f5; PADDING-BOTTOM: 3px; PADDING-TOP: 3px
}
TH {
 FONT-WEIGHT: bold; FONT-SIZE: 12px; BACKGROUND-IMAGE: url(images/admin_bg_1.gif); COLOR: white; BACKGROUND-COLOR: #4455aa
}
TD.bodytitle {
 BACKGROUND-IMAGE: url(images/admin_bg_2.gif)
}
TD.bodytitle1 {
 BACKGROUND-IMAGE: url(images/admin_bg_3.gif)
}
TD.tablebody1 {
 PADDING-RIGHT: 3px; PADDING-LEFT: 3px; BACKGROUND: #bebbdb; PADDING-BOTTOM: 3px; PADDING-TOP: 3px
}
TD.forumRowHighlight {
 PADDING-RIGHT: 3px; PADDING-LEFT: 3px; BACKGROUND: #e4edf9; PADDING-BOTTOM: 3px; PADDING-TOP: 3px
}
.tableBorder {
 BORDER-RIGHT: #183789 1px solid; BORDER-TOP: #183789 1px solid; BORDER-LEFT: #183789 1px solid; WIDTH: 98%; BORDER-BOTTOM: #183789 1px solid; BACKGROUND-COLOR: #ffffff
}
.tableBorder1 {
 WIDTH: 98%
}
.helplink {
 FONT: 10px verdana,arial,helvetica,sans-serif; CURSOR: help; TEXT-DECORATION: none
}
.copyright {
 PADDING-RIGHT: 1px; BORDER-TOP: #6595d6 1px dashed; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; FONT: 11px verdana,arial,helvetica,sans-serif; COLOR: #4455aa; PADDING-TOP: 1px; TEXT-DECORATION: none
}
.menuskin {
 BORDER-RIGHT: #666666 1px solid; BORDER-TOP: #666666 1px solid; BACKGROUND-IMAGE: url(../skins/default/dvmenubg3.gif); VISIBILITY: hidden; FONT: 12px Verdana; BORDER-LEFT: #666666 1px solid; BORDER-BOTTOM: #666666 1px solid; BACKGROUND-REPEAT: repeat-y; POSITION: absolute; BACKGROUND-COLOR: #efefef
}
.menuskin A {
 PADDING-RIGHT: 10px; PADDING-LEFT: 25px; BEHAVIOR: url(inc/noline.htc); COLOR: black; TEXT-DECORATION: none
}
#mouseoverstyle {
 BORDER-RIGHT: #597db5 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: #597db5 1px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 2px; BORDER-LEFT: #597db5 1px solid; PADDING-TOP: 0px; BORDER-BOTTOM: #597db5 1px solid; BACKGROUND-COLOR: #c9d5e7
}
#mouseoverstyle A {
 COLOR: black
}
.menuitems {
 PADDING-RIGHT: 1px; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; MARGIN: 2px; WORD-BREAK: keep-all; PADDING-TOP: 1px
}
</STYLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>
    <form id="form1" runat="server">
   
    <asp:DataGrid ID=GuestList runat=server AutoGenerateColumns="False" Width="95%" BorderWidth="0px" CellPadding="0" ShowHeader="False" AllowPaging="True" OnPageIndexChanged="GuestList_PageIndexChanged" PageSize="3" Height="320px" OnDeleteCommand="GuestList_DeleteCommand">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <br />
     <table width="700" height="211" border="0" align="center" cellpadding="3" cellspacing="1" class="tableBorder">
      <tbody>
        <tr>
          <th class="tableHeaderText" height="25" align="left">&nbsp;<asp:Button ID="BtnDelete" runat="server" CommandName="Delete"  CommandArgument='<%# DataBinder.Eval(Container.DataItem,"id")%>' Text="删除该留言"/></th>
        </tr>
      </tbody>
      <tbody>
        <tr>
          <td height="23" class="forumRow"><table width="100%" border="0" cellpadding="0" cellspacing="5">
              <tr>
                <td width="12%" height="12" align="left" style="width: 13%; height: 12px;">您的姓名:</td>
                <td width="26%" style="height: 12px"><%# DataBinder.Eval(Container.DataItem,"name") %></td>
                <td width="15%" style="height: 12px">联系电话:</td>
                <td width="47%" style="height: 12px"><%# DataBinder.Eval(Container.DataItem,"tel") %></td>
              </tr>
          </table></td>
        </tr>
        <tr>
          <td height="23" class="forumRow"><table width="100%" border="0" cellpadding="0" cellspacing="5">
              <tr>
                <td style="width: 9%" align="left">您的邮箱:</td>
                <td style="width: 19%"><%# DataBinder.Eval(Container.DataItem,"email") %></td>
                <td style="width: 8%">您的地址:</td>
                <td width="33%"><%# DataBinder.Eval(Container.DataItem,"addr") %></td>
              </tr>
          </table></td>
        </tr>
        <tr>
          <td height="23" class="forumRow"><table width="100%" border="0" cellpadding="0" cellspacing="5">
              <tr>
                <td width="14%" align="left" style="width: 12%">留言标题:</td>
                <td width="86%"><%# DataBinder.Eval(Container.DataItem,"title") %></td>
              </tr>
          </table></td>
        </tr>
        <tr>
          <td valign="top" class="forumRow"><table width="100%" border="0" cellpadding="0" cellspacing="5">
            <tr>
              <td width="13%" align="left" style="height: 27px">留言内容:</td>
              <td width="39%" align=left style="height: 27px"><%# DataBinder.Eval(Container.DataItem,"content") %></td>
              <td width="48%" align=left style="height: 27px"></td>
            </tr>
          </table></td>
        </tr>
      </tbody>
</table>

    </ItemTemplate>
        <HeaderStyle Width="100%" />
    </asp:TemplateColumn>
       
    </Columns>
        <FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
            Font-Underline="False" HorizontalAlign="Center" />
        <PagerStyle Mode="NumericPages" NextPageText="下一页" PrevPageText="上一页" />
    </asp:DataGrid>&nbsp;
    </form>
</body>
</html>

 

后台:

 

using System;
using System.Data;
using System.Data.OleDb;
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 Admin_Admin_Guest : System.Web.UI.Page
{
    OleDbConnection MyConn;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["UserID"] == "" || Session["UserID"] == null)
        {
            Response.Write("<script language=javascript>window.alert('为了系统安全,请您重新登陆');window.location.href=('Admin_Login.aspx')</script>");
        }
        if (!IsPostBack)
        {
            this.DataToBind();
        }
    }
    public void DataToBind()
    {
        MyConn = DB.CreateDB();
        MyConn.Open();
        OleDbCommand cmd = new OleDbCommand("select * from guestbook order by id desc", MyConn);
        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        da.Fill(ds, "guestbook");
        this.GuestList.DataSource = ds;
        this.GuestList.DataKeyField = "id";
        this.GuestList.DataBind();
        MyConn.Close();
    }

    protected void GuestList_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            e.Item.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#1e90ff'");
            e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            ((Button)(e.Item.Cells[0].Controls[0])).Attributes.Add("onclick", "return confirm('确认删除?')");

        }
    }
    protected void GuestList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        this.GuestList.CurrentPageIndex = e.NewPageIndex;
        this.DataToBind();
    }


    protected void GuestList_DeleteCommand(object sender, DataGridCommandEventArgs e)
    {

        MyConn = DB.CreateDB();
        int id = int.Parse(e.CommandArgument.ToString());
        Response.Write(id);
        //this.GuestList.DataKeys[e.Item.ItemIndex].ToString();
        MyConn.Open();
        OleDbCommand cmd = new OleDbCommand("delete from guestbook where id=" + id, MyConn);
        cmd.ExecuteNonQuery();
        MyConn.Close();
        this.DataToBind();
    }


}

 

转载于:https://www.cnblogs.com/net2/archive/2010/06/18/1760616.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值