c# - Adding User Controls at runtime via <script runat="server"> -


i have problem executing control inside <script runat="server"> tags in aspx page.

the control works when defined declaratively in html section of page, not able make work when placed within script tag.

at beginning of page register control with:

<%@ register assembly="app_web_exemple.ascx.cc671b29" namespace="moncontrol" tagprefix="moncontrol" %> 

in html, call (successfully) following declaration:

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

when try add programmatically through <script runat="server">, however, not able execute it. tried tags asp:text , asp:literal, follows, doesn’t work.

in html part:

<asp:text id="testcontrol" runat="server" /> 

in script part

testcontrol.text = "<moncontrol:exemple isbn=\"9782894646151\" runat=\"server\" />"; 

to clarify, you're looking programmatically add user control web forms page @ runtime. there few ways of accomplishing this.

before begin, it's worth noting code wrote "works" insomuch compiles , doesn't throw runtime error. it's not executing control. suspect if @ html, you'll find control declaration being output string literal (i.e., unprocessed server). disregarded browser since doesn't know <moncontrol:exemple /> tag represents. that's not want.

establishing control container

regardless of approach take, you'll want start type of container on page can add control to, such panel. if don't want container output wrapper markup, can use placeholder:

<asp:placeholder id="controlcontainer" runat="server" /> 

this serves similar purpose current text control, except purpose provide container add user control to. more on later.

programmatically creating control

next, you're going create control programmatically. run various options. universal approach use parsecontrol() method (reference). should this:

control control = page.parsecontrol("<%@ register assembly=\"app_web_exemple.ascx.cc671b29\" namespace=\"moncontrol\" tagprefix=\"moncontrol\" %><moncontrol:exemple isbn=\"9782894646151\" runat=\"server\" />"); 

that parse control using same method processes declarative syntax on page, , return control object exemple control first control in controls collection.

i find touch sloppy, though, there cleaner approaches. in case, appears control being compiled assembly and, therefore, has code behind defined inherits usercontrol. if so, should able like:

exemple control = new exemple(); 

and set properties on programmatically, way in other c# object. cleaner!

if control instead being compiled dynamically server, you'd instead use reference directive loadcontrol() method, described in msdn article how to: create instances of asp.net user controls programmatically. don't believe method work you, however.

adding control instance page

regardless of approach take, next step same: add control you've programmatically added page adding controls collection of target container. e.g.,:

controlcontainer.controls.add(control); 

note: can technically add page class's control collection, too, doesn't give control on over page placed; having placeholder control (or equivalent) lets specify want control appear.

i hope helps. there couple of caveats depending on how wrote , compiled control, should give basic structure needed address problem.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -