0

我是 asp.net 的新手,有一些线索,但不知道如何做到这一点,所以任何帮助都会非常感谢。

我有一个带分页的数据列表如何通过下拉列表从用户输入更新数据列表,这将绑定我的数据列表和分页?

我的私人财产

private int CurrentPage
    {
        get
        {
            object objPage = ViewState["_CurrentPage"];
            int _CurrentPage = 0;
            if (objPage == null)
            {
                _CurrentPage = 0;
            }
            else
            {
                _CurrentPage = (int)objPage;
            }
            return _CurrentPage;
        }
        set { ViewState["_CurrentPage"] = value; }
    }
    private int fistIndex
    {
        get
        {

            int _FirstIndex = 0;
            if (ViewState["_FirstIndex"] == null)
            {
                _FirstIndex = 0;
            }
            else
            {
                _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
            }
            return _FirstIndex;
        }
        set { ViewState["_FirstIndex"] = value; }
    }
    private int lastIndex
    {
        get
        {

            int _LastIndex = 0;
            if (ViewState["_LastIndex"] == null)
            {
                _LastIndex = 0;
            }
            else
            {
                _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
            }
            return _LastIndex;
        }
        set { ViewState["_LastIndex"] = value; }
    }

私有方法

private void BindItemsList()
    {

        DataTable dataTable = dm.GetDataTable().Tables["product"];// middle tier which pulls data from a database
        _PageDataSource.DataSource = dataTable.DefaultView;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = 1;
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _PageDataSource.PageCount;
        this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
        this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;

        this.dlPhones.DataSource = _PageDataSource;
        this.dlPhones.DataBind();
        this.doPaging();
    }

    /// <summary>
    /// Binding Paging List
    /// </summary>
    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");

        fistIndex = CurrentPage - 5;


        if (CurrentPage > 5)
        {
            lastIndex = CurrentPage + 5;
        }
        else
        {
            lastIndex = 10;
        }
        if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
        {
            lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
            fistIndex = lastIndex - 10;
        }

        if (fistIndex < 0)
        {
            fistIndex = 0;
        }

        for (int i = fistIndex; i < lastIndex; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }

        this.dlPaging.DataSource = dt;
        this.dlPaging.DataBind();
    }

页面代码背后的代码

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindItemsList();
        }
    }//end of page load
    protected void lbtnNext_Click(object sender, EventArgs e)
    {

        CurrentPage += 1;
        this.BindItemsList();

    }
    protected void lbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        this.BindItemsList();

    }
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            this.BindItemsList();
        }
    }
    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Style.Add("fone-size", "14px");
            lnkbtnPage.Font.Bold = true;

        }
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {

        CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
        this.BindItemsList();

    }
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {

        CurrentPage = 0;
        this.BindItemsList();


    }

asp.net 头版

