Rhs
Rhs

Reputation: 3318

Preserving DropDownList settings

I have a webpage: Menu.aspx, where I have the following controls of relevance:

In my Edit.aspx page, I have a cancel button, which redirects the user back to the Menu page with the following C# method

protected void btnCancel_click(object sender, EventArgs e)
{
    Response.Redirect("Menu.aspx");
}

What I would like to do is when I redirect my user back to Menu.aspx, I preserve the value of the DropDownList that they selected.

Thanks in advance!

EDIT: Ideally, I want to achieve this without a QueryString (if Possible)

EDIT2: I was thinking of something invovling a viewstate or postback but I'm unfamiliar with those.

Upvotes: 1

Views: 144

Answers (2)

codingbiz
codingbiz

Reputation: 26386

When you called Response.Redirect("Menu.aspx"); you were already making a new request which the previous page was not part. It's like calling the page anew.

You can put the value in session before coming to Edit.aspx or pass the value back through query string

 Response.Redirect("Menu.aspx?dropdownvalue=123");

On Menu.aspx, you'll check if ?dropdownvalue=123 exist and you set selected item to that value

Upvotes: 1

Marlon Vidal
Marlon Vidal

Reputation: 669

Isn't the most ellegant solution, but you can create a session variable containing the selected value.

For example:

dropDown_SelectedIndex(object sender, EventArgs e)
{
   Session["SelectedItem"] = dropDown.SelectedValue;
}

When you do the redirect, on the page load of the Menu.aspx, after you populate the DropDown, check if this session variable exists, then select the item and remove it from the application.

void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
         //populate drop down here

         if(Session["SelectedItem"] != null)
         {
              dropDown.SelectedValue = Session["SelectedItem"].ToString();
              Session["SelectedItem"] = null;
         }
   }
}

Or you can pass it via query string, but this way you preserve you url

Upvotes: 1

Related Questions