简体   繁体   中英

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

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

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

The reason I ask is that I have a control that works when I add it to the ASPX page like so:

...
<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 a 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?

The control tree ends up the same if you define in markup or add programmatically. However there is plenty of room for the control implementor to screw up along the way.

You can go look how ASP.NET compiles the aspx:

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

The timing when the control is added to the page might be an issue. The usual pattern is add the control in an overload of the CreateChildControls method. If the control needs to resolve viewstate you need to make sure this is called during init, eg by calling EnsureChildControls.

Adding to ninja's debbugging hint. Does it make any difference if you add a label the same way. Does it show up?

Is this a user control or server control?

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

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

If it's server control then you can just new up an instance:

GoogleMap map = new GoogleMap();

after you have the instance and add it to the control tree (by inserting it into the PlaceHolder) it should perform the same as when it would have been declared in the markup.

If you are setting properties outside of the LoadControl call, why are you making that new empty object array instead of just using the overload that has one parameter?

Also, if you attach a debugger to it and step through, do you notice anything weird about the control before you do your Controls.Add() call? Is there an exception being thrown? if so, which one? if not, what does the markup in the browser look like for where the placeholder is?

“ Works”有点模棱两可,但是,如果您的意思是永远不会执行事件处理程序,则需要在页面onload事件中加载它。

如果控件要求使用viewstate,则必须确保在Page_Load事件之前将其添加到页面中,否则将不会填充viewstate,并且最有可能的事件和其他项目将无法正常运行。

One important difference is that if you create a control dynamically, you will not get, by default, any values from skins set. You must manually call control.ApplyStyleSheetSkin(page): http://msdn.microsoft.com/en-us/library/system.web.ui.control.applystylesheetskin.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM