DateGradeView分页绑定

时间:2021-05-20 12:35:37

<form method="post" id="nform" runat="server">
               <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" DataKeyNames="CollageID"
                    Width="90%" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCreated="GridView1_RowCreated"
                   CellPadding="4" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" BackColor="White"
                   BorderColor="#3366CC"  BorderStyle="None" BorderWidth="1px"  >               
                <Columns>
                    <asp:BoundField DataField="CollageName" HeaderText="学院名称" />
                    <asp:BoundField DataField="Explain" HeaderText="学院简介" />
                    <asp:BoundField DataField ="CreateTime" HeaderText="创建时间"/>   
                    <asp:HyperLinkField DataNavigateUrlFields="CollageName" DataNavigateUrlFormatString="CollageTableDetail.aspx?id={0}" Text="查看详情" HeaderText="详细信息" />
                    <asp:HyperLinkField DataNavigateUrlFields="CollageID" DataNavigateUrlFormatString="CallbackURl.aspx?id={0}" HeaderText="操作" Text="删除" />
                </Columns>
                   <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                   <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                   <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Center" />
                <PagerTemplate>
                    <asp:LinkButton ID="cmdFirstPage" runat="server" CommandName="Page" CommandArgument="First" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>" CausesValidation="False">首页</asp:LinkButton>
                    <asp:LinkButton ID="cmdPreview" runat="server" CommandArgument="Prev" CommandName="Page" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>" CausesValidation="False">上一页</asp:LinkButton>
                    <asp:PlaceHolder runat="server" ID="Numeric" />
                    <asp:LinkButton ID="cmdNext" runat="server" CommandName="Page" CommandArgument="Next" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>" CausesValidation="False">下一页</asp:LinkButton>
                    <asp:LinkButton ID="cmdLastPage" runat="server" CommandArgument="Last" CommandName="Page" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>" CausesValidation="False">末页</asp:LinkButton>
                </PagerTemplate>
                   <RowStyle BackColor="White" ForeColor="#003399" />
                   <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                   <SortedAscendingCellStyle BackColor="#EDF6F6" />
                   <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                   <SortedDescendingCellStyle BackColor="#D6DFDF" />
                   <SortedDescendingHeaderStyle BackColor="#002876" />
            </asp:GridView>
           </form>        
  </div>

<!--后台代码-->

protected IList<CollageTable> list = new List<CollageTable>();         MesaageBal bal = new MesaageBal();         protected void Page_Load(object sender, EventArgs e)         {             Bind();         }         public void Bind()         {             list = bal.selectcollage();             GridView1.DataSource = list;             GridView1.DataBind();         }         protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)         {             GridView1.PageIndex = e.NewPageIndex;             GridView1.DataBind();

}

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)         {             if (e.Row.RowType == DataControlRowType.Pager)             {                 PlaceHolder ph = e.Row.FindControl("Numeric") as PlaceHolder;                 for (int i = 1; i <= GridView1.PageCount; i++)                 {                     var btn = new LinkButton { CommandName = "Page", CommandArgument = i.ToString(), Text = "[" + i + "]" };                     ph.Controls.Add(i - 1 != GridView1.PageIndex ? (Control)btn : new LiteralControl(i.ToString()));                     ph.Controls.Add(new LiteralControl(" "));                 }             }         }         protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)         {             int indexs = GridView1.SelectedIndex;            Response.Write(indexs+"");         }