aspx:

 

 

 

 <asp:DataGrid ID="DataGrid1" AutoGenerateColumns="false" runat="server" AllowPaging="true"
                AllowSorting="True" BorderColor="#33CCFF" CellPadding="4" ForeColor="#333333"
                GridLines="None" PageSize="2" ShowFooter="True" OnSelectedIndexChanged="DataGrid1_SelectedIndexChanged1"   >
                <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" 
                 />
                <AlternatingItemStyle BackColor="White" />
                <ItemStyle BackColor="#EFF3FB" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <Columns>
                    <asp:TemplateColumn HeaderText="全选">
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox3" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="编辑">
                        <HeaderStyle Width="45px" />
                        <ItemTemplate>
                            <a href="UserList.aspx?user_id=<%# Eval("user_id")%>&op=edit"">编辑</a>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="查看">
                        <HeaderStyle Width="45px" />
                        <ItemTemplate>
                            <a href="UserList.aspx?user_id=<%# Eval("user_id")%>&op=view"">查看</a>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="删除">
                        <HeaderStyle Width="45px" />
                        <ItemTemplate>
                            <a href="UserList.aspx?user_id=<%# Eval("user_id")%>&op=delete"">删除</a>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="全选">
                        <HeaderStyle Width="10px"></HeaderStyle>
                        <HeaderTemplate>
                            <!--  <input id="CheckAll"  value="全选"name="CheckAll" type="checkbox" onclick="ChooseAll()">-->
                            <span></span>
                            <input id="CheckAll" name="CheckAll" type="button" value="全选" onclick="ChooseAll()" />
                            <input id="Checkbox2" name="Checkbox2" type="button" value="删除" onclick="DelRec()">
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server"></asp:CheckBox>
                        </ItemTemplate>
                        <ItemTemplate>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:BoundColumn DataField="userName" HeaderText="userName"></asp:BoundColumn>
                    <asp:BoundColumn DataField="passWord" HeaderText="passWord"></asp:BoundColumn>
                    <asp:BoundColumn DataField="user_id" HeaderText="user_id"></asp:BoundColumn>
                </Columns>
            </asp:DataGrid>
          
                        <asp:LinkButton ID="btnFirst" runat="server" CommandArgument="fist" OnClick="btnFirst_Click">首页</asp:LinkButton>&nbsp;&nbsp;
                        <asp:LinkButton ID="btnPrev" runat="server"   CommandArgument="prev" OnClick="btnPrev_Click">上一页

                        </asp:LinkButton>&nbsp;&nbsp;
                        <asp:LinkButton ID="btnNext" runat="server" CommandArgument="next" OnClick="btnNext_Click">下一页

                        </asp:LinkButton>&nbsp;&nbsp;
                        <asp:LinkButton ID="btnLast" runat="server" CommandArgument="last" OnClick="btnLast_Click">末页</asp:LinkButton>&nbsp;&nbsp;
                        <asp:Label ID="lblCurrentIndex" runat="server"></asp:Label><asp:Label ID="lblPageCount"
                            runat="server"></asp:Label>&nbsp;&nbsp; 跳转到<asp:TextBox ID="txtGoPage" runat="server"
                                Width="30px" CssClass="textbox"></asp:TextBox>
                        <asp:Button ID="btnGo" runat="server" Text="GO" CssClass="button" Width="29px" OnClick="btnGo_Click1">
                        </asp:Button>

 

 

 

 

aspx.cs

 

 public void BindData()
    {

        DataTable dt = UserData.SelectAll();
        DataGrid1.DataSource = dt;
        DataGrid1.DataBind();

        ShowStatsPage();
    }

 

    public void PagerButtonClick(object sender, System.EventArgs e)
    {
        //获得LinkButton的参数值
        String arg = ((LinkButton)sender).CommandArgument;

        switch (arg)
        {
            case ("next"):
                if (DataGrid1.CurrentPageIndex < (DataGrid1.PageCount - 1))
                    DataGrid1.CurrentPageIndex++;
                break;
            case ("prev"):
                if (DataGrid1.CurrentPageIndex > 0)
                    DataGrid1.CurrentPageIndex--;
                break;
            case ("fist"):
                DataGrid1.CurrentPageIndex = 0;
                break;
            case ("last"):
                DataGrid1.CurrentPageIndex = (DataGrid1.PageCount - 1);
                break;
            default:
                //本页值
                DataGrid1.CurrentPageIndex = Convert.ToInt32(arg);
                break;
        }
        BindData();
    }

    public void ShowStatsPage()
    {
        //显示页面信息
        lblCurrentIndex.Text = "当前为第:" + ((int)DataGrid1.CurrentPageIndex + 1) + "页</font>]";
        lblPageCount.Text = ">>共:" + DataGrid1.PageCount + "页</font>]";
    }

 

    public void MyDataGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
        //处理按下数字的方法
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        BindData();
    }

 

    public void btnGo_Click1(object sender, System.EventArgs e)
    {
        //页面直接跳转的代码
        if (txtGoPage.Text.Trim() != "")
        {
            int PageI = Int32.Parse(txtGoPage.Text.Trim()) - 1;
            if (PageI >= 0 && PageI < (DataGrid1.PageCount))
                DataGrid1.CurrentPageIndex = PageI;
        }
        BindData();
    }
    //----------------------翻页代码结束

 


    protected void btnFirst_Click(object sender, EventArgs e)
    {
        PagerButtonClick(sender, e);
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        PagerButtonClick(sender, e);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        PagerButtonClick(sender, e);
    }

    protected void btnLast_Click(object sender, EventArgs e)
    {
        PagerButtonClick(sender, e);
    }
    protected void DataGrid1_SelectedIndexChanged(object sender, EventArgs e)
    {
        PagerButtonClick(sender, e);
    }

 

 

 

posted on 2009-04-23 18:05  releaseyou  阅读(299)  评论(0编辑  收藏  举报