views:

744

answers:

3

Is it recommended to check the Page.IsPostBack in a user control Page_Load Event like

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {


        }


    }

I am getting wierd results

Edit ~ Here is the thing. When the main form is loaded, I use Request.QueryString to get the customer id which I then place in a SESSION variable.

On the control Load event I read the SESSION variable to get the data for that customer. So, do I need to check PostBack at the control level?

Edit ~ Here is the load event of the control

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Getting and storing the customer account number 
            if (string.IsNullOrEmpty((string)Session["CustomerNumber"]))
            {
                Session["CustomerNumber"] = cust.GetCustomerNumber(myHelper.GetCustomerIDFromQueryString());
                LoadProductData();
            }
        }

    }

Here is the myHelper Class

static class myHelper
{
    public static Guid GetCustomerIDFromQueryString()
    {
        //Getting the GUID (used as customerid in CRM) from the URL request of the selected account.  
        return Sql.ToGuid(System.Web.HttpContext.Current.Request["ID"]);
    }

}

}

A: 

Just checking it for no reason? Absolutely not. If you should do something only on first load and not on subsequent post backs then it's the pattern that should be used.

Mehrdad Afshari
A: 

Are you sure that you will always have a "CustomerNumber" already stored in the Session by the time you get to your page? Is there any rhyme or reason that you can find as to when you get data and when you don't?

AgileJon
Sometimes I get data and sometimes not. If I remove the PostBack everything works fine. I edited my poost with exactly what I am doing.
Saif Khan
+1  A: 

If you use "!IsPostBack" in page load, when the user click other control it do a postBack, so you don't get your data. I hope that helps you.

RG