Solved

Loading Web User Control Dynamically

Posted on 2010-11-09
15
256 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 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