2013-12-16 182 views
1

嗨我想有一個DropDownList外的GridView,顯示頁碼列表。當用戶點擊頁碼時,GridView應該進入該頁面。我能夠填充DropDownList的,但它不是在GridViewGridView跳轉到頁面DropDownList

在這裏工作是我的GridView和DropDownList的

<asp:DropDownList ID="ddlPageNumber" runat="server" AutoPostBack="True"  OnSelectedIndexChanged="ddlPaging_SelectedIndexChanged"> 
     </asp:DropDownList> &nbsp;of 
     <%=GridView1.PageCount%> 


    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" BorderStyle="Solid" GridLines="Both" HeaderStyle-BackColor="#990033" Width="1000px" 
    DataSourceID="EntityDataSource1" OnRowDataBound="GridView1_RowDataBound" OnDataBound="GridView1_DataBound"> 
     <HeaderStyle ForeColor="White"></HeaderStyle> 
    <Columns> 
     <asp:BoundField DataField="intBatchID" HeaderText="Batch ID" ReadOnly="True" 
      SortExpression="intBatchID" /> 
     <asp:BoundField DataField="vcharName" HeaderText="Name" ReadOnly="True" 
      SortExpression="vcharName" /> 
     <asp:BoundField DataField="dtmScheduled" HeaderText="Date Scheduled" 
      ReadOnly="True" SortExpression="dtmScheduled" /> 
     <asp:BoundField DataField="intBatchPriorityLevel" 
      HeaderText="Priority Level" ReadOnly="True" 
      SortExpression="intBatchPriorityLevel" /> 
    </Columns> 
    <PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" PageButtonCount="4" PreviousPageText="Previous" NextPageText="Next" FirstPageText="First" LastPageText="Last" /> 
    <PagerStyle HorizontalAlign="Center" />   
</asp:GridView> 

這裏的背後

protected void GridView1_DataBound(object sender, EventArgs e) 
    { 
     for (int cnt = 0; cnt < GridView1.PageCount; cnt++) 
     { 
      int curr = cnt + 1; 

      ListItem item = new ListItem(curr.ToString()); 

      if (cnt == GridView1.PageIndex) 
      { 
       item.Selected = true; 
      } 

      ddlPageNumber.Items.Add(item); 

     } 
    } 

    protected void ddlPaging_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     GridView1.PageIndex = ddlPageNumber.SelectedIndex; 
    } 

回答

2

我的代碼在更新PageIndex你需要重新綁定網格,如下所示:

protected void ddlPaging_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    GridView1.PageIndex = ddlPageNumber.SelectedIndex; 
    GridView1.DataBind(); 
} 

UPDATE:

因爲你是動態生成下拉的頁碼列表項,那麼你需要時回發到服務器發生或當網格是反彈,這樣的重新構建它們:

private void BuildPageNumbers() 
{ 
    ddlPageNumber.Items.Clear(); 

    for (int cnt = 0; cnt < GridView1.PageCount; cnt++) 
    { 
     int curr = cnt + 1; 

     ListItem item = new ListItem(curr.ToString()); 

     if (cnt == GridView1.PageIndex) 
     { 
      item.Selected = true; 
     } 

     ddlPageNumber.Items.Add(item); 

    } 
} 

現在,在您DataBoundPage_Load事件,你可以調用這個方法,像這樣:

protected void Page_Load(object sender, EventArgs e) 
{ 
    BuildPageNumbers(); 
} 

protected void GridView1_DataBound(object sender, EventArgs e) 
{ 
    BuildPageNumbers(); 
} 
+0

我嘗試了數據綁定,它仍然沒有工作,但我意識到,NEIT她是我的pagesize下拉菜單,分頁工作也不在GridView內部。我收到錯誤消息無法在DropDownList中選擇多個項目。當我點擊數字分頁。任何想法? – hollyquinn

+0

@hollyquinn - 你是否重新綁定每個「Page_Load」上的網格? –

+0

卡爾不,我不是。我可以做? – hollyquinn