»áÔ±£º ÃÜÂ룺 ¡¡Ãâ·Ñ×¢²á | Íü¼ÇÃÜÂë | »áÔ±µÇ¼ ÍøÒ³¹¦ÄÜ£º ¼ÓÈëÊÕ²Ø ÉèΪÊ×Ò³ ÍøÕ¾ËÑË÷  
 °²È«¼¼Êõ¼¼ÊõÎĵµ
  ¡¤ °²È«ÅäÖÆ
  ¡¤ ¹¤¾ß½éÉÜ
  ¡¤ ºÚ¿Í½Ìѧ
  ¡¤ ·À»ðǽ
  ¡¤ Â©¶´·ÖÎö
  ¡¤ ÆƽâרÌâ
  ¡¤ ºÚ¿Í±à³Ì
  ¡¤ ÈëÇÖ¼ì²â
 °²È«¼¼ÊõÂÛ̳
  ¡¤ °²È«ÅäÖÆ
  ¡¤ ¹¤¾ß½éÉÜ
  ¡¤ ·À»ðǽ
  ¡¤ ºÚ¿ÍÈëÇÖ
  ¡¤ Â©¶´¼ì²â
  ¡¤ Æƽⷽ·¨
  ¡¤ É±¶¾×¨Çø
 °²È«¼¼Êõ¹¤¾ßÏÂÔØ
  ¡¤ É¨Ã蹤¾ß
  ¡¤ ¹¥»÷³ÌÐò
  ¡¤ ºóÃÅľÂí
  ¡¤ ¾Ü¾ø·þÎñ
  ¡¤ ¿ÚÁîÆƽâ
  ¡¤ ´úÀí³ÌÐò
  ¡¤ ·À»ðǽ
  ¡¤ ¼ÓÃܽâÃÜ
  ¡¤ ÈëÇÖ¼ì²â
  ¡¤ ¹¥·ÀÑÝʾ
¼¼ÊõÎĵµ > Web¿ª·¢ > ASP.Net
Ç×ÃܽӴ¥ASP.Net(13)
·¢±íÈÕÆÚ£º2003-06-26 00:00:00×÷Õߣº· ³ö´¦£º  

¸öÐÔ»¯µÄ·ÖҳʵÏÖ

ÎÒÃÇÇ°Ãæ½²µÄ·ÖÒ³£¬Ö»²»¹ýÊÇͨ¹ýÐÞ¸ÄDataGridµÄÊôÐÔÀ´ÊµÏÖ·ÖÒ³£¬ÕâÑùÓÐÕâÑùµÄºÃ´¦£¬×î´óµÄ¾ÍÊǼòµ¥£¬ºÇºÇ£¬¸ù±¾²»ÓòÙÐÄ£¬·ÖÒ³ÊÇÈçºÎ²úÉúµÄ¡£

µ«ÊÇËüͬÑùÓÐȱµã£¬²»ÄÜ°´ÕÕÎÒÃÇÏëÏñµÄ²úÉú¸÷ÖÖÎÒÃÇÐèÒªµÄÑùʽ¡£

ûÓа취£¬Ïë¸öÐÔ»¯¹¦ÄÜ£¬Ö»ÓÐ×ÔÒѶ¯ÊÖÀ´×öÁË£¬ºÇºÇ¡£

ÎÒÃÇÒ»²½²½µÄÀ´£¬Ê×ÏÈÊǵ¼ÈëÐèÒªµÄÃüÃû¿Õ¼ä¡£ÏÂÃæµÄÀý×Ó£¬ÆäʵҲÊÇÎÒ´Ó¹úÍâÕÒÀ´µÄ£¬ÔÙ¼ÓÉϵã¸öÈ˵Ķ«¶«£¬ÔÙºº»¯£¬ºÇºÇ¡£½ñÌìÐÄÇéºÜºÃ£¬ÎÒÁ¬±êÇ©É«²Ê¶¼¸ø´ó¼ÒÏÔʾ³öÀ´ÁË¡£ºÇºÇ£¬¸üÀûÓÚ´ó¼Ò¿´³ÌÐò¡£

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SQL" %>

ÎÒÃÇÏÈ¿´¿´£¬ÎÒÃǵÄweb¿Ø¼þÊÇÄÄЩ£¬ÔÙ¿´¿´´úÂëÊÇÔõôдµÄ£¬ÕâÑù±È½ÏºÃ£º£©

<body>

