Link to dropdown options
Is it possible to reference a specific value in the options dropdown from another page?
In other words, let's say I'm on page 1 and I want to bind a link to page 2 that has a dropdown list of options with three different values ββin it. Suppose, by default, when you go to page 2, the value 1 appears in the dropdown.
Is it possible to link the link to page 2 and change the value of that checkbox box? If you click the link on page 1, it will automatically display the value 3 instead of 1 on page 2.
a source to share
This is certainly possible. You can pass a flag in your request. So on page 1, you have a link to page2, for example "page2.aspx? Option = 3". Then, in the page2 method, PageLoad
just read that value from the query string ( Request.QueryString["option"]
) and set the selected item accordingly DropDownList
.
On one page1 you would ...
<a href="page2.aspx?option=3">link to page 2</a>
In the code of the page 2 code, based on the example Al ...
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;
}
}
}
a source to share
John Freeland's answer is basically how I would do it. You probably want to install the code to set the index of the list in the codebehind class inside the Page_Load function.
You can also keep the value of the parameter set in the ASP.Net session, but it gets a little trickier if you start allowing the user to opt out of the site. They might go back to page 2 and still have a session variable, something unexpected. Also, you may have problems deleting the session if the user is inactive for a while or the server is reset. On the plus side, if you put it in a Session object, you can navigate between pages and store whatever data you need.
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 in a section! IsPostBack so that it only runs the first time the user accesses the page.
a source to share