Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a project that uses masterpage. There is three dropdownlists on masterpage that lists categories. When I select a categories, it redirects to another aspx page. But after page redirects, the dropdownlist's selected values becoming empty . I want it to keep its state and show the selected category as selectedvalue.
Posted

put this in your code

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
// call dropdownlist bind code here
BindDriopdownlist();
}
hope it will work
 
Share this answer
 
You can use querystrings for sending selected index of dropdown and when you redirect just look for the value in Querysting if you get the value just select the value of dropdown.

just a snippet for your reference:

C#
protected void btnSubmitProductFinder_Click(object sender, ImageClickEventArgs e)
        {
           
            var MakerId = drpVehicleMaker.SelectedItem.Value;
          

           
                var strUrl = "";
                strUrl = "productfinder.aspx?MakerId=" + MakerId ;
              
                Response.Redirect(strUrl);
            }
        }



and on your pageload:

C#
if (Request.QueryString["MakerId"] != null)
                       {

                           //drpVehicleMaker
                           try
                           {
                               int drpVehicleMakerId = int.Parse(Request.QueryString["MakerId"].ToString());   
  drpVehicleMaker.Items.FindByValue(drpVehicleMakerId.ToString()).Selected = true;


let me know if it works.
 
Share this answer
 
When you redirecting to new page it will load both master page and content page again. You need to pass the selected value of drop down list to next page by using techniques like sessions, query strings etc.. for example in your master page do as below, here session is used to store the value.
C#
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["YourDropdown"] != null)
            {
                dropdown.SelectedIndex = (int)Session["YourDropdown"];
            }
        }
    }

    protected void dropdown_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["YourDropdown"] = dropdown.SelectedIndex;
        Response.Redirect("to somewhere");
    }
}
 
Share this answer
 
v2

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