动态创建不同的会话变量(Dynamically Create Different Session Variables)

我想知道是否有可能根据变量动态创建具有不同名称的不同会话变量?

我试图以这种方式完成,但似乎不喜欢我的变量

protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e) { LinkButton lb = (LinkButton)e.Row.FindControl("MyLinkButton"); Label options = (Label)e.Row.FindControl("MyLabel"); if (e.Row.RowType == DataControlRowType.DataRoow) { Session[lb] = options; //I was trying to use this to create Session Variables with Different Names Dynamically } }

所以我的最终结果是,我将有以下会话,不必手动创建所有这些。

Session["Value"] = "MyOption" Session["Value1"] = "MyOption1" Session["Value2"] = "MyOption2" Session["Value3"] = "MyOption3"

I was wondering if it was possible to dynamically create different session variables with different names based on a variable?

I attempted to accomplish this way but does not seem to like my variable

protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e) { LinkButton lb = (LinkButton)e.Row.FindControl("MyLinkButton"); Label options = (Label)e.Row.FindControl("MyLabel"); if (e.Row.RowType == DataControlRowType.DataRoow) { Session[lb] = options; //I was trying to use this to create Session Variables with Different Names Dynamically } }

So my end result I would have the following sessions with out having to manually create all of them.

Session["Value"] = "MyOption" Session["Value1"] = "MyOption1" Session["Value2"] = "MyOption2" Session["Value3"] = "MyOption3"

最满意答案

您可以创建部分列表。 对于exaple:

List<string> sections=GetSectionsList();

列表中的每个项目都会创建一个Session变量并填充状态,如下所示:

foreach(string section in sections) { Session[section] = GetSectionStatus(section); }

然后每个部分的onload检查部分会话值并相应地采取行动。 但是有一条建议在这种情况下使用会话,只要这些部分的数量要少得多,比如2到5,否则每个部分加载的数据库调用会更好。

You can create a list of sections. For exaple:

List<string> sections=GetSectionsList();

The for each item in your list create a Session variable and populate with the status, something like this:

foreach(string section in sections) { Session[section] = GetSectionStatus(section); }

Then onload of each section check for the sections session value and act accordingly. But a piece of advice there use session for this situation only if the sections are considerably less in number say 2 to 5, else a db call on load of each section would be better.

动态创建不同的会话变量(Dynamically Create Different Session Variables)

我想知道是否有可能根据变量动态创建具有不同名称的不同会话变量?

我试图以这种方式完成,但似乎不喜欢我的变量

protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e) { LinkButton lb = (LinkButton)e.Row.FindControl("MyLinkButton"); Label options = (Label)e.Row.FindControl("MyLabel"); if (e.Row.RowType == DataControlRowType.DataRoow) { Session[lb] = options; //I was trying to use this to create Session Variables with Different Names Dynamically } }

所以我的最终结果是,我将有以下会话,不必手动创建所有这些。

Session["Value"] = "MyOption" Session["Value1"] = "MyOption1" Session["Value2"] = "MyOption2" Session["Value3"] = "MyOption3"

I was wondering if it was possible to dynamically create different session variables with different names based on a variable?

I attempted to accomplish this way but does not seem to like my variable

protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e) { LinkButton lb = (LinkButton)e.Row.FindControl("MyLinkButton"); Label options = (Label)e.Row.FindControl("MyLabel"); if (e.Row.RowType == DataControlRowType.DataRoow) { Session[lb] = options; //I was trying to use this to create Session Variables with Different Names Dynamically } }

So my end result I would have the following sessions with out having to manually create all of them.

Session["Value"] = "MyOption" Session["Value1"] = "MyOption1" Session["Value2"] = "MyOption2" Session["Value3"] = "MyOption3"

最满意答案

您可以创建部分列表。 对于exaple:

List<string> sections=GetSectionsList();

列表中的每个项目都会创建一个Session变量并填充状态,如下所示:

foreach(string section in sections) { Session[section] = GetSectionStatus(section); }

然后每个部分的onload检查部分会话值并相应地采取行动。 但是有一条建议在这种情况下使用会话,只要这些部分的数量要少得多,比如2到5,否则每个部分加载的数据库调用会更好。

You can create a list of sections. For exaple:

List<string> sections=GetSectionsList();

The for each item in your list create a Session variable and populate with the status, something like this:

foreach(string section in sections) { Session[section] = GetSectionStatus(section); }

Then onload of each section check for the sections session value and act accordingly. But a piece of advice there use session for this situation only if the sections are considerably less in number say 2 to 5, else a db call on load of each section would be better.