Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loading Web User Control Dynamically

Posted on 2010-11-09
15
Medium Priority
?
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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