Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Render and get html from usercontrol

Reply
Thread Tools

Render and get html from usercontrol

 
 
John Olsen
Guest
Posts: n/a
 
      06-10-2005
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



 
Reply With Quote
 
 
 
 
Craig Deelsnyder
Guest
Posts: n/a
 
      06-10-2005
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
 
Reply With Quote
 
 
 
 
John Olsen
Guest
Posts: n/a
 
      06-10-2005
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:...
> 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



 
Reply With Quote
 
hellocrowley hellocrowley is offline
Junior Member
Join Date: May 2007
Posts: 1
 
      05-28-2007
I have exactly the same problem too. Can anyone help?
 
Reply With Quote
 
chrisajohn chrisajohn is offline
Junior Member
Join Date: Jan 2008
Posts: 1
 
      01-09-2008
OK - this isn't perfect, but here is how I got around it.

First a created a "dummy" user control (lets call it UCDummy) that only contained the form tag and the control I "really" wanted to render the output of.

The reason I did this is because sometimes I use the "real" usercontrol on my website and if I were to keep the form tag on it, it would give me an error saying there are too many form tags on the page.

For example - this code is within the "dummy" user control:

Code:
<asp:form runat="server">
... THE USER CONTROL HERE
</asp:form>
Then on the page where I want to render the html (in my case I put it in the database for later use), I do the following:

Code:
StringBuilder SB = new StringBuilder();
StringWriter SW = new StringWriter(SB);
HtmlTextWriter htmlTW = new HtmlTextWriter(SW);
UCDummy.Visible = true;
UCDummy.RenderControl(htmlTW);
WhereEverIWantItToGo = SB.ToString();
UCDummy.Visible = false;
Now I have the best of both worlds. It does necessitate an extra file, but sometimes it doesn't need to be pretty, it just needs to work.

Hope this helps!
 
Reply With Quote
 
shailendrasinh shailendrasinh is offline
Junior Member
Join Date: Aug 2010
Posts: 1
 
      08-30-2010
Hi John, I have also the same problem in my cms. But the thing is I have a NewsLetter control and I am tring to render it in html. It gives the error and if I put <form runat="server"> again in control, it says duplicate form tags.

One more issue, is I get html rendered but I have server side event in that control which is not executed from the page when I render the control.

Please let me know If you have found any solution for this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Render UserControl in Class wihtout access to webform? =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?= ASP .Net 0 08-01-2007 06:30 PM
Accessing rails render method outside of view / Decorating render Glenn Gillen Ruby 0 11-17-2006 02:30 PM
UserControl render html in server side rabii.mail@gmail.com ASP .Net 1 05-30-2005 03:54 PM
Usercontrol derived from Usercontrol without double Html Code Reik ASP .Net Web Controls 1 04-12-2005 04:34 PM
Page.Render do not render complete page Lau Lei Cheong ASP .Net 1 05-15-2004 04:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57