Solved

Loading Web User Control Dynamically

Posted on 2010-11-09
15
258 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

732 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