If multi-select is not your question, storing it by each variable will not scale well. If you want to store each selection into the Session, you can store a List of strings into the session. (taking advantage of the fact that the items are stored in the order we insert them in a list)
protected void DropDownListAddNumber_SelectedIndexChanged(object sender, EventArgs e)
{
List<string> sessionList = GetSessionVariable();
sessionList.Add(DropDownListAddNumber.Items.FindByValue
(DropDownListAddNumber.SelectedValue));
}
// you can access the selected values from the list
// if sessionList.Any(), then sessionList.Last() will contain
// the last selected value.. etc.
private List<string> GetSessionVariable()
{
var list = Session["SelectionValuesList"] as List<string>;
if (list == null)
{
list = new List<string>();
Session["SelectionValuesList"] = list;
}
return list;
}
OR
if you still want to use multiple session variables, you need to do some sort of counter. multiple variables will be created. this is error prone.
int lastCounter = 0;
protected void DropDownListAddNumber_SelectedIndexChanged(object sender, EventArgs e)
{
++lastCounter;
Session["selection" + lastCounter] = DropDownListAddNumber.Items
.FindByValue(DropDownListAddNumber.SelectedValue);
}
// you can access last value as
// var lastSelectedValue = Session["selection" + lastCounter];