wootscootinboogie
wootscootinboogie

Reputation: 8695

PostBack and ViewState

I've seen some code that seems idiomatic when working with ViewState variables like

protected void Page_Load(object sender, EventArgs e)
    {
        //find if this is the initial get request
        //after you click a button, this code will not run again
        if (!IsPostBack)
        { 
            if(ViewState["clicks"] ==null)
            {
                ViewState["clicks"] = 0;
            }
            //we're using the ViewState[clicks] to initialize the text in the text box
            TextBox1.Text = ViewState["clicks"].ToString();
        }
    }

Can someone point out a situation in which we absolutely need to check if(ViewState["clicks"] == null) or the program will not run? I tried adding another button, clicking the new button first, and then clicking Button1 and the program still ran fine, even though after the Button 2 click it was a postback, the program still functioned the same after I clicked button 1 multiple times.

Upvotes: 0

Views: 1098

Answers (2)

Caius Jard
Caius Jard

Reputation: 74605

Can someone point out a situation in which we absolutely need to check if(ViewState["clicks"] == null) or the program will not run?

Sure:

    protected void Page_Load(object sender, EventArgs e)
    {
        //find if this is the initial get request
        //after you click a button, this code will not run again
        if (!IsPostBack)
        {
            //if (ViewState["clicks"] == null)
            //{
            //    ViewState["clicks"] = 0;
            //}
            //we're using the ViewState[clicks] to initialize the text in the text box
            TextBox1.Text = ViewState["clicks"].ToString();
        }
    }

That will break because you're attempting to call a method on something you need to be not-null, but on first page load, it will be null. If you're asking why we test it for null first, before we assign it, then you should know that the if-null test is not for the benefit of the assignment, it is for the benefit of the line that sets the textbox text. With that IF block present in the code we can guarantee that by the time we come to use ViewState["clicks"].ToString() we won't try to call ToString() on a null (because ViewState["clicks"] has either been set elsewhere or it has been defaulted by this IF bock)

after the Button 2 click it was a postback, the program still functioned the same after I clicked button 1 multiple times

But.. when it's a postback, this entire code block won't run at all.. ViewState will never be used in the PageLoad if its a postback

Upvotes: 0

CSharpConductor
CSharpConductor

Reputation: 506

Because ViewState is a Dictionary Object (StateBag) No exception is thrown if you attempt to get a value out of view state that does not exist. To be sure that the value you want is in view state you would do what you are asking about.

Also, if you are developing a control or shared component that will be used on page that has ViewState disabled ViewState will be null for ViewState values.

Bits of this were taken from: http://msdn.microsoft.com/en-us/library/ms228048%28v=vs.85%29.aspx

Upvotes: 1

Related Questions