<h3><font face="Verdana">¸öÐÔ»¯µÄ·ÖҳʵÀý^&^</font></h3>

<form runat=server>

<ASP:DataGrid id="MyDataGrid" runat="server"

AllowPaging="True"

PageSize="10"

PagerStyle-Mode="NumericPages"

PagerStyle-HorizontalAlign="Right"

OnPageIndexChanged="MyDataGrid_Page"

BorderColor="black"

BorderWidth="1"

GridLines="Both"

CellPadding="3"

CellSpacing="0"

Font-Name="Verdana"

Font-Size="8pt"

HeaderStyle-BackColor="#aaaadd"

AlternatingItemStyle-BackColor="#eeeeee"

AutoGenerateColumns="false"

>

<property name="Columns">

<asp:BoundColumn HeaderText="¹¤×÷ÊÒ³ÉÔ±" DataField="Enter_ID" />

<asp:BoundColumn HeaderText="µÇ½ʱ¼ä" DataField="Enter_Time" />

</property>

</ASP:DataGrid>

<p>

<asp:LinkButton id="btnFirst" runat="server"

Text="Ê×Ò³"

CommandArgument="0"

ForeColor="navy"

Font-Name="verdana" Font-size="8pt"

OnClick="PagerButtonClick"

/>

 

<asp:LinkButton id="btnPrev" runat="server"

Text="Ç°Ò³"

CommandArgument="prev"

ForeColor="navy"

Font-Name="verdana" Font-size="8pt"

OnClick="PagerButtonClick"

/>

 

<asp:LinkButton id="btnNext" runat="server"

Text="ºóÒ³"

CommandArgument="next"

ForeColor="navy"

Font-Name="verdana" Font-size="8pt"

OnClick="PagerButtonClick"

/>

 

<asp:LinkButton id="btnLast" runat="server"

Text="Ä©Ò³"

CommandArgument="last"

ForeColor="navy"

Font-Name="verdana" Font-size="8pt"

OnClick="PagerButtonClick"

/>

<p>

<asp:Checkbox id="chk1" runat="server"

Text="ÏÔʾÄÚÖõÄÒ³Êý"

Font-Name="Verdana"

Font-Size="8pt"

AutoPostBack="true"

/>

<p>

<table bgcolor="#eeeeee" cellpadding="6"><tr><td nowrap><font face="Verdana" size="-2">

<asp:Label id="lblCurrentIndex" runat="server" /><br>

<asp:Label id="lblPageCount" runat="server" /><br>

</font></td></tr></table>

</form>

</body>

´ÓÉÏÃæµÄÀý×ÓÎÒÃÇ¿ÉÒÔ¿´³öµã»÷LinkButton¿Ø¼þOnClick´¥·¢µÄÊÇPageButtonClickʼþ,DataGridÒ³ÃæOnPageIndexChanged¸Ä±ä´¥·¢µÄÊÇMyDataGrid_Pageʼþ£¬ÎÒÃÇÒÔºó¾ÍÊǾÍÊÇÒª±àдÕâÁ½¼þʼþµÄ´úÂë

ÏÂÃæÊÇÏÈÒª²éѯµÄÊý¾Ý¿âµÄÐÅÏ¢£¬ÓÃÒ»¸öº¯Êý±íʾ£¬ÒòΪ¾­³£Óõ½£º£©,ÎÒ´ò¿ªµÄ±í£¬ºÇºÇ£¬ÊÇÎÒÃǹ¤×÷ÊÒ¹ÜÀíÇøµÄµÇ½¼Ç¼±í£¨¹þÓÖÂôÁ˵ãÎÒÃǹ¤×÷ÊÒµÄÃØÃܸø´ó¼Ò)

ICollection CreateDataSource()

{

  /*

    ¶ÁÈ¡Êý¾Ý¿âµÄÐÅÏ¢£¬»ñµÃDataView

  */

  SQLConnection MyConnection = new SQLConnection("server=localhost;uid=sa;pwd=123456;database=aspcn");

  SQLDataSetCommand MyDataSetCommand = new SQLDataSetCommand("select * from admin_enter order by Enter_Time desc",MyConnection);

  DataSet ds= new DataSet();

  MyDataSetCommand.FillDataSet(ds,"admin_enter");

  return ds.Tables["admin_enter"].DefaultView;

}

