首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

为何实现不了分页

2013-02-25 
为什么实现不了分页head idHead1 runatservertitle无标题页/titlelink href../CSS/CSS.css

为什么实现不了分页

<head id="Head1" runat="server">
    <title>无标题页</title>
    <link href="../CSS/CSS.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .style1
        {
            width: 390px;
            height: 34px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table align="center" class="css" style="width: 570px; height: 301px" border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td align="center" class="style1">
                        企业公告</td>
                </tr>
                <tr>
                    <td style="width: 390px">
                        <asp:DataList ID="DataList1" runat="server" BackColor="white" BorderColor="White"
                            BorderStyle="none" BorderWidth="1px" CellPadding="2" 
                            GridLines="horizontal" OnDeleteCommand="DataList1_DeleteCommand" Width="536px" 
                            onitemcommand="DataList1_ItemCommand" 
                            onitemdatabound="DataList1_ItemDataBound" 
                           >
                            <ItemTemplate>
                                <table border="0" class="css" style="width: 658px; height: 0px" cellpadding="0" cellspacing="0">
                                    <tr>


                                        <td style="width: 898px">
                                            标题:<%# DataBinder.Eval(Container.DataItem, "title")%></td>
                                        <td align="center" style="width: 266px">
                                            <asp:ImageButton ID="ImageButton1" runat="server" AlternateText="删除公告" CommandName="delete"
                                                Height="21px" Width="61px" ImageUrl="~/images/delete.gif" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" style="height: 40px; vertical-align: top;">
                                            <%# DataBinder.Eval(Container.DataItem, "content")%>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="width: 898px">


                                            时间:<%# DataBinder.Eval(Container.DataItem, "noteTime", "{0:yyyy-MM-dd}")%></td>
                                        <td style="width: 266px">
                                            发布人:<%# DataBinder.Eval(Container.DataItem, "noteperson")%></td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                            <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                            <AlternatingItemStyle BackColor="#F7F7F7" />
                            <ItemStyle BackColor="White" ForeColor="#4A3C8C" />
                           <FooterTemplate>
         <div style="text-align: center">
                    <table id="Page" border="1" cellpadding="0" cellspacing="0" 
                        style="font-size: 12px; width: 68%">
                        <tr>
                            <td >
                <asp:Label ID="labCurrentPage" runat="server">当前页</asp:Label>
                <asp:Label ID="labPageCount" runat="server">总页数</asp:Label>


                <asp:LinkButton ID="lnkbtnFirst" runat="server" CommandName="first" Font-Underline="False"
                                    ForeColor="Black">首页</asp:LinkButton>
                <asp:LinkButton ID="lnkbtnFront" runat="server" CommandName="pre" Font-Underline="False"
                                    ForeColor="Black">上一页</asp:LinkButton> 
                <asp:LinkButton ID="lnkbtnNext" runat="server" CommandName="next" Font-Underline="False"
                                    ForeColor="Black">下一页</asp:LinkButton>
                <asp:LinkButton ID="lnkbtnLast" runat="server" CommandName="last" Font-Underline="False"
                                    ForeColor="Black">尾页</asp:LinkButton>
                                &nbsp;&nbsp; 跳转至:<asp:TextBox ID="txtPage" runat="server" Width="35px" Height="21px"></asp:TextBox>
                                <asp:Button ID="Button1" runat="server" CommandName="search" Text="GO"
                                    Height="19px" />
                <br />
                            </td>
                        </tr>
                    </table>
                </div>
        </FooterTemplate>

                            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                        </asp:DataList></td>
                </tr>


                <tr>
                    <td style="width: 390px">
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>
后台代码:
public partial class BaseInfo_BaseNoticeList : System.Web.UI.Page
{
    BaseClass bc = new BaseClass();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["loginName"] == null)
        {
            Response.Write("<script>this.parent.location.href='../login.aspx'</script>");
            return;
        }
        if (!IsPostBack)
        {
            DataList1.DataSource = bc.GetDataSet("select * from tb_note order by id desc", "tb_note");
            DataList1.DataKeyField = "id";
            DataList1.DataBind();
        }
        if (Session["loginName"].ToString().ToLower() != "tsoft")
            ((ImageButton)DataList1.Items[0].FindControl("ImageButton1")).Visible = false;
    }
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int str = (int)DataList1.DataKeys[e.Item.ItemIndex];
        bool bl;
        bl = bc.ExecSQL("delete from tb_note where id='" + str + "'");
        Response.Write(bc.MessageBox("删除成功!"));
        DataList1.DataSource = bc.GetDataSet("select * from tb_note order by id desc", "tb_note");
        DataList1.DataKeyField = "id";
        DataList1.DataBind();

    }
    
    protected static PagedDataSource pds = new PagedDataSource();//创建一个分页数据源的对象且一定要声明为静态

    private void BindDataList(int currentpage)
    {
        pds.AllowPaging = true;//允许分页
        pds.PageSize = 3;//每页显示3条数据
        pds.CurrentPageIndex = currentpage;//当前页为传入的一个int型值


        string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
              SqlConnection con = new SqlConnection(connStr);
               string strSql = "SELECT * FROM tb_note";//定义一条SQL语句
        con.Open();//打开数据库连接 (当然此句可以不写的)
        SqlDataAdapter sda = new SqlDataAdapter(strSql, con);
        DataSet ds = new DataSet();
        sda.Fill(ds);//把执行得到的数据放在数据集中
        pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中
        DataList1.DataSource = pds;//绑定Datalist
        DataList1.DataBind();
        con.Close();
    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        switch (e.CommandName)
        {      case "first"://第一页
                pds.CurrentPageIndex = 0;
                BindDataList(pds.CurrentPageIndex);
                break;
            case "pre"://上一页
                pds.CurrentPageIndex = pds.CurrentPageIndex - 1;
                BindDataList(pds.CurrentPageIndex);
                break;
            case "next"://下一页
                pds.CurrentPageIndex = pds.CurrentPageIndex + 1;
                BindDataList(pds.CurrentPageIndex);
                break;
            case "last"://最后一页
                pds.CurrentPageIndex = pds.PageCount - 1;
                BindDataList(pds.CurrentPageIndex);
                break;
            case "search"://页面跳转页
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    int PageCount = int.Parse(pds.PageCount.ToString());


                    TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;
                    int MyPageNum = 0;
                    if (!txtPage.Text.Equals(""))
                        MyPageNum = Convert.ToInt32(txtPage.Text.ToString());
                    if (MyPageNum <= 0 || MyPageNum > PageCount)
                    {
                        Response.Write("<script>alert('请输入页数并确定没有超出总页数!')</script>");
                        txtPage.Text = "";
                    }
                    else
                        BindDataList(MyPageNum - 1);
                }
                break;
        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            Label CurrentPage = e.Item.FindControl("labCurrentPage") as Label;
            Label PageCount = e.Item.FindControl("labPageCount") as Label;
            LinkButton FirstPage = e.Item.FindControl("lnkbtnFirst") as LinkButton;
            LinkButton PrePage = e.Item.FindControl("lnkbtnFront") as LinkButton;
            LinkButton NextPage = e.Item.FindControl("lnkbtnNext") as LinkButton;
            LinkButton LastPage = e.Item.FindControl("lnkbtnLast") as LinkButton;
            CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();//绑定显示当前页
            PageCount.Text = pds.PageCount.ToString();//绑定显示总页数
            if (pds.IsFirstPage)//如果是第一页,首页和上一页不能用
            {


                FirstPage.Enabled = false;
                PrePage.Enabled = false;
            }
            if (pds.IsLastPage)//如果是最后一页"下一页"和"尾页"按钮不能用
            {
                NextPage.Enabled = false;
                LastPage.Enabled = false;
            }
        }
    }

}
[解决办法]
    protected static PagedDataSource pds = new PagedDataSource();//创建一个分页数据源的对象且一定要声明为静态
    private void BindDataList(int currentpage)
    {

 pds.CurrentPageIndex 永远都是0
[解决办法]
建议楼主还是用分页控件吧http://www.webdiyer.com/AspNetPagerDemo/default.aspx
[解决办法]
http://www.51aspx.com/code/AspNetPager72Samples

热点排行