473,657 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Some controls cannot RenderControl

Hi group,

I'm trying to get the outputted html from some specific controls on a page,
and I'm finding that some controls will render fine, while others will throw
the error that "[control] must be placed inside a form tag with
runat=server." For example, a label control will render fine, but a button
will not. It seems that any control that has a server-side click event will
not accept being rendered in this way. Does anyone know what I can do to
get such controls to render? Here's my code:

aspx snippet:

<form id="f" runat="server">
<div>
<asp:Label ID="control" runat="server" Text="Chage to an asp:Button"
/>
</div>
</form>
..cs snippets:

using System;
using System.Web.UI;
using System.IO;

// ...

using (StringWriter sw = new StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter( sw);
control.RenderC ontrol(htw);
Response.Write( sw.ToString());
Response.End();
}

Thanks for any help.

Joshua C

Jul 9 '07 #1
2 5314
Hi there,

I'm afraid once control is put on the page, its Page & Parent property is
assigned, and here's a statement that prevents control from being rendered:

bool useSubmitBehavi or = this.UseSubmitB ehavior;
if (this.Page != null)
{
this.Page.Verif yRenderingInSer verForm(this);
}

There is no problem in assigning Page to null, unfortunatelly, if Page is
null, Parent.Page is returned:

public virtual Page Page
{
get
{
if ((this._page == null) && (this.Parent != null))
{
this._page = this.Parent.Pag e;
}
return this._page;
}
set
{
if (this.OwnerCont rol != null)
{
throw new InvalidOperatio nException();
}
this._page = value;
}
}

Unfortunatelly, Parent property is readonly so cannot interfere with its
value (actually you could using Reflection but it would get nasty). Anyway,
it's gonna work if you create controls dynamically, i.e.
Button btn = new Button();
btn.Name = "btn";
btn.ID= "btn";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter( sw);
btn.RenderContr ol(htw);
Response.Write( sw.ToString());
Response.End();
}

but not if the button was placed on the page.

Regards
--
Milosz
"Josh Carver" wrote:
Hi group,

I'm trying to get the outputted html from some specific controls on a page,
and I'm finding that some controls will render fine, while others will throw
the error that "[control] must be placed inside a form tag with
runat=server." For example, a label control will render fine, but a button
will not. It seems that any control that has a server-side click event will
not accept being rendered in this way. Does anyone know what I can do to
get such controls to render? Here's my code:

aspx snippet:

<form id="f" runat="server">
<div>
<asp:Label ID="control" runat="server" Text="Chage to an asp:Button"
/>
</div>
</form>
..cs snippets:

using System;
using System.Web.UI;
using System.IO;

// ...

using (StringWriter sw = new StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter( sw);
control.RenderC ontrol(htw);
Response.Write( sw.ToString());
Response.End();
}

Thanks for any help.

Joshua C

Jul 9 '07 #2
Thanks a lot for your help, Milosz. I was actually able to get it to work
without loading the control dynamically. I just added:

EnableEventVali dation="false"

to the page directive. And I added this to the code-behind after reading
what you wrote:

public override void VerifyRendering InServerForm(Co ntrol control)
{
return;
}

Thanks again!

Josh
"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plwrot e in message
news:C0******** *************** ***********@mic rosoft.com...
Hi there,

I'm afraid once control is put on the page, its Page & Parent property is
assigned, and here's a statement that prevents control from being
rendered:

bool useSubmitBehavi or = this.UseSubmitB ehavior;
if (this.Page != null)
{
this.Page.Verif yRenderingInSer verForm(this);
}

There is no problem in assigning Page to null, unfortunatelly, if Page is
null, Parent.Page is returned:

public virtual Page Page
{
get
{
if ((this._page == null) && (this.Parent != null))
{
this._page = this.Parent.Pag e;
}
return this._page;
}
set
{
if (this.OwnerCont rol != null)
{
throw new InvalidOperatio nException();
}
this._page = value;
}
}

