简体   繁体   中英

Adding ASP.NET User Controls at runtime via <script runat=“server”>

I am having trouble executing a control inside the <script runat="server"> tags in an *.aspx page.

The control works when it is defined declaratively in the HTML section of the page, but I am unable to make it work when placed within the script tag itself.

At the beginning of the page I register my control with:

<%@ Register assembly="App_Web_exemple.ascx.cc671b29" namespace="Moncontrol" tagprefix="moncontrol" %>

Then, in the HTML, I call it (successfully) with the following declaration:

<moncontrol:exemple ISBN="9782894646151" runat="server" />

When I try to add it programmatically within the <script runat="server"> , however, I am unable to execute it. I tried with the tags <asp:Text /> and <asp:Literal /> , as follows, but that also doesn't doesn't work.

In the HTML part:

<asp:Text id="TestControl" runat="server" />

In the script part

TestControl.Text = "<moncontrol:exemple ISBN=\"9782894646151\" runat=\"server\" />";

To clarify, what you're looking to do is programmatically add a User Control to your Web Forms page at runtime. There are a few ways of accomplishing this.

Before we begin, it's worth noting that the code you wrote likely "works" insomuch that it compiles and doesn't throw a runtime error. But it's also not executing the control. I suspect if you look at your HTML, you'll find the control declaration being output as a string literal (ie, unprocessed by the server). It is then disregarded by the browser since it doesn't know what the <moncontrol:exemple /> tag represents. That's obviously not what you want.

Establishing a Control Container

Regardless of which approach you take, you'll want to start with some type of container on your page that you can add the control to, such as a Panel . If you don't want the container to output any wrapper markup, you can use a Placeholder :

<asp:Placeholder id="ControlContainer" runat="server" />

This serves a similar purpose as your current Text control, except its only purpose is to provide a container that you will add your user control to. From ASP.NET's perspective, however, this can be any type of server control, including a <script runat="server"> , as per your request. More on that later.

Programmatically Creating the Control

Next, you're going to create the control programmatically. This is where we run into various options. The most universal approach is to use ParseControl() method ( reference ). This looks something like this:

Control control = Page.ParseControl("<%@ Register assembly=\"App_Web_exemple.ascx.cc671b29\" namespace=\"Moncontrol\" tagprefix=\"moncontrol\" %><moncontrol:exemple ISBN=\"9782894646151\" runat=\"server\" />");

That will parse the control using the same method that processes the declarative syntax on the page, and return a Control object with your Exemple control as the first control in its Controls collection.

I find that syntax a bit sloppy, however, since it's representing a .NET object and its properties as a string literal. Given that, there are some cleaner approaches. In this case, it appears that your control is being compiled into an assembly and, therefore, likely has a Code Behind defined which inherits from UserControl . If so, you should be able to simply do something like:

Exemple control = new Exemple();

And then set the properties on it programmatically, the way you would in any other C# object. Much cleaner!

If your control was instead being compiled dynamically by the server, then you'd instead use the Reference directive with the LoadControl() method, as described in the MSDN article How to: Create Instances of ASP.NET User Controls Programmatically . I don't believe that method will work for you, however.

Adding the Control Instance to the Page

Regardless of which approach you take, the next step is the same: you then add the control you've programmatically added to your page by adding it to the Controls collection of the target container. Eg,:

ControlContainer.Controls.Add(control);

Note : You can technically just add this to the Page class's Control collection, too, but that doesn't give you any control over where on the page it is placed; having a PlaceHolder control (or equivalent) lets you specify exactly where you want the control to appear.

I hope this helps. There are a couple of caveats depending on how you wrote and compiled your control, but this should give you the basic structure needed to address your problem.

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