I have two dropdownlist inside a gridview.
1st Dropdown -- For Selecting the
Page No
.2nd Dropdown -- For Selecting the
Page Size
.
The Issue related to the dropdownlist is:-
When I change
the Selection it works fine and shows me the exact result. But When I bring the selection to the last value again my gridview
breaks. Tried with all the code and debugged also but couldn't find the exact solution.
Please see the aspx code for the dropdownlist
inside the Gridview
:-
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:GridView ID="grdUser"
OnPageIndexChanging="grdUser_PageIndexChanging"
AutoGenerateColumns="false" EnableViewState="true"
AllowPaging="true"
OnDataBound="grdUser_DataBound"
runat="server"
Width="100%"
border="1"
DataKeyNames="Id"
PageSize="2">
<Columns>
<asp:TemplateField HeaderText="Select" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="username" HeaderText="UserName" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
<asp:BoundField DataField="email" HeaderText="Email Id" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
<asp:BoundField DataField="usertype" HeaderText="UserType" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
<asp:BoundField DataField="active" HeaderText="Active" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="15%" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
<ItemTemplate>
<asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Image" ItemStyle-Width="15" EditImageUrl="~/images/edit.png" ShowEditButton="True" ControlStyle-Width="15" ControlStyle-Height="15" CancelImageUrl="~/images/close.png" UpdateImageUrl="~/images/update.png">
<ControlStyle Height="20px" Width="20px"></ControlStyle>
</asp:CommandField>
</Columns>
<PagerStyle ForeColor="Blue"
BackColor="LightBlue" />
<PagerTemplate>
<table style="width: 100%">
<tr>
<td class="col-md-7">
<asp:Label ID="MessageLabel"
ForeColor="Blue"
Text="Select a page:"
runat="server" />
<asp:LinkButton ID="FirstLB" runat="server" CommandName="Page" CommandArgument="First" ToolTip="First"><<</asp:LinkButton>
<asp:LinkButton ID="PrevLB" runat="server" CommandName="Page" CommandArgument="Prev" ToolTip="Previous"><</asp:LinkButton>
<asp:DropDownList ID="PageDropDownList"
AutoPostBack="true"
OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged"
runat="server" />
<asp:LinkButton ID="NextLB" runat="server" CommandName="Page" CommandArgument="Next" ToolTip="Next">></asp:LinkButton>
<asp:LinkButton ID="LastLB" runat="server" CommandName="Page" CommandArgument="Last" ToolTip="Last">>></asp:LinkButton>
</td>
<td class="col-md-3">
<asp:Label ID="PageSizeLabel" runat="server" Text="Select Page Size: "></asp:Label>
<asp:DropDownList ID="ddlPageSize" runat="server" OnSelectedIndexChanged="ddlPageSize_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="2" Text="2" />
<asp:ListItem Value="5" Text="5" />
<asp:ListItem Value="10" Text="10" />
</asp:DropDownList>
</td>
<td class="col-md-2">
<asp:Label ID="CurrentPageLabel"
ForeColor="Blue"
runat="server" />
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Note: The Gridview is under the UpdatePanel
.
Also see the Code behind for the each event and the BindGrid();
protected void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString;
SqlCommand cmd = new SqlCommand("select Id,username,email,usertype,active from tbl_User ORDER By Id DESC");
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
grdUser.DataSource = dt;
grdUser.DataBind();
DisablePageDirections();
// System.Threading.Thread.Sleep(3000);
//return dt;
}
}
}
}
private void DisablePageDirections()
{
if (grdUser.PageIndex == 0)
{
(grdUser.BottomPagerRow.FindControl("FirstLB") as LinkButton).Enabled = false;
(grdUser.BottomPagerRow.FindControl("PrevLB") as LinkButton).Enabled = false;
}
if (grdUser.PageIndex == grdUser.PageCount - 1)
{
(grdUser.BottomPagerRow.FindControl("NextLB") as LinkButton).Enabled = false;
(grdUser.BottomPagerRow.FindControl("LastLB") as LinkButton).Enabled = false;
}
}
protected void ddlPageSize_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow pagerRow = grdUser.BottomPagerRow;
DropDownList pageSizeList = (DropDownList)pagerRow.Cells[0].FindControl("ddlPageSize");
grdUser.PageSize = Convert.ToInt32(pageSizeList.SelectedValue);
Context.Session["PageSize"] = pageSizeList.SelectedValue;
BindGrid();
}
protected void PageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow pagerRow = grdUser.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
grdUser.PageIndex = pageList.SelectedIndex;
BindGrid();
}
Also see the gridview databound Event:-
protected void grdUser_DataBound(object sender, EventArgs e)
{
GridViewRow pagerRow = grdUser.BottomPagerRow;
DropDownList pageSizeList = (DropDownList)pagerRow.Cells[0].FindControl("ddlPageSize");
if (Context.Session["PageSize"] != null)
{
pageSizeList.SelectedValue = Context.Session["PageSize"].ToString();
}
// Retrieve the DropDownList and Label controls from the row.
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
if (pageList != null)
{
for (int i = 0; i < grdUser.PageCount; i++)
{
int pageNumber = i + 1;
ListItem item = new ListItem(pageNumber.ToString());
if (i == grdUser.PageIndex)
{
item.Selected = true;
}
pageList.Items.Add(item);
}
}
if (pageLabel != null)
{
int currentPage = grdUser.PageIndex + 1;
pageLabel.Text = "Page " + currentPage.ToString() + " of " + grdUser.PageCount.ToString();
}
}
Also, there is no problem in the ddlPageSize
and it is working fine