datagrid標簽分頁

界面設計
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="WebForm1" %>

<html>
 <HEAD>
  <title>WebForm1</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <script>
   window.resizeTo(screen.width,screen.height-30);
   </script>
   <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 56px" runat="server"
    AllowSorting="True" AutoGenerateColumns="False" Width="408px" AllowPaging="True" PageSize="3"
    DataKeyField="au_id" HorizontalAlign="Center">
    <Columns>
     <asp:BoundColumn DataField="au_id" SortExpression="au_id" ReadOnly="True" HeaderText="ID"></asp:BoundColumn>
     <asp:BoundColumn DataField="city" SortExpression="city" ReadOnly="True" HeaderText="城市"></asp:BoundColumn>
     <asp:TemplateColumn SortExpression="au_id" HeaderText="操作">
      <ItemTemplate>
       <asp:LinkButton runat="server" ID="delbutton" Text="刪除" CommandName="Delete" CausesValidation="false"></asp:LinkButton>
      </ItemTemplate>
     </asp:TemplateColumn>
    </Columns>
    <PagerStyle Visible="False" PageButtonCount="6"></PagerStyle>
   </asp:datagrid>
   <asp:label id="Label1" style="Z-INDEX: 106; LEFT: 344px; POSITION: absolute; TOP: 168px" runat="server">
   </asp:label><asp:linkbutton id="LBtfir" style="Z-INDEX: 105; LEFT: 104px; POSITION: absolute; TOP: 168px" runat="server">首頁</asp:linkbutton>
   <asp:linkbutton id="LBtlast" style="Z-INDEX: 104; LEFT: 288px; POSITION: absolute; TOP: 168px" runat="server">尾頁</asp:linkbutton>
   <asp:linkbutton id="LBtpre" style="Z-INDEX: 103; LEFT: 224px; POSITION: absolute; TOP: 168px" runat="server">上一頁</asp:linkbutton>
   <asp:linkbutton id="LBtnex" style="Z-INDEX: 102; LEFT: 152px; POSITION: absolute; TOP: 168px" runat="server">下一頁</asp:linkbutton>
   </form>
 </body>

</html>

c#后台代碼

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

//Namespace DataGrid
//{

/// <summary>
/// WebForm1 的摘要說明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
 protected System.Web.UI.WebControls.LinkButton LBtnex;
 protected System.Web.UI.WebControls.LinkButton LBtpre;
 protected System.Web.UI.WebControls.LinkButton LBtlast;
 protected System.Web.UI.WebControls.LinkButton LBtfir;
 protected System.Web.UI.WebControls.Label Label1;
 protected System.Web.UI.WebControls.DataGrid DataGrid1;
 public string SortField;

 private void Page_Load(object sender, System.EventArgs e)
 {
  // 在此處放置用戶代碼以初始化頁面
  if(!Page.IsPostBack)
  {
   databind();
  }
 }
 public void databind()
 {
  SqlConnection con = new SqlConnection("server=d00-09;database=pubs;uid=sa;pwd=");
  con.Open();
  string sql="select * from authors";
  SqlDataAdapter ada = new SqlDataAdapter(sql,con);
  DataSet ds = new DataSet();
  ada.Fill(ds,"temp");
  ds.Tables["temp"].DefaultView.Sort=SortField;
  DataGrid1.DataSource=ds.Tables["temp"].DefaultView;
  DataGrid1.DataBind();
  ada.Dispose();
  con.Close();
  Label1.Text="共"+ds.Tables["temp"].DefaultView.Count.ToString()+"條記錄,"+DataGrid1.PageCount.ToString()+"頁,第"+(DataGrid1.CurrentPageIndex+1).ToString()+"頁,本頁"+DataGrid1.Items.Count.ToString()+"條記錄";
 }

 #region Web 表單設計器生成的代碼
 override protected void OnInit(EventArgs e)
 {
  //
  // CODEGEN: 該調用是 ASP.NET Web 表單設計器所必需的。
  //
  InitializeComponent();
  base.OnInit(e);
 }

 /// <summary>
 /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
 /// 此方法的內容。
 /// </summary>
 private void InitializeComponent()
 {
  this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
  this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand_1);
  this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound_1);
  this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
  this.LBtfir.Click += new System.EventHandler(this.LBtfir_Click);
  this.LBtlast.Click += new System.EventHandler(this.LBtlast_Click);
  this.LBtpre.Click += new System.EventHandler(this.LBtpre_Click);
  this.LBtnex.Click += new System.EventHandler(this.LBtnex_Click);
  this.Load += new System.EventHandler(this.Page_Load);

 }
 #endregion

 private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
 {
  DataGrid1.CurrentPageIndex=e.NewPageIndex;
  databind();

 }

 private void LBtnex_Click(object sender, System.EventArgs e)
 {
  if(DataGrid1.CurrentPageIndex<DataGrid1.PageCount-1)
  {
   DataGrid1.CurrentPageIndex++;
   databind();
  }
 }

 private void LBtpre_Click(object sender, System.EventArgs e)
 {
  if(DataGrid1.CurrentPageIndex>0)
  {
   DataGrid1.CurrentPageIndex--;
   databind();
  }
 }

 private void LBtfir_Click(object sender, System.EventArgs e)
 {
  DataGrid1.CurrentPageIndex=0;
  databind();
 }

 private void LBtlast_Click(object sender, System.EventArgs e)
 {
  DataGrid1.CurrentPageIndex=DataGrid1.PageCount-1;
  databind();
 }

 private void DataGrid1_DeleteCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 {
  try
  {

   string ID = (string)DataGrid1.DataKeys[(int) e.Item.ItemIndex];
   string sql="delete from authors where au_id='"+Convert.ToString(ID)+"'";
   SqlConnection con = new SqlConnection("server=d00-09;database=pubs;uid=sa;pwd=");
   con.Open();
   SqlCommand com=new SqlCommand(sql,con);
   com.ExecuteNonQuery();
   com.Dispose();
   con.Close();
   if (DataGrid1.Items.Count == 1 && DataGrid1.CurrentPageIndex > 0)
   {
    DataGrid1.CurrentPageIndex--;
   }
   databind();
  }
  catch(Exception ex)
  {
   Response.Write("<script>alert('"+ex.Message+"')</script>");
   //Page.RegisterStartupScript("","<script>alert('"+ex.Message+"')</script>");

  }

 }

 private void DataGrid1_ItemDataBound_1(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
 {
  if(e.Item.ItemIndex<0) return;
  e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='cccccc'");
  e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor");
  LinkButton lbtnDel = (LinkButton)e.Item.FindControl("delbutton");
  string ID = (string)DataGrid1.DataKeys[(int) e.Item.ItemIndex];
  lbtnDel.Attributes.Add("onclick", "return confirm('您真的要刪除"+ID+ " 行嗎?');");

 }

 private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
 {
  SortField=(string)e.SortExpression;
  databind();
 
 }

 private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
 {
//  'Response.Write ("<script>alert('dd');</script>");
 }


}
//}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值