Click here to Skip to main content
15,848,157 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Custom paging in Gridview. I have created a Control in which there is a dropdown to selece page size. Everything works fine but when i am refreshing page the dropdown lost it's value
here is my code
ASP.NET
<asp:DropDownList ID="ddlPageRowSize" EnableViewState="True" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPageRowSize_SelectedIndexChanged">
                <asp:ListItem Value="10">10</asp:ListItem>
                <asp:ListItem Value="20">20</asp:ListItem>
                <asp:ListItem Value="50">50</asp:ListItem>
                <asp:ListItem Value="100">100</asp:ListItem>
            </asp:DropDownList>

on page load i am saving dropdown selected value in viewstate here is my code
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        ViewState["PageSize"] = ddlPageRowSize.SelectedItem.Value;
    }
// suppose i have selected 100 from dropdown and then refresh the page 
// ddlPageRowSize.SelectedItem.Value always returns 10 

what wrong i am doing can anyone suggest....
Posted

your page load event should be soemthing like this
C#
protected void Page_Load(object sender, EventArgs e)
    {

  //  if (!Page.IsPostBack)
  //  {
     //do something
   // }
   // else
   // {
     //   ddlPageRowSize.SelectedIndex= Convert.ToInt32(Session["index"]);

         if(Session["index"] == null)
         {
             //do something
         }
         else
         {
             ddlPageRowSize.SelectedIndex= Convert.ToInt32(Session["index"]);  
         }


    }

where you set the value to the session in the ddlPageRowSize_SelectedIndexChanged event like
C#
protected void ddlPageRowSize_SelectedIndexChanged(object sender, EventArgs e)
{
    Session["index"] = ddlPageRowSize.SelectedIndex;
}
 
Share this answer
 
v2
Comments
Vishal Pand3y 9-Jan-14 1:16am    
when i refresh the page (!Page.IsPostBack) always returns true so each time i am refreshing the page ddlPageRowSize.SelectedValue returns 10
Naz_Firdouse 9-Jan-14 1:24am    
please check ,y updated answer
Selected="True" to your list item will solve your problem.

ASP.NET
<asp:DropDownList ID="ddlPageRowSize" EnableViewState="True" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPageRowSize_SelectedIndexChanged">
                <asp:ListItem Selected="True" Value="10">10</asp:ListItem>
                <asp:ListItem Value="20">20</asp:ListItem>
                <asp:ListItem Value="50">50</asp:ListItem>
                <asp:ListItem Value="100">100</asp:ListItem>
            </asp:DropDownList>


please use the following code.
C#
protected void Page_Load(object sender, EventArgs e)
{

    if (Session["PageSize"] != null)
        ddlPageRowSize.SelectedValue = Convert.ToString(Session["PageSize"]);

}

protected void ddlPageRowSize_SelectedIndexChanged(object sender, EventArgs e)
{
    Session["PageSize"] = ddlPageRowSize.SelectedValue;
}
 
Share this answer
 
v5
Comments
Vishal Pand3y 8-Jan-14 2:56am    
but when i will refresh the page, it will return always 100 whether i have selected 50 from dropdown
Sibasisjena 8-Jan-14 3:17am    
Check my solution 2
Sibasisjena 8-Jan-14 3:14am    
Ok do one thing in the ddlPageRowSize_SelectedIndexChanged event save the selected value in view state. Assign the saved value in the page load again.
Vishal Pand3y 8-Jan-14 3:53am    
No its not working after refreshing page i am getting 10 always
Sibasisjena 8-Jan-14 5:02am    
I have changed my solution please check it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900