2

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..
            }
        }

1 Answer 1

6

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:

public string text
{
    get { if(ViewState["text"]==null)ViewState["text"]=""; return (String)ViewState["text"]; }
    set { ViewState["text"] = value; }
}

Nine Options for Managing Persistent User State in Your ASP.NET Application

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.