>>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:
>
>
>// Querystring
>if (Request.QueryString["SomeVariable"] != null)
>{
> // Save the querystring variable into the session
> Session["SomeVariable"] = Request.QueryString["SomeVariable"];
>}
>
>// Post
>if (Request.Form["SomeVariable"] != null)
>{
> // Save the posted variable into the session
> Session["SomeVariable"] = Request.Form["SomeVariable"];
>}
>
I used what you suggested to initialize the value to the Session variable and it works great. However, I wanted the form to go to another form upon updating the value so I put a Server.Transfer command into the TextBox_Changed event. It switches just fine but when I put in code to save the Session variable just before the transfer, it doesn't get saved...ie...
private void TextBox_TextChanged(...)
{
Session["helloname"] = TextBox.Text;
Server.Transfer("Form2.aspx");
}
When I step through the code, apparently the Text value hasn't been updated yet, because it's still empty. I know I could do it by adding another object like an OK button and assign the variable there, but is there a way to do it without adding another object?
Larry