views:

89

answers:

2

I don't know if this is possible so bare with me on this one. Can you link to a specific value in an options drop down box from another page? In other words, let's say that I'm on page 1 and I want to anchor link to page 2 that has an options drop down box with 3 different values in it. Let say by default when you go to page 2, the drop down option box is showing value 1. Is it possible to link to page 2 and change the value of that option box on the fly? Whereas when you click the link on page 1 it will automatically show value 3 instead of 1 on page 2.

+1  A: 

This is certainly possible. You can pass a flag in your querystring. So, on page1 you have a link to page2 like "page2.aspx?option=3". Then, in page2's PageLoad method, simply read that value from the querystring (Request.QueryString["option"]) and set the selected item of the DropDownList appropriately.

One page1 you would have...

<a href="page2.aspx?option=3">link to page 2</a>

In the codebehind of page2, based on Al's example...

void Page_Load(object sender, EventArgs e) {
   if (!Page.IsPostBack) {
      int option;
      if(int.TryParse(Request.QueryString["option"], out option) { //Only set the value if it is actually an integer
         ddlList.SelectedIndex = option;
      }
   }
}
Jon Freeland
Thanks Jon for your answer. I'm kind of new in asp.net. Can you give me a code sample for what you just described?
arcadian
A: 

Jon Freeland's answer is basically the way I would do it. You probably want to put the code to set the list index in the codebehind class inside the Page_Load function.

You could also save the value of the option to set in the ASP.Net session, but that gets a little trickier if you start letting the user bounce around the site. They may come back to page 2 and still have the session variable set to something unexpected. Also, you may run into trouble with the Session getting deleted if the user is inactive for a while or if the server is reset. On the plus side, if you put it in the Session object, you can move back and forth between your pages and keep all the data you need right handy.

If you want to see a sample, try something like:


void Page_Load (object sender, EventArgs e) {
   if (! Page.IsPostBack) {
     ddlList.SelectedIndex = Request.QueryString["option"]
   }

You want to put the code inside the !IsPostBack section so that it will only run the first time the user is directed to the page.

Al Crowley