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...
-thx-
![]() |
0 |
![]() |
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.
![]() |
0 |
![]() |
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..
-thx-
![]() |
0 |
![]() |
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 ";
PlaceHolder1.Controls.Add(literal1);
Literal literal2 = new Literal();
literal2.Text = "jumped ";
PlaceHolder1.Controls.Add(literal2);
Literal literal3 = new Literal();
literal3.Text = "over the moon. ";
PlaceHolder1.Controls.Add(literal3);Just remember that since you're dynamically adding the Literals, that they're not in ViewState, and must be re-added on each PostBack.
NC...
![]() |
0 |
![]() |
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>));
![]() |
0 |
![]() |
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!
-thx-
![]() |
0 |
![]() |
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();
phInfo.Controls.Add(lit);
break;
case "ASCXInfo":
phInfo.Controls.Add(LoadControl(arInfo2[1].ToString()));
break;
}
}
}
Just remember what NC already stipulated, the Placeholder object is not contained within the ViewState of the page
![]() |
0 |
![]() |
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?
-thx-
![]() |
0 |
![]() |