Link to home
Start Free TrialLog in
Avatar of sstoos
sstoos

asked on

On Demand Loading of a control

I currently have a asp.net page that has an Infragistics control on it.  The control's page load event fires before the parent page load event.  My problem is that the parent page needs to save data and then set a session variable which passes to the control, but due to the fact that the control loads first, the session variable never gets set.  Is there a way to not load the control and call it after the savedata method on the parent page?
Avatar of tusharashah
tusharashah

You can dynamically load the UserControl whenever you want... or after you sessions are initialize:


Add this line in your ASPX HTML
<%@ Reference Control = "UserControl1.ascx" %>


And then in code Add this after you initialize sessions

UserControl1 uc1 = (UserControl1) Page.LoadControl("UserControl1.ascx");
PlaceHolder1.Controls.Add(uc1);

-tushar
ASKER CERTIFIED SOLUTION
Avatar of tusharashah
tusharashah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sstoos

ASKER

Ok...I'm a little new on this concept. I'm using vb so I assume I declare the control in the declarations as control, right. Then what do you mean by Placeholder1?  Also, what is uc1 referencing?
You can declare it as the way I showed.

Then what do you mean by Placeholder1?
--> PlaceHolder1 is WebControl if you BrowsYour WebControl you'll see "PlaceHolder" in them.
--> Drag and drop PlaceHolder1 wherever you want to put your UserControl

UC1
--> UserControl1 is the Name of you Control and uc1 is Object of that control

But, I think the 2nd option will be lot easier to deal with if you can adapt that..

-tusahr
Avatar of sstoos

ASKER

So if I was to use the second option....sorry to be so dense, but what does "Response.redirect(request.RawURL) do?  And this code would reside in the ascx page load???
Response.Redirect  : Will Redirect you to URL that is passed as Argument
Request.RawUrl      : Will Give you current URL of page.

You need call Response.Redirect( Request.RawUrl ) after you have Set your Session variable.

"the parent page needs to save data and then set a session variable which passes to the control"
--> Set Session Variable and then use Response.Redirct to Re-load page

.. Page_Load(..)
{
   if( Session["YourSessionName"] == null )
   {
       SaveData();
       Session["YourSessionName"] = "SetSession";
       Response.Redirect( Request.RawUrl );
   }
}

ps. You should always check that Session Variable are not null before using them anywhere.

-tushar
Avatar of sstoos

ASKER

The only disadvantage that I can see with this, is that the control will still load first and am trying to get around that, if possible.
Alright in that case
- Put PlaceHolder/Panel wherever you want to show UserControl
- After you Set your session add following line:

Dim uc1 As UserControl1 = CType(Page.LoadControl("UserControl1.ascx"), UserControl1)
PlaceHolder1.Controls.Add(uc1)

[ Replace UserControl1 with name of your UserControl.
     i.e. if your UserControl is Header.ascx then replace "UserControl1" with "Header" ]

-tushar