Parsing text with code and ASP.NET

G

Guest

I am trying (without any luck) to find a way to parse a string and output its
processed results.

ie:

string text = "This is text from a CMS tool <asp:Label runat=\"server\"
id=\"test\" text=\"Hello World\" />";

OR

string text = "Current date is: <%=DateTime.Now.ToString() %>";

Response.Write(SomeHowEvaluateText(text));

Any ideas?
 
G

Guest

Steven,

It isn't entirely clear from your post exactly what the goal is, but maybe
this will help. In the first example, it looks like you are attempting to
render a declarative Label control into the Response Output stream, that
won't work at all.

In your second example you could do the following:

string text = "Current date is:" +DateTime.Now.ToString() ;

Response.Write(text);

The issue here is that declarative markup, e.g. "<asp:Label ...." is
processed when the page runs through the server. You CAN add a label control
dynamically in your codebehind, if that helps. That would be something like
(using a placeholder control):

Label lbl = new Label();
lbl.Text="This is the text";
lbl.Id="Label1";
Placeholder1.Controls.Add(lbl);

Peter
 
G

Guest

Hi Peter,

I am not trying to programatically add controls or concatinate strings
together; rather I need to parse/evaluate ASP.NET code and render the output.

For context purposes, this is needed for a CMS-like application where the
data is stored in a DB and I want users to be able to leverage the ASP.NET
runtime.


The full field may look something like this:

string text = @"Hello world. The current date is <%=DateTime.Now%>. <br
/><br />Here is a custom control:<sb:TestControl runat=""server"" id=""test""
/>";


I want to be able to render the output of this to the page.
 
G

Guest

Hi Peter,

First off, thanks for all of your assistance so far -this is definitely a
push in the right direction!

Unfortunatly I don't think this will do it for me as I do not want to serve
up virtual pages, rather, I will have an ASP.NET page (that it based off a
master page) that contains a webcontrol (think of a literal control for
simplicity sake) whos job it will be to emit the rendered content of ASP.NET
controls and code from a database table.
 
G

Guest

You could still use a custom Virtual Path Provider for this. Providers can
also be chained, so if something isn't found, it would fallback to th
Previous (the default) provider. If you have stuff coming out of a database,
this could be the way to go because it gives you the ability to store
user-specific code and controls in a database table column / row and key off
of something like a querystring item.
Peter
 
Joined
Nov 4, 2010
Messages
1
Reaction score
0
Parsing control text

This works in many cases:

Code:
this.Controls.Add(this.Page.ParseControl(this.Template));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top