开发者

How can I add form data from ASP.NET wizard into a session array object?

I'm trying to add form data from an asp.net wizard into a session array object. The object should store the question number and question answer selected by the user for each question. I'm trying to use a multidimensional array to store the question number and the associated answer. I would also be op开发者_JAVA技巧en to using a hashtable or dictionary solution.

This is the code I've got so far:

string[,] strQandA = new string[10, 2] {{"1", Q1.SelectedValue}, {"2", Q2.SelectedValue}, {"3", Q3.SelectedValue}, {"4", Q4.SelectedValue}, {"5", Q5.SelectedValue}, {"6", Q6.SelectedValue}, {"7", Q7.SelectedValue}, {"8", Q8.SelectedValue}, {"9", Q9.SelectedValue}, {"10", Q10.SelectedValue}};
Session["mySession"] = strQandA;

Is this correct? Any help would be appreciated. Thanks


Better practice for your needs is Dictionary with the question number as key and selected value as the value. Something like:

DropDownList[] arrDDL = new DropDownList[] { Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10 };
Dictionary<int, string> strQandA = new Dictionary<int, string>();
for (int i = 0; i < arrDDL.Length; i++)
   strQandA.Add(i + 1, arrDDL[i].SelectedValue);
Session["mySession"] = strQandA;

To access specific value later e.g. the third question answer:

(Session["mySession"] as Dictionary<int, string>)[3]


You need to cast the collection, Response.Write calls ToString() on the object and will return the default implementation.

 Dictionary<string, YourType> strQandA = new Dictionary<string, YourType>();
Session["mySession"] = strQandA;

Dictionary<string, YourType> mySession = (Dictionary<string, YourType>)Session["mySession"];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