开发者

How can I set dropdownlist default values based on variables?

I have several pages linked together that I want to maintain drop down selections for. I've seen a number of ways to do this with session values, etc, but I want to set the default value while still having the option of selecting other values.

Effect开发者_JAVA技巧ively, users will make selections on page 1 and then on page 2 they will use those same selections, but also potentially want to change their selections. My thought was that I could load the selections into a table in SQL in page 1 and then in page 2 call that table and set the values as variables. My question, then, is how I would set the default dropdown value to be a variable as opposed to a set value.

I am using VisualStudio2010 with ASP Webforms using C#.

Any help or suggestions would be much appreciated!


I certainly wouldn't use a table for that. Your first guess to use sessions is probably right on. It seems like you have the impression that you can't change the session values, however...

Your pages should work exactly like you like:

Have the user select items on page 1, then store those values in the session. Then on page two, set the values from the session as the default value in the drop down list. If they change their selection, simply update the session variable to the new value. Then you could repeat ad infinitum.

EDIT: Examples,

Setting a session variable:

Session["DropDownValue1"] = DropDownList1.SelectedIndex;

Setting the default valuie of a DropDown:

DropDownList1.SelectedIndex = (int?)Session["DropDownValue1"] ?? 0;

The ?? in the second example is a null coalescing operator. It means that if the object before it is null, then instead use the thing that is after it.

You will use both of those examples in the code-behind file *.cs

Then you can have a OnSelectedIndexChanged event to update your session variable on the second page, once the user changes it, if you like.

Add this attribute to your DropDownList in the *.aspx file: OnSelectedIndexChanged="Index_Changed"

With event handler code in your code-behind *.cs file:

  void Index_Changed(Object sender, EventArgs e) {

   Session["DropDownValue1"] = DropDownList1.SelectedIndex;
}


You can store it the Session if you want to to just persist for the term of the Session. If you want it to last longer, you could use cookies or you will need to store it in a database.

When you first load your DropDownList just check to see if you should default the value by looking for a stored default:

 // Initialize the DDL then check for the default value
 if (Session["DDL1Default"] != null)
 {
     ddl1.SelectedValue = Session["DDL1Default"].ToString();
 }

When the user selects a value and then navigates you should store it:

 // in some navigate event
 Session["DDL1Default"] =  ddl1.SelectedValue;


A database would work, but you could also use cookies or the Session. What you would need to do is on the drop down list selectionchanged event, save those values into the Session (for example). Then when the second page's page_load event fires, read those Session values in.

For a drop down list, you can set the selected value from either an object or a constant/value.

string myVal = Session["listChoice"];
lstChoices.SelectedValue = myVal;


You can still use Session variables.
In the Page_Load, set the drop down list's SelectedValue:

myDropDownList.SelectedValue = Session["myDropDownValue"].ToString();


Edit
Of course. Just update the variable in the myDropDownList_OnSelectedChange() event handler. You will need to make sure to put the other code, where you are setting the value from page 1 in the Page_Load, in an if(!IsPostBack) to prevent the dropdownlist from not updating.


Thanks, everyone! It is now working. In case anyone else runs into this, here's what I ended up with:

Page 1 and Page 2 CodeBehind in Page_Load section:

       if (!Page.IsPostBack) { 
          if (Session["PAG"] != null) { 
              DdlPAG.SelectedValue = Session["PAG"].ToString(); } }
       Session["PAG"] = DdlPAG.SelectedValue;

Thanks again!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