2012-11-30 48 views
0

我是新來的asp.net,有一些線索,但不知道如何做到這一點,所以任何幫助將是偉大的謝謝你。如何將具有分頁功能的Datalist與下拉列表綁定?

我有一個Datalist與分頁如何更新從用戶輸入通過下拉列表綁定我的datalist和分頁的datalist?

我的私人性質

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> 

回答

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(); 
     } 


    }