How to programmatically add user control to the middle of text inside Literal text control ? Pls help...

Greetings fellow programmers,

I greatly appreciate all the help I can get. Here is the problem.

I have a function that pulls content from the database and put the content into a literal control. Somewhere in that content, I need to insert a user control (bla.ascx). Ideally, I want to use string.replace to find the location where I want to insert the user control. But I can't... there is no such option on the literal control methods and properties.

Do I have to overwrite render method?

I'd normally use loadcontrol and add it to the placeholder control, but in this case, I can't dynamically add a placeholder control into a middle of text in the literal control.

Any suggestions?

Here is the code:

protected System.Web.UI.WebControls.Literal Content;

Content.Text = database.getContent();
Control MyUserControl;
MyUserControl = LoadControl("bla.ascx");
// Inside Content I want to replace a word, such as <changethis> with the user control (MyUserControl)... how to do this?

Thanks a bunch...

10/4/2005 4:40:41 PM 93655 articles. 6 followers. Follow

7 Replies

Similar Articles

[PageSpeed] 48

Instead of using a Literal Control, have you considered a PlaceHolder and adding Controls into the placeholder.  I have not played around with adding multiple controls into a singe PlaceHolder object, but I don't see why you couldn't....but worst come to worst

1) replace the literal function with a Placeholder
2) rely on the ASCX control to render the text and the other objects that you need to display.
10/4/2005 4:55:49 PM
Thanks for the reply but I dont think you understand the problem. It's how to add the user control into a middle of the text. If I replaced the literal with placeholder, I will  end up trying to find a way to insert the user control to the middle of the text in the placeholder. Basically, what i'm trying to find out is how to positioned the user control in the middle of the text/content. Any server control I put inside that content will not get compiled, instead, it will be displayed as it is.

Help pls.. 
10/4/2005 5:18:36 PM

In the aspx file, add a PlaceHolder inside the <form> tags:
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>

That will add this to the CodeBehind file:
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;

Then to set the text:
Literal literal1 = new Literal();
literal1.Text = "The cow ";
Literal literal2 = new Literal();
literal2.Text = "jumped ";
Literal literal3 = new Literal();
literal3.Text = "over the moon. ";

Just remember that since you're dynamically adding the Literals, that they're not in ViewState, and must be re-added on each PostBack.


10/4/2005 5:45:15 PM

NC - exactly!

To go further, if you want to add an ASCX file you would do

PlaceHolder1.Controls.Add(LoadControl(<PATH & FILENAME TO ASCX FILE>));

10/4/2005 5:56:47 PM
Thank you for your help. I've found a solution based on your answer, however, the circumstances is not the same as what you have in mind.

Lets say on my aspx page I have this literal control.
<asp:Literal ID="Content" Runat=server></asp:Literal>

And then on the code behind, within the page load function, I populate the literal control with some text from the database. The key is that the text comes from a database !

Content.Text = database.getContent();

What I want to achieve is to scan the text and replace certain words with a usercontrol.
Ideally, this is what I want:

Content.Text.Replace("keyword", myusercontrol.ascx);

But we all know that it's not gonna happen that easily. Is there a way to achieve this? Or am I approaching the problem from a wrong angle?

Thanks again!
10/4/2005 7:24:07 PM

I like solving problems, and your problem was especially interesting to me...however, using the Literal Control was, in my opinion, the wrong object...I still think that the Placeholder Object is your best bet...because that is exactly what the object was intended to do, contain other objects....

Below is some code, that may or may not help you, but hopefully, it will lead you in the right direction...

protected System.Web.UI.WebControls.PlaceHolder phInfo;

private void Page_Load(object sender, System.EventArgs e)


string info = "LiteralInfo:Here is Test1 Rendered<BR>~ASCXInfo:Test1.ascx~LiteralInfo:<BR>Here is Test2 Rendered<BR>~ASCXInfo:Test2.ascx~LiteralInfo:<BR>This is the end.";//this could be the string you get from your DB

char[] split1 = {'~'};

char[] split2 = {':'};

string[] arInfo1 = new string[]{};

arInfo1 = info.Split(split1);

for(int x=0; x<arInfo1.Length;x++)


string[] arInfo2 = new string[]{};

arInfo2 = arInfo1[x].Split(split2);

switch (arInfo2[0])


case "LiteralInfo":

Literal lit = new Literal();

lit.Text = arInfo2[1].ToString();



case "ASCXInfo":






Just remember what NC already stipulated, the Placeholder object is not contained within the ViewState of the page

