前台:
<asp:DataGrid ID=GgGrid runat=server AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Height="12px" Width="100%" OnDeleteCommand="GgGrid_DeleteCommand" OnItemDataBound="GgGrid_ItemDataBound" AllowPaging="True" OnPageIndexChanged="GgGrid_PageIndexChanged" PageSize="20">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditItemStyle BackColor="#2461BF" />
<SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" Mode="NumericPages" />
<AlternatingItemStyle BackColor="White" />
<ItemStyle BackColor="#EFF3FB" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundColumn DataField="id" HeaderText="编号"></asp:BoundColumn>
<asp:HyperLinkColumn DataNavigateUrlField="id" DataNavigateUrlFormatString="Admin_Edit.aspx?id={0}"
DataTextField="title" HeaderText="标题"></asp:HyperLinkColumn>
<asp:ButtonColumn CommandName="Delete" Text="删除" HeaderText="删除"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
后台:
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_Gg : 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>");
}
this.DataToBind();
}
private void DataToBind()
{
string ptype = Request.QueryString["p"];
MyConn = DB.CreateDB();
MyConn.Open();
OleDbCommand cmd = new OleDbCommand("select * from product where pro_name='"+ptype+"'", MyConn);
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds, "product");
this.GgGrid.DataKeyField = "id";
this.GgGrid.DataSource = ds;
this.GgGrid.DataBind();
MyConn.Close();
}
protected void GgGrid_DeleteCommand(object source, DataGridCommandEventArgs e)
{
MyConn = DB.CreateDB();
MyConn.Open();
string ID = this.GgGrid.DataKeys[e.Item.ItemIndex].ToString();
OleDbCommand cmd1 = new OleDbCommand("delete from product where id=" + ID, MyConn);
cmd1.ExecuteNonQuery();
MyConn.Close();
this.DataToBind();
}
protected void GgGrid_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");
((LinkButton)(e.Item.Cells[2].Controls[0])).Attributes.Add("onclick", "return confirm('确认删除?')");
}
}
protected void GgGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
this.GgGrid.CurrentPageIndex = e.NewPageIndex;
this.DataToBind();
}
}