前台:

 

<%@ 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 ></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();
    }


}

 

相关文章: