>Can someone show me an example of how to do a GET & SET for a web form textbox control? I want to initialize the textbox value with a Session variable that needs to persist. I've added a Session variable to the web.config file but cannot get it to work.
>
>Thanks,
>Larry Long
It's not really necessary to add that to the web.config. Code like this should work (from within your Page_Init() of your .aspx.cs file), assuming you have a lable control named "lblTest" on the form.
if (Session["Variable"] == null)
{
Session.Add("Variable", "Hello");
}
else
{
this.lblTest.Text = Session["Variable"].ToString();
}
(although I'm showing a label, a textbox works exactly the same way).
This is the "Set" portion of it. To "Get", depending on how you are posting variables to/from the pages (through querystring values or via POST), you retrieve the variables using the Request object:
if (Request.QueryString["SomeVariable"] != null)
{
Session["SomeVariable"] = Request.QueryString["SomeVariable"];
}
if (Request.Form["SomeVariable"] != null)
{
Session["SomeVariable"] = Request.Form["SomeVariable"];
}