Page_Load and IsPostBack

Jan 18, 2010 at 9:52 AM

Hi,

I'm adding this great controls in some project. I usually use IsPostBack property in page load to check if is the fist time that I load the control (and so I use IsPostBack ti initialize the control)

Now, with the PartialUpdatePanel the IsPostBack property is always true, beacuse the PartialUpdatePanel load the user control with a postback (I suppose).
There is an alternative check for test if is the first postback?

Thanks a lot

ClaudioCas

Coordinator
Jan 19, 2010 at 12:29 PM

Hi Claudio,

I also faced the problem. My workaround was to declare a ViewState-Property in the UserControl loaded by PartialUpdatePanel and set it to true at the first PreRender. So next time the control is loaded by a PostBack, the Property is true as an indicator, the control was loaded the second time.

Example:

public class PostBackUserControl : UserControl
    {
        protected new bool IsPostBack
        {
            get { return (bool)(ViewState["IsPostBack"] ?? false); }
            set { ViewState["IsPostBack"] = true; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            IsPostBack = true;
        }
    }

With this way you can check the now new declared property IsPostBack in your OnLoad code. It simulates the behavior of ASP.NET IsPostBack.

I hope this helps,

Stefan

 

Jan 20, 2010 at 1:44 PM

Thanks a lot for your reply, I will test soon as possible

Claudio