Unfortunatelly, Parent property is readonly so cannot interfere with its
value (actually you could using Reflection but it would get nasty).
Anyway,
it's gonna work if you create controls dynamically, i.e.
Button btn = new Button();
btn.Name = "btn";
btn.ID= "btn";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter( sw);
btn.RenderContr ol(htw);
Response.Write( sw.ToString());
Response.End();
}

but not if the button was placed on the page.

Regards
--
Milosz
"Josh Carver" wrote:
>Hi group,

I'm trying to get the outputted html from some specific controls on a
page,
and I'm finding that some controls will render fine, while others will
throw
the error that "[control] must be placed inside a form tag with
runat=server ." For example, a label control will render fine, but a
button
will not. It seems that any control that has a server-side click event
will
not accept being rendered in this way. Does anyone know what I can do to
get such controls to render? Here's my code:

aspx snippet:

<form id="f" runat="server">
<div>
<asp:Label ID="control" runat="server" Text="Chage to an
asp:Button"
/>
</div>
</form>
..cs snippets:

using System;
using System.Web.UI;
using System.IO;

// ...

using (StringWriter sw = new StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter( sw);
control.RenderC ontrol(htw);
Response.Write( sw.ToString());
Response.End();
}

Thanks for any help.

Joshua C

Jul 10 '07 #3

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

Similar topics

3
2941
by: doron | last post by:
Hi, I'm tryig to add a LinkButton with events to a custom control (custom control : Datagrid) My CreateChildControls is generating an error (Specified cast is not valid.)
3
2639
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event and this causes control B to be created, when this is done the VIEWSTATE is lost for CONTROL B. In the EVENT that causes CONTROL B to be created I have to set
1
3142
by: sleigh | last post by:
Hello, I'm building a web application that will build a dynamic form based upon questions in a database. This form will have several different sections that consist of a panel containing one to many questions. To keep it simple, I'll describe the basics of what I'm trying to design. I've created a TextBox composite control that consists of a label for
2
2218
by: Juan Romero | last post by:
Hey guys, I am working on a web custom control that basically draws a table (ASP Table) with a few child controls in the cells. I have a command button inside one of these cells. The problem I am running into is that I cannot get the click event of this object handled. I have tried wiring the event with "AddHandler" among other things and that seems not to work neither.
2
4766
by: Tom Bray | last post by:
Ok I am baffled I can not figure out this problem. I am getting the following error: Portal Error - A DropDownList cannot have multiple items selected. Error information Name Value Message A DropDownList cannot have multiple items selected. Source System.Web TargetSite Void RenderContents(System.Web.UI.HtmlTextWriter)
0
1655
by: Earl Teigrob | last post by:
I have created a Custom Control that accepts an array of Objects and uses the contents of those objects to add controls to its child control struture and have them rendered to the display. I also add this array of objects to view state and regenerate the objects on postback using the System.Activator.CreateInstance Method. This all seems to work fine but the viewstate of my dyanically recreated objects is not being restored. What to I need...
5
5133
by: Nathan Sokalski | last post by:
When running a page I am working on, I recieve the following error: Cannot use a leading .. to exit above the top directory. I suspect this has something to do with the problem I posted in a recent posting "HyperLink.NavigateUrl always adds an extra ../ when going to a higher level directory" on Sunday, May 29, 2005 7:33 PM However, I cannot be sure because I cannot view the generated code since it gives the error before anything is...
1
1547
by: estebistec | last post by:
Hello, I've developed a CompositeControl in C# for ASP.NET 2.0. In this control I am simulating Edit and View modes by hiding or showing appropriate controls (setting Control.Visible). Basically I'm simulating the functionality of the DetailsView or a GridView cell, except that my data is dynamic (variable number of fields), so I can't just use those. So I have custom edit and view controls for this dynamic data and I render one of...
2
3578
by: Nigil | last post by:
Hi Guys, Does anyone know how to RenderControl with sub controls? cos I am having issues with that. Following is my code. Inside my UserControls, I have a Repeater. <form method="post" class="Form1" runat="server" id="Form1"> <div id="Main_Div" runat="server">
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.