10/4/2005 9:39:25 PM
Thank you so much for your input. The concept is very clever but don't you think it's too complicated to achieve something so straighforward and simple as that. 

I'll test your code tomorrow and will let u know how it goes.

Thanks again ! I appreciate it very much.

In case there are other people interested in enlighten me from a different approach, let me sum up the problem here:

There is a database that holds html content for a website (saved by Content Management System). Within that html content, regular user can add custom tag such as <insertcontrol1> or <insertcontrol2>. What I need is to scan this content and replace any occurance of  '<insertcontrol1>' or '<insertcontrol2>' with a corresponding usercontrol. The user control itself is nothing but a bunch of html and javascript code, nothing fancy.

Is there other way to achieve this?

10/4/2005 11:09:07 PM

Similar Artilces:

control text in a text box inside a Form view
I want to get text from a querystring into a text box that is prt of a form view. I have tried writing code in the page load that changes the text value inside that text box but i get the error that the texbox name is not in the current context. My question is how to call a textbox (or any other control) inside a form view? You can use the FormView's OnDataBound event to access the textbox on your formview: OnDataBound="FormView1_OnDataBound" Protected Sub FormView1_OnDataBound(ByVal sender As Object, ByVal e As EventArgs)        If FormView1.Curren...

Web user control inside update panel replace with another web user control
 Hi there. I have a page with a menu. an and then the content. My thing is. That i have the update panel, and inside that i have a regual panel in which i add the Web User Controls i need. Which all works fine. My problem is. That i i have a submit funcion on almoast all the Web User controls. What i would like is that i inside that update panel, can dispose the original Web user Control and then add a response Web User Control. In normal language. Is there a way that i can load one web user control into antoher and then disposose the original? Thanks in advance hello. well, what ...

MS Publisher / Word-like textframe web control... flowing text between text controls
Does anyone know of an ASP.Net web control capable of linking text boxes/frames together similarly to the Publisher & Word textframe objects (via the NextLinkedTextFrame property in Publisher or the TextFrame.Next property in Word)? The goal is to have text flow from one textbox into another. Thx.....

access controls on web user control x from web user control Y
I have 2 web users controls in a page and I would like to get or set the selected values of a few dropdown boxes on web user control X from web user control Y and I also would like to know how to access functions that are on a web user control X from a web user control Y, is this possible? How can I do it? User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below Using PropertiesIf the Sample.ascx control has two textboxes and submit button.You can access the valu...

Add user control from user control to web page
Hi there, I have a user control, that is inside another user control on a web page. From this user contfol, I need to add something to a place holder on the parent web page. I am having troubles referencing the placeholder correclty. I can get it fine as build time, but on runtime it kicks up the "Object reference not set to an instance of an object" error. Any ideas would be greatly appreciated. -------------------------------------------------------------- protected Civil_and_Corporate.candcadmin._default _default; ------------------------- Control objControl...

Web User Control to Web User Control...
Hi, I have 2 User controls on a page and want Control1 to call a method of Control2. Is this Possible? Example: Con1 = Datalist showing products, each with a Quantity Box and a Button. Con2 = Repeater showing Cart Preview (name, Price) in page.aspx. When I click on the button on the DataList in Con1 it adds the item into the Session based Cart. However it does not update the Cart Preview. I need to call BindPreview() in Con2 in order to update the Cart Preview... Does that make sense? argh! any help much appreciated! PeteWeb Design Nottingham, UK Well I can call BindPreview() from m...

superreview requested: [Bug 285285] Add SINGLE_LINE state to single line text inputbox : [Attachment 176741] Clean up form control and text field state code, add single line extended state support
Louie Zhao <> has asked Henry Jia <> for superreview: Bug 285285: Add SINGLE_LINE state to single line text inputbox Attachment 176741: Clean up form control and text field state code, add single line extended state support ...

superreview granted: [Bug 285285] Add SINGLE_LINE state to single line text inputbox : [Attachment 176741] Clean up form control and text field state code, add single line extended state support
Henry Jia <> has granted Louie Zhao <>'s request for superreview: Bug 285285: Add SINGLE_LINE state to single line text inputbox Attachment 176741: Clean up form control and text field state code, add single line extended state support ...

How to access individual controls from a form containing the web user control containing those controls
 Below is a block of code in my .aspx form named default.aspx. In this form I load a web user control. I then add it to a place holder control.(Lines 3 and 4 below) So how do I access the individual controls in my web user control? I want to set the properties on those controls, but I am baffled as to how to access the control. For example I have a text label control named "label3". How do I access this control. Because for example, under certain situations I want to hide that control. ( Label3.Visible = false;)  1    if(!Page.IsPostBack) 2&nb...

