What's the difference in behavior between adding a control to an ASPX page directly, loading software, and adding it to a placeholder?

Is there a difference in behavior between adding a control to an ASPX page directly and software loading the control and adding it to a placeholder?

The control inherits from System.Web.UI.WebControls.DataBoundControl

.

I ask that I have a control that works when I add it to an ASPX page, for example:

...
<blah:GoogleMap ID="GoogleMap1" runat="server" Width="640px" Height="600px" ... DataSourceID="_odsMarkers" DataAddressField="Address" DataTextField="Description">
</blah:GoogleMap>
...

      

But not when I use the following in the codebehind page:

GoogleMap map = (GoogleMap)this.LoadControl(typeof(GoogleMap), new object[] { });
//... set properties
this.placeholder1.Controls.Add(map); //add to placeholder

      

Anyone have any ideas why this might be the case?

+1


source to share


6 answers


The control tree ends up being the same if you define in markup or add programmatically. However, there are plenty of options for the executive performer to hang on the road.

You can see how ASP.NET compiles aspx:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files

      



A temporary situation where a control is added to the page can be a problem. The regular template adds a control to the overload of the CreateChildControls method. If the control needs to allow browsing, you need to make sure it is called during init, eg. by calling EnsureChildControls.

Adding to debbugging ninja tooltip. It doesn't matter if you add the shortcut in the same way. Does he appear?

+3


source


Is this a user or server control?

If it's a custom control, they should be loaded by their path, not by type:

GoogleMap map = (GoogleMap)this.LoadControl("~/Controls/GoogleMap.ascx");

      



If it manages the server, you can simply create a new instance:

GoogleMap map = new GoogleMap();

      

after you have an instance and add it to the control tree (by inserting it into the PlaceHolder), it should accomplish the same as in the case of markup.

+2


source


If you are setting properties outside of the LoadControl call, why are you creating a new empty array of objects instead of just using the one parameter overload?

Also, if you attach a debugger to it and follow the steps, will you notice anything strange about the control before you make the call to Controls.Add ()? Is there an exception? if yes, which one? if not, what does the markup look like in the browser, where is the placeholder?

+1


source


"Works" is somewhat ambiguous, but if you mean the event handlers never get executed, you need to load it into the onload event on the page.

+1


source


If a control requires the use of a viewstate, you must ensure that it is added to the page BEFORE the Page_Load event, otherwise the viewstate will not be populated and most likely events and other elements will not work as expected.

+1


source


An important difference is that if you create the control dynamically, by default you won't get any values ​​from the skins. You have to manually call control.ApplyStyleSheetSkin (page): http://msdn.microsoft.com/en-us/library/system.web.ui.control.applystylesheetskin.aspx

0


source







All Articles