Reputation: 652
Hi I want to have a DropDownList outside of my GridView that displays a list of page numbers. When the user clicks on a page number, the GridView should go to that page. I am able to populate the DropDownList, but it's not working with the GridView
Here is my GridView and DropDownList
<asp:DropDownList ID="ddlPageNumber" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPaging_SelectedIndexChanged">
</asp:DropDownList> 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>
Here's my code behind
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;
}
Upvotes: 1
Views: 2101
Reputation: 34846
After you update the PageIndex
you need to rebind the grid, like this:
protected void ddlPaging_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.PageIndex = ddlPageNumber.SelectedIndex;
GridView1.DataBind();
}
UPDATE:
Since you are dynamically building the drop down list items for the page numbers, then you need to re-build them whenever a post back to the server happens or whenever the grid is rebound, like this:
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);
}
}
Now in your DataBound
and Page_Load
events, you can call this method, like this:
protected void Page_Load(object sender, EventArgs e)
{
BuildPageNumbers();
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
BuildPageNumbers();
}
Upvotes: 3