Solved

Loading Web User Control Dynamically

Posted on 2010-11-09
15
254 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

21 Experts available now in Live!

Get 1:1 Help Now