È»ºóÖÐÊÇPage_Loadº¯Êý,ÔÚÕâÀïÖ÷ÒªÊÇÅжÏÒ»ÏÂÊÇ·ñÏÔʾDataGrid×Ô´øµÄÄÇЩ·ÖÒ³Êý×Ö£¬Ê¹ÓõÄÊÇPageStyleµÄVisibleÊôÐÔ£º

void Page_Load(Object sender, EventArgs e)

{

  //ÅжÏÊÇ·ñÒþ²ØPagerStyle-Mode

  if (chk1.Checked)

  {

    MyDataGrid.PagerStyle.Visible=true;

  }

  else

  {

    MyDataGrid.PagerStyle.Visible=false;

  }

  BindGrid();

}

ÏÂÃæÊÇ´¦Àíµã»÷ʼþµÄPagerButtonClick£¬ÕâÊÇÎÒÃǵĺËÐIJ¿·Ö£¬ÆäʵÎÒÃDzÙ×÷µÄÒ²Ö»ÊÇDataGridµÄCurrentPageIndexÊôÐÔ¡£Èç¹ûCurrentPageIndexСÓÚPageCountÔòÓÐÏÂÒ»Ò³£¬Èç¹ûCurrentPageIndex´óÓÚ0Ôò±íʾÓÐÇ°Ò»Ò³¡£

void PagerButtonClick(Object sender, EventArgs e)

{

  //»ñµÃLinkButtonµÄ²ÎÊýÖµ

  String arg = ((LinkButton)sender).CommandArgument;

  switch(arg)

  {

    case ("next"):

      if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))

      MyDataGrid.CurrentPageIndex ++;

      break;

    case ("prev"):

      if (MyDataGrid.CurrentPageIndex > 0)

      MyDataGrid.CurrentPageIndex --;

      break;

    case ("last"):

      MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);

      break;

    default:

      //±¾Ò³Öµ

      MyDataGrid.CurrentPageIndex = arg.ToInt32();

      break;

  }

  BindGrid();

}

ÏÂÃæÊÇMyDataGrid_Page,Ö÷Òª²Ù×÷Êǵ÷ÓÃBindGridº¯Êý£¬ÒÔ½«Êý¾Ý½»¸øDataGridÏÔʾ:

void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e)

{

  //´¦Àí°´ÏÂÊý×ֵķ½·¨

  BindGrid();

}

×îºóÊÇÁ½¸öº¯Êý£¬ËûÃǵÄ×÷Óã¬ÎÒ¶¼×¢ÊÍÁË£º£©

void BindGrid()

{

  //½«DataView°ó¶¨µ½DataGridÉÏÈ¥

  MyDataGrid.DataSource = CreateDataSource();

  MyDataGrid.DataBind();

  ShowStats();

}

void ShowStats()

{

  //ÏÔʾҳÃæÐÅÏ¢

  lblCurrentIndex.Text = "µ±Ç°Ò³ÊýΪ£º " + ((int)MyDataGrid.CurrentPageIndex+1);

  lblPageCount.Text = "×ÜÒ³ÊýÊÇ£º " + MyDataGrid.PageCount;

}

µ½´ËΪֹ£¬ÎÒÃǵĸöÐÔ»¯Ò³ÃæÒѾ­Íê³ÉÁË£¨È«²¿´úÂëºÍÏÔʾ¿´Ò»Ï½Ú)£¬ÎÒÃÇ×ܵÄ˼Ïë¾ÍÊÇʹÓÃLinkButton¿Ø¼þ×öΪ·­Ò³µÄ±êÖÄ£¬Í¨¹ýÅжÏLinkButtonµÄCommandArgumentÖµ£¬²Ù×÷DataGridµÄCurrentPageIndexÊôÐÔ£¬ÒÔ´ïµ½·­Ò³µÄЧ¹û¡£

Èç¹û´ó¼ÒÔÚ±¾½ÚÖп´²»¶®½á¹¹£¬Çë²Î¿´ÏÂÒ»½ÚµÄÈ«²¿´úÂëÒÔ¼°Ê¾Àý¡£

¡¡

¡¾·µ»Ø¶¥²¿¡¿ ¡¾´òÓ¡±¾Ò³¡¿ ¡¾¹Ø±Õ´°¿Ú¡¿

¹ØÓÚÎÒÃÇ / ¸øÎÒÁôÑÔ / °æȨ¾Ù±¨ / Òâ¼û½¨Òé / ÍøÕ¾±à³ÌQQȺ   
Copyright ©2003- 2024 Lihuasoft.net webmaster(at)lihuasoft.net ¼ÓÔØʱ¼ä 0.00177