473,387 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Render and get html from usercontrol

Hi.

I`m building a small CMS, and want to add the possibility to include server
side code inside static html-strings that is stored in a database.

For e.g. in the string "<div><b>News></b><br>[Controls/News.ascx]</div>",
[Controls/News.ascx] should be replaced by the rendered html-outpu from a
usercontrol that prints out database content. I use regex to get the content
of the []-tags, and load the control and get the output-html with the
following code:

/// <param name="html">Static html from database whith [] tags containg
usercontrols to render</param>
private string renderIncludes(string html )
{
string pattern = @"(\[.*\])";
Match m = Regex.Match(html, pattern, RegexOptions.IgnoreCase);
if (m.Success)
{
for(int i = 0; i < m.Groups.Count; i++)
{
string search = m.Groups[i].Value;
string control = search.Replace("[","").Replace("]","");
Control c = LoadControl(control);
c.DataBind();
string _html = renderControl(c);
html = html.Replace(search, _html);
}
}
return html;
}

private string renderControl(Control ctrl)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter tw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}

This works well for usercontrols with text ouput only (e.g. usercontrols
with Repeaters), but when it comes to usercontrols with input a TextBox (e.g
a contact form), I get the following exception:

System.Web.HttpException: Control 'Name' of type 'TextBox' must be placed
inside a form tag with runat=server

Source:

ctrl.RenderControl(hw);

Does anybody have a tip on what to do, or how to proceed? Is there another
way of doing what I`m trying to accomplish?

Best regards,
John

Nov 19 '05 #1
2 3651
John Olsen wrote:
Hi.

I`m building a small CMS, and want to add the possibility to include server
side code inside static html-strings that is stored in a database.

For e.g. in the string "<div><b>News></b><br>[Controls/News.ascx]</div>",
[Controls/News.ascx] should be replaced by the rendered html-outpu from a
usercontrol that prints out database content. I use regex to get the content
of the []-tags, and load the control and get the output-html with the
following code:

/// <param name="html">Static html from database whith [] tags containg
usercontrols to render</param>
private string renderIncludes(string html )
{
string pattern = @"(\[.*\])";
Match m = Regex.Match(html, pattern, RegexOptions.IgnoreCase);
if (m.Success)
{
for(int i = 0; i < m.Groups.Count; i++)
{
string search = m.Groups[i].Value;
string control = search.Replace("[","").Replace("]","");
Control c = LoadControl(control);
c.DataBind();
string _html = renderControl(c);
html = html.Replace(search, _html);
}
}
return html;
}

private string renderControl(Control ctrl)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter tw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}

This works well for usercontrols with text ouput only (e.g. usercontrols
with Repeaters), but when it comes to usercontrols with input a TextBox (e.g
a contact form), I get the following exception:

System.Web.HttpException: Control 'Name' of type 'TextBox' must be placed
inside a form tag with runat=server

Source:

ctrl.RenderControl(hw);

Does anybody have a tip on what to do, or how to proceed? Is there another
way of doing what I`m trying to accomplish?

Best regards,
John


It really means what it says. Somewhere you have to have in your HTML
markup a form with id="something" and runat="server" as attributes.
Only within that are controls like Textbox valid. So in the HTML
surrounding all this dynamic content, add such a form...

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 19 '05 #2
But my really problem is that I have <form runat=server> tags in the base
..ASPX page that is supposed to display the html from the database and and
thus the output from possibly any included usercontrols ([News.ascx],
[Contact.ascx])

If I add <form runat..> in the usercontrols, I get an error because when the
rendered output is added to the ASPX page, the results contain two forms...

Any thougts...??

"Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
news:u6**************@tk2msftngp13.phx.gbl...
John Olsen wrote:
Hi.

I`m building a small CMS, and want to add the possibility to include
server
side code inside static html-strings that is stored in a database.

For e.g. in the string "<div><b>News></b><br>[Controls/News.ascx]</div>",
[Controls/News.ascx] should be replaced by the rendered html-outpu from a
usercontrol that prints out database content. I use regex to get the
content
of the []-tags, and load the control and get the output-html with the
following code:

/// <param name="html">Static html from database whith [] tags containg
usercontrols to render</param>
private string renderIncludes(string html )
{
string pattern = @"(\[.*\])";
Match m = Regex.Match(html, pattern, RegexOptions.IgnoreCase);
if (m.Success)
{
for(int i = 0; i < m.Groups.Count; i++)
{
string search = m.Groups[i].Value;
string control = search.Replace("[","").Replace("]","");
Control c = LoadControl(control);
c.DataBind();
string _html = renderControl(c);
html = html.Replace(search, _html);
}
}
return html;
}

private string renderControl(Control ctrl)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter tw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new
System.Web.UI.HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}

This works well for usercontrols with text ouput only (e.g. usercontrols
with Repeaters), but when it comes to usercontrols with input a TextBox
(e.g
a contact form), I get the following exception:

System.Web.HttpException: Control 'Name' of type 'TextBox' must be placed
inside a form tag with runat=server

Source:

ctrl.RenderControl(hw);

Does anybody have a tip on what to do, or how to proceed? Is there
another
way of doing what I`m trying to accomplish?

Best regards,
John


It really means what it says. Somewhere you have to have in your HTML
markup a form with id="something" and runat="server" as attributes. Only
within that are controls like Textbox valid. So in the HTML surrounding
all this dynamic content, add such a form...

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dave McCracken | last post by:
I am hosting .Net UserControls in HTML pages with Object Tags. These UserControls are clients of remotable objects that run on the same machine. The remote objects execute callbacks via sponsor...
4
by: ATS | last post by:
BUG/PRODUCTION-DOWN: UserControl running from HTML not working. Someone please help, I have a production down situation. For whatever reason, UserControls are not owkring from HTML/IE. When...
1
by: Steve Gelfmann | last post by:
I am new to ASP.NET. So I am trying to learn from the book I just bought. There is a simple example of how to build a user control which I religiously typed into the Notepad, but the results are...
4
by: Zuel | last post by:
Hi Folks. So I have a small problem. My DoPostBack function is not writen to the HTML page nor are the asp:buttons calling the DoPostBack. My Goal is to create a totaly dynamic web page where...
1
by: rabii.mail | last post by:
Hello, I've developped simple UserControl and I wanna to get generated html in server side. I've tested that using WebControl (RenderContent) and it works well. But, when I try to do that with my...
5
by: serge calderara | last post by:
Dear all, I am new in asp.net and prepare myself for exam I still have dificulties to understand the difference between server control and HTML control. Okey things whcih are clear are the fact...
5
by: Piotr Strycharz | last post by:
Hi I want to render a page in memory. That is (pseudo-code): Page aPage = new Page(); LiteralControl lc = new LiteralControl("<form runat=server><asp:Label id=label Runat=server/>...");...
0
by: =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?= | last post by:
I need to render a user control as a HTML string. I do not want the control to appear on the page, however. Here is what I am doing… I have an HTML template that will ultimately be sent via...
1
by: =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?= | last post by:
I posted this a week ago, but did not get a respone yet. I need to render a user control as a HTML string. I do not want the control to appear on the page, however. Here is what I am doing… I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.