I have the code below and am trying to access the ‘text’ string in the lnkSave_Click function, but text doesn’t seem accesible from lnkSave_Click function, it always seems empty.
private string _text = "";
public string text
{
get { return _text; }
set { _text = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
text = "Hello World!";
}
}
protected void lnkSave_Click(object sender, EventArgs e)
{
if (text == "Hello World!")
{
... do things..
}
}
When it’s empty it is accessible. The reason why it’s always empty is that it’s a field. Every object in an ASP.NET page is disposed at the end of the life-cycle. So it’ll be initialized with
""on every postback.You could use the ViewState to persist the value:
Nine Options for Managing Persistent User State in Your ASP.NET Application