I usually use a Session-backed property when working with a session object
public string MyVariable
{
get
{
object temp = Session["MyVariable"];
return temp == null ? string.Empty : temp as string;
}
set
{
Session["MyVariable"] = value;
}
}
Now I don't have to remember what the Session variable name is and I have intellisense support for it. If at some point the Session variable is null then it will return a default value, which in this case is an empty string. The default can be changed depending on what the actual data type is you're working with. Here's a quick example of how you would use this in your code.
public string MyVariable
{
get
{
object temp = Session["MyVariable"];
return temp == null ? string.Empty : temp as string;
}
set
{
Session["MyVariable"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
MyVariable = "This is some text.";
TextBox1.Text = MyVariable;
}