Solved

Loading Web User Control Dynamically

Posted on 2010-11-09
15
253 Views
Last Modified: 2012-05-10
Hi Experts,

We have a text field in our database which allows users to decide where to place a built usercontrol...

So the text that would be saved could be something like this: <table><tr><td>Use the below form to contact us</td></tr><tr><td>#ContactForm2#</td><tr></table>

when we are displaying the text on the front end we need to replace #ContactForm2# with the relevant control which may be loadcontrol("~/ContactForm2.ascx")

Has anyone done this before? If so, how?

Regards
Desperate
0
Comment
Question by:skrombeen
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 3

Expert Comment

by:abdkhlaif
ID: 34092446
i'm not sure about loading controls from a file but you can create and add a control at runtime:

Label l = new Label();

l.Text = "test";

this.Page.Controls.Add(l);

Open in new window

0
 
LVL 2

Expert Comment

by:AarthiPrabakaran
ID: 34092493
string.Replace ("#ContactForm2#",usercontrol )
0
 
LVL 1

Author Comment

by:skrombeen
ID: 34092563
the method: string.Replace ("#ContactForm2#",usercontrol )
does not work, as its expecting a string/char to replace the first part with
0
 
LVL 6

Expert Comment

by:ErikVB
ID: 34092616
you can first parse the string from the database, and create literal controls for the simple html. When you hit a token you recognize, load the correct control. So, in the end, all you will be doing is adding controls to the page. most of them will be litererals, some of them will be your custom user controls. Of course.. do all of this in page.init, otherwise you will lose viewstate...
0
 
LVL 1

Author Comment

by:skrombeen
ID: 34092752
thanks ErikVB, DO you perhaps have a little sample code for me to work from?
0
 
LVL 6

Expert Comment

by:ErikVB
ID: 34092953
yep, i will post that tonight, on my way to a client now...
0
 
LVL 1

Author Comment

by:skrombeen
ID: 34092997
thanks, i'll wait till tonight!
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:derftoy
ID: 34094344
I use a placeholder control.  Then I just use placeholder.controls.add([your control]).

You can just dinamicaly create the control and add it.  Something like this

webusercontrol cntrl = new webusercontrol();
placeholder.controls.add(cntrl);
0
 
LVL 1

Author Comment

by:skrombeen
ID: 34094381
Hi Derftoy,

My data will look something like this: <table><tr><td>Use the below form to contact us</td></tr><tr><td>#ContactForm2#</td><tr></table>

and the ## part will be replaced by the user control...I dont see how your code will resolve the issue at hand?
0
 
LVL 4

Expert Comment

by:derftoy
ID: 34094441
I see what your saying... your building the page dynamicaly and putting the #ContactForm2# in the HTML.  I use the placeholder control and add the usercontrol there as I displayed earlier.  Sorry, I think Erik has what you need... you will need to parse the HTML.
0
 
LVL 6

Expert Comment

by:ErikVB
ID: 34095947
for this simple problem, a simple solution should suffice. You can split the string on your token delimiters, and create controls for the seperate substrings. something like this code. Disclaimer: i typed this from the top of my head, not guaranteed errorfree ;)

dim originalString as string = "<table><tr><td>Use the below form to contact us</td></tr><tr><td>#ContactForm2#</td><tr></table>"



dim splitString as string() = originalString.Split("#"c)

dim NewControl as control

for each subString as string in splitString

    Select Case subString

        Case "ContactForm2"

            newControl = loadcontrol("~/ContactForm2.ascx")

            newControl.ID = me.ID + ContactForm2

        Case else

            newControl = new LiteralControl(substring)

    End Select

    me.Controls.Add(newControl)

next

Open in new window

0
 
LVL 6

Expert Comment

by:ErikVB
ID: 34095961
hmm, line 9 should have been:
newControl.ID = me.ID + "ContactForm2 "
<:>if you expect more contactforms to be put in the string, you need to add a counter as well
 
0
 
LVL 1

Author Comment

by:skrombeen
ID: 34096017
thanks Erik, but your sample would add the control to the form and exclude the <table ... part... or did i miss something?

Do you think i could run some java to move the item from the "temporary location" into the table part?
otherwise, i've been thinking about old dirty iframes...
0
 
LVL 6

Accepted Solution

by:
ErikVB earned 500 total points
ID: 34096093
the trick is what happens in the else statement of the select.
so if your html would be very simple, like this: <div>#ContactForm2#</div>, the split would give you a string array with 3 elements:
1: <div>
2:ContactForm2
3:</div>
when you loop through those items, you will create controls as you need them. For the 1st and the 3d item, you would create a new literal control, with the corresponding string as content for the literal. This will be output to the pages as just your plain html (because thats what a literal does.. it just outputs what you put into it)
for the second element you will load your usercontrol, and add that to the page
no iFrames needed and no JavaScript / jQuery either..
0
 
LVL 1

Author Closing Comment

by:skrombeen
ID: 34096118
Top Class Erik, thank you! I get it!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now