Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PostBack reloads Page.LoadControl Control

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Hello
I am trying to take advantage of adding user controls on the fly to save duplicate code.

I have three controls that are added to the page in the Page_Init function. IT looks like this
Code:
 protected void Page_Init()
    {
        if (Request.QueryString.HasKeys())
        {
            gstNotifyType = Convert.ToString(Request.QueryString["NotifyType"]);
        }
        if (gstNotifyType != String.Empty)
        {
            /* load titelDesc control */
            gUcTitleDesc = (uCtrlTitleDesc)this.LoadControl("~/Controls/global/TitleDesc.ascx");
            gUcTitleDesc.ID = "gUcTitleDesc";

            /* load notification control */
            Label lblSelectOptions = new Label();
            lblSelectOptions.ID = "lblSelectOptions";
            lblSelectOptions.Text = "<h4>Select your options.</h4>";
            gUcNotify = (uCtrlNotify)this.LoadControl("~/Controls/notifications/" + gstNotifyType + ".ascx");
            gUcNotify.ID = "gUcNotify";


            /* load schedule control */
            gUcSchedule = (uCtrlScheudule)this.LoadControl("~/Controls/global/Schedule.ascx");
            gUcSchedule.ID = "gUcSchedule";


            phUserControl_Notifcation.Controls.Add(lblSelectOptions);
            phUserControl_TitleDesc.Controls.Add(gUcTitleDesc);
            phUserControl_Notifcation.Controls.Add(gUcNotify);
            phUserControl_Schedule.Controls.Add(gUcSchedule);

            this.LoadComplete += new EventHandler(wFrmNotificationManage_LoadComplete);
        }
        else
        {
            throw new Exception("No Notificaiton type was specified");
        }
    }

The gUcNotify allows users to add or remove options. When a user removes options and a post back is fired, when the page reloads,the origional state of the control is loaded.

Any way to avoid this? I can not add
Code:
if (!Page.IsPostBack)
before the meat of the function because then the controls will not get added at all and the page will look empty.

I am putting this code in Page_Init as opposed to Page_Load because MSDN says
MSDN said:
When you load a control into a container control, the container raises all of the added control's events until it has caught up to the current event. However, the added control does not catch up with postback data processing. For an added control to participate in postback data processing, including validation, the control must be added in the Init event rather than in the Load event.

Thanks,
RalphTrent
 
yes, by loading the control during Init and setting the control ID it will be incorporated into the page's state management and show up on additional requests.

Not sure how to solve it using webforms. This is one of the reasons I abandoned webforms in favor of MVC.

you may be able to create the html dom elements on the client (with a js library) and pull them from one of the Request collections (Params, Form, Querystring) depending on how they end up at the server. This would bypass the webforms page lifecycle altogether.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
I figured it out.

Here is the offending code.
Code:
this.LoadComplete += new EventHandler(wFrmNotificationManage_LoadComplete);

This code calls code to reset the fields just as I was describing. I do the following
Code:
if(!Page.IsPostBack
{
[tab]this.LoadComplete += new EventHandler(wFrmNotificationManage_LoadComplete);
}

Problem Solved
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top