前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="goodgl.aspx.cs" Inherits="yonghugl" %>
<!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="LinkButton1" runat="server" οnclick="LinkButton1_Click">返回</asp:LinkButton>
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/title.jpg" Height="143px" Width="100%" ImageAlign="Top"/>
</div>
<div>
<table>
<asp:DataGrid ID="dgNotice" runat="server" DataKeyField="GoodsId"
AllowPaging="true" PageSize="5" AutoGenerateColumns="false"
OnPageIndexChanged="dgNotice_PageIndexChanged" Width="585px"
ondeletecommand="dgNotice_DeleteCommand" >
<Columns>
<asp:BoundColumn DataField="GoodsId" HeaderText="商品ID">
<HeaderStyle BackColor="ActiveBorder" HorizontalAlign="center" />
<ItemStyle HorizontalAlign="center" />
</asp:BoundColumn>
<asp:BoundColumn DataField="GoodsTitle" HeaderText="商品名称" >
<HeaderStyle BackColor="Aqua" HorizontalAlign="Center"/>
<ItemStyle HorizontalAlign="center" />
</asp:BoundColumn>
<asp:BoundColumn DataField="Content" HeaderText="描述">
<HeaderStyle BackColor="ActiveBorder" HorizontalAlign="center" />
<ItemStyle HorizontalAlign="center" />
</asp:BoundColumn>
<asp:BoundColumn DataField="ContractMan" HeaderText="上传者">
<HeaderStyle BackColor="ActiveBorder" HorizontalAlign="center" />
<ItemStyle HorizontalAlign="center" />
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="删除">
<HeaderStyle HorizontalAlign="center" Width="40px" BackColor="ActiveBorder" />
<ItemStyle HorizontalAlign="center" />
<ItemTemplate >
<asp:LinkButton ID="sc" runat="server" CommandName="delete">
<img src="../image/delete.gif" />
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Mode="NumericPages" />
</asp:DataGrid>
</table>
<table >
</table>
</div>
</form>
</body>
</html>
后台
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class yonghugl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
NoticeDataBind();
}
protected void NoticeDataBind()
{
string sql;
sql = "select * from Goods ";
DataTable dt = Class1.GetDsFromSql(sql).Tables[0];
dgNotice.DataSource = dt;
dgNotice.DataBind();
}
protected void dgNotice_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
dgNotice.CurrentPageIndex = e.NewPageIndex;
NoticeDataBind();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Server.Transfer("guanli.aspx");
}
protected void dgNotice_DeleteCommand(object source, DataGridCommandEventArgs e)
{
string id = dgNotice.DataKeys[e.Item.ItemIndex].ToString();
string sqlStr = "delete from Goods where ";
sqlStr += "GoodsId='" + id.ToString() + "'";
if (Class1.ExecuteSql(sqlStr))
{
NoticeDataBind();
}
else
{
}
}
}