<asp:Label ID="lblSearch" runat="server" Text="Search By Phone"></asp:Label>
                <asp:DropDownList ID="ddlCat" runat="server">
                    <asp:ListItem>Nokia</asp:ListItem>
                    <asp:ListItem>Samsung</asp:ListItem>
                    <asp:ListItem>Motorola</asp:ListItem>
                    <asp:ListItem>Sony</asp:ListItem>
                    <asp:ListItem>Ericsson</asp:ListItem>
                    <asp:ListItem>Sony</asp:ListItem>
                    <asp:ListItem>LG</asp:ListItem>
                    <asp:ListItem>Apple</asp:ListItem>
                    <asp:ListItem>HTC</asp:ListItem>
                    <asp:ListItem>BlackBerry</asp:ListItem>
                    <asp:ListItem>HP</asp:ListItem>
                    <asp:ListItem>Huawei</asp:ListItem>
                    <asp:ListItem>Acer</asp:ListItem>
                    <asp:ListItem>Asus</asp:ListItem>
                    <asp:ListItem>Dell</asp:ListItem>
                    <asp:ListItem>Alcatel</asp:ListItem>
                    <asp:ListItem>Vodafone</asp:ListItem>
                    <asp:ListItem>T-Mobile</asp:ListItem>
                    <asp:ListItem>Toshiba</asp:ListItem>
                    <asp:ListItem>Gigabyte</asp:ListItem>
                    <asp:ListItem>Pantech</asp:ListItem>
                    <asp:ListItem>ZTE</asp:ListItem>
                    <asp:ListItem>Micromax</asp:ListItem>
                    <asp:ListItem>BLU</asp:ListItem>
                    <asp:ListItem>Spice</asp:ListItem>
                    <asp:ListItem>Icemobile</asp:ListItem>
                    <asp:ListItem>verykool</asp:ListItem>
                    <asp:ListItem>Vertu</asp:ListItem>
                    <asp:ListItem>Celkon</asp:ListItem>
                    <asp:ListItem>NIU</asp:ListItem>
                    <asp:ListItem>Yezz</asp:ListItem>
                    <asp:ListItem>Parla</asp:ListItem>
                    <asp:ListItem>Plum</asp:ListItem>
                    <asp:ListItem>Sim Card</asp:ListItem>
                </asp:DropDownList>
                <asp:DataList ID="dlPhones" runat="server" DataKeyField="PID" RepeatDirection="Horizontal" RepeatColumns="1" CellPadding="10" CellSpacing="10" >
                    <ItemTemplate>           
                        <asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' />   
                        <br />Price:
                        <asp:Label ID="PriceLabel" runat="server" Text='<%# "$" + Eval("Price") %>'  />
                        <br />

                        <asp:Image ID="Image1" runat="server" CssClass="ImageStyles" ImageUrl='<%# "GetImage.ashx?Id=" + Eval("PID") %>' BackColor="White" BorderStyle="Ridge" BorderColor="WhiteSmoke" />
                        <br />           
                    </ItemTemplate>
                </asp:DataList>                
               <table cellpadding="0" border="0">
                    <tr>
                        <td align="right">
                            <asp:LinkButton ID="lbtnFirst" runat="server" CausesValidation="false" OnClick="lbtnFirst_Click"><img src="images/First.png" height="50px" width="100px" onmouseover="this.src='images/FirstMouseOver.png';" onmouseout="this.src='images/First.png';" /></asp:LinkButton>
                            &nbsp;</td>
                        <td align="right">
                            <asp:LinkButton ID="lbtnPrevious" runat="server" CausesValidation="false" OnClick="lbtnPrevious_Click"><img src="images/Previouse.png" onmouseover="this.src='images/PreviouseMouseOver.png';" onmouseout="this.src='images/Previouse.png';" /></asp:LinkButton>&nbsp;&nbsp;</td>
                        <td align="center" valign="middle">
                            <asp:DataList ID="dlPaging" runat="server" RepeatDirection="Horizontal" OnItemCommand="dlPaging_ItemCommand"
                                OnItemDataBound="dlPaging_ItemDataBound">
                                <ItemTemplate>
                                    <asp:LinkButton ID="lnkbtnPaging" Font-Size="X-Large" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
                                        CommandName="Paging" Text='<%# Eval("PageText") %>'></asp:LinkButton>&nbsp;
                                </ItemTemplate>
                            </asp:DataList>
                        </td>
                        <td align="left">
                            &nbsp;&nbsp;<asp:LinkButton ID="lbtnNext" runat="server" CausesValidation="false"
                                OnClick="lbtnNext_Click"><img src="images/Next.png" onmouseover="this.src='images/NextMouseOver.png';" onmouseout="this.src='images/Next.png';" /></asp:LinkButton></td>
                        <td align="left">
                            &nbsp;
                            <asp:LinkButton ID="lbtnLast" runat="server" CausesValidation="false" OnClick="lbtnLast_Click"><img src="images/Last.png" onmouseover="this.src='images/LastMouseOver.png';" onmouseout="this.src='images/Last.png';" /></asp:LinkButton></td>
                    </tr>
                    <tr>
                        <td colspan="5" align="center" style="height: 30px" valign="middle">
                            <asp:Label ID="lblPageInfo" runat="server"></asp:Label></td>
                    </tr>
                </table>
4

1 回答 1

0

我自己想出了要发布解决方案的内容,以防万一有人遇到同样的问题。

我不确定这是否是最好的方法,所以请原谅我。

1) 创建一个Session["search"] = "First";

2)创建下拉列表onselectindexchange的方法

<asp:DropDownList ID="ddlCat" runat="server" AutoPostBack="true" OnSelectedIndexChanged="itemSelected">

Code behind method

protected void itemSelected(object sender, EventArgs e)
    {
        Session["search"] = "Second";
        RebindItemList();
    }

3) 重新绑定方法

private void RebindItemList()
    {
        DataTable dataTable = dm.GetSpecifProduct(ddlCat.SelectedItem.Text).Tables["product"];// middle tier method with returns a Dataset or DataTable up to you.
        _PageDataSource.DataSource = dataTable.DefaultView;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = 1;
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _PageDataSource.PageCount;
        this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
        this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;

        this.dlPhones.DataSource = _PageDataSource;
        this.dlPhones.DataBind();
        this.doPaging();
    }

4) 分页按钮的逻辑语句

protected void lbtnNext_Click(object sender, EventArgs e)
    {

        CurrentPage += 1;
        string searchCat = (string) Session["search"];
        if (searchCat == "First")
        {
            this.BindItemsList();
        }
        if (searchCat == "Second")
        {
            this.RebindItemList();
        }
    }
    protected void lbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        string searchCat = (string)Session["search"];
        if (searchCat == "First")
        {
            this.BindItemsList();
        }
        if (searchCat == "Second")
        {
            this.RebindItemList();
        }

    }
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            string searchCat = (string)Session["search"];
            if (searchCat == "First")
            {
                this.BindItemsList();
            }
            if (searchCat == "Second")
            {
                this.RebindItemList();
            }
        }
    }
    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Style.Add("fone-size", "14px");
            lnkbtnPage.Font.Bold = true;

        }
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {

        CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
        string searchCat = (string)Session["search"];
        if (searchCat == "First")
        {
            this.BindItemsList();
        }
        if (searchCat == "Second")
        {
            this.RebindItemList();
        }

    }
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {

        CurrentPage = 0;
        string searchCat = (string)Session["search"];
        if (searchCat == "First")
        {
            this.BindItemsList();
        }
        if (searchCat == "Second")
        {
            this.RebindItemList();
        }


    }
于 2012-12-01T23:41:18.943 回答