Javascript inside "web user control" not run if load "web user control" under ajax UpdatePanel
I have this problem and please if some can help me, I place here a sample code to see, but in my real problem the web user control have a very complicate javascript. I have a web user control that have some javascript inside, let say for example.1 <script type="text/javascript" > 2 function RunMe() 3 { 4 alert("Client id:<%=ClientID%>"); 5 } 6 </script> 7 8 9 <a href="javascript:RunMe()">Run javascript</a> a simple call to a simple javascript, inside the web user control And now I have...

Web Control with <abc:Control>TEXT HERE</abc:Control>
I'm trying to create a custom web control and support putting stuff in the middle - between the openning and closing tags:  <abc:Control>TEXT HERE</abc:Control> I would like to be able to put both text and other web controls between the tags. How do I do this? hi, why not the normal web user control? It also works fine. Maybe it is hard to create a control with the openning and closing tags...

chinese character in rich text control and text control
I have a chinese character in either rich text control or text control. The character is look fine if it is a small size (10, 12), however when the font size is changed bigger (36 or 72), the character is out of shape(many dot, not smooth). Is anyone has any idea to print a bigger chinese character from pb7.03. ...

Help! How to add a standard web control to a web form?
I have just created a web application using Visual Studio .NET 2008. I opened the Default.aspx in design view, but I noticed that if I drag any control under the standard control tab to the web form, the cursor changes to the shape ( a circle with a line across to it ) that tells me that I cannot do that.  Why? Did I miss something?  It seems this is the very basic thing people will do. Please help!     It could be because you designer view is out of sync with you source view. Try right clicking into your design and selected 'Refresh...

Referencing a text box in a user control placed within another user control
 I need help in referencing a text box from .aspx code behind. This text box is placed in a user control uc1 that is placed within another user control uc2. User control uc2 is placed on the page. Thanks!  UserControl foundControl = ((UserControl)uc2.FindControl("uc1")); Textbox foundTextbox = ((TextBox)foundControl.FindControl("TextBox1")); or something like that Don't forget to click "Mark as Answer" on the post that helped you. Can that be done in a single statement? ((TextBox)((UserControl)uc2.FindControl("uc1")).FindCon...

Web resources about - How to programmatically add user control to the middle of text inside Literal text control ? Pls help... -

[...] How to Programmatically Administer Off-Facebook Pages with the Open Graph API - Inside Facebook ...
With the Open Graph protocol that Facebook announced at last week’s f8 conference, any URL can now be treated just like a Facebook Page. After ...

Well, This Is Different Time Inc. Now Selling Print Ads Programmatically
Machines are now selling some print ads at Time Inc. The nation's largest magazine publisher owner of People, Time, Sports Illustrated and InStyle ...

Now Marketers Can Buy Sponsored Content Programmatically
Publishers' branded content is going programmatic. Today, Sharethrough and AppNexus are rolling out real-time bidding for Sharethrough's news ...

Wesabe makes Financial Data available programmatically
Wesabe has just done what I've been trying to convince banks to do for years. They're making financial data available via a simple POX (Plain ...

IPG Begins Buying Local Broadcast Programmatically: Can Target, Retarget Viewers In Near Real-Time
In what likely is the most significant development yet to bring programmatic media-buying to local broadcast television, Interpublic's Mediabrands ...

Google Launches Cloud SQL API To Allow Developers To Manage Their Databases Programmatically
... to use Google’s admin interface. Today, however, Google is launching the Cloud SQL API . This new REST API will allow developers to programmatically ...

Are Programmatically Purchased Native Ads Native Enough?
It seems everyone and their mothers are pushing native ad units and tools to support them. Facebook, Twitter, Yahoo and most recently AOL all ...

Most Major U.S. Publishers Now Sell Ads Programmatically
Most major online publishers in the U.S. now sell at least some of their ad space using automated "programmatic" technology, according to new ...

TV Ads Could Soon Be Sold Programmatically, Magna’s Todd Gordon
The key efficiency new-wave digital media sales holds over TV is clear – super-targeting through addressable, automated and programmatic buying ...

How To Use Facebook's New Power Editor For Ads
Here's a guide to the power editor in Facebook's ad manager.

Resources last updated: 1/18/2016 4:24:57 AM