Why adding runat=server to a div tag throws an exception of type 'System.Web.HttpException in contro

J

John Dalberg

I am looking at a problem which is preventing my code to get a reference to
any asp control inside a div section which has a runat=server attribute.

This can be reproduced in a simple test:
Create a blank webform and add this html inside the <form> section:
<div id="myDiv" runat=server>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>

In the code behind, add this code in the page_load event handler:

foreach (Control c in this.Controls)
{

}

Put a breakpoint on the first curly brace. When the code breaks, look at
the Controls collection in the debugger. One of the controls shows:
{InnerText =
'((System.Web.UI.HtmlControls.HtmlContainerControl)(this.Controls._controls
[1])).InnerText' threw an exception of type 'System.Web.HttpException'}

Inside my loop, I have code to get a reference to the textbox which never
excutes. When I remove the runat=server from the div tag, the exception is
gone.

What am I missing?

TIA.

John Dalberg
 
G

Guest

try putting your controls inside an asp:panel control instead. I believe it
renders a div anyway.
Peter
 
J

John Saunders [MVP]

John Dalberg said:
I am looking at a problem which is preventing my code to get a reference to
any asp control inside a div section which has a runat=server attribute.

This can be reproduced in a simple test:
Create a blank webform and add this html inside the <form> section:
<div id="myDiv" runat=server>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>

In the code behind, add this code in the page_load event handler:

foreach (Control c in this.Controls)
{

}

First, please put quotes around "server" in your div. Second, make sure it
gets into the .designer.cs file: while viewing the markup, click the Design
tab, wait until it's finished loading, then you can click back to Source.
The .designer.cs should then have a field named "myDiv".

Then, loop "foreach (Control c in myDiv.Controls)" and see if one of them is
the text box.

The text box control is not directly within the page's Controls collection.
It should be in the Controls collection of the div.
 
J

John Dalberg

John Saunders said:
First, please put quotes around "server" in your div. Second, make sure
it gets into the .designer.cs file: while viewing the markup, click the
Design tab, wait until it's finished loading, then you can click back to
Source. The .designer.cs should then have a field named "myDiv".

Then, loop "foreach (Control c in myDiv.Controls)" and see if one of them
is the text box.

The text box control is not directly within the page's Controls
collection. It should be in the Controls collection of the div.\

Thanks for your reply.

While runat=server is not wrong, quotes is for standards sake. Plus VS2005
is doing it. I have to manually add the quotes. You can also tell the VS
team to add quotes on all the attributes. This piece doesn't cause any
errors.

myDiv is added correctly in the .designer.cs. Otherwise I would get object
has no reference error in the code behind.

The textbox is in the div collection but the div collection itself is
getting an exception. This exception is only visible inside the debugger
and because of this exception, the foreach ignores the div and moves along
with the rest of the Page's collection. I can't look in the div collection
if the Page can't see the div control.

John Dalberg
 
G

Guest

The textbox is in the div collection but the div collection itself is
getting an exception. This exception is only visible inside the debugger
and because of this exception, the foreach ignores the div and moves along
with the rest of the Page's collection. I can't look in the div collection
if the Page can't see the div control.

John,

to look in the div collection I think you can do the following

foreach (Control c in this.Controls)
{
IterateThroughChildren(c);
....your code here...
}

void IterateThroughChildren(Control parent)
{
foreach (Control c in parent.Controls)
{
Response.Write(c.ClientID);
if (c.Controls.Count > 0)
{
IterateThroughChildren(c);
}
}
}

you can also try to FindControl("myDiv"), and cast it to
HtmlGenericControl.

I'm not sure about the origin of exception, I think, it's something
related to the control's life cycle - need to look in MSDN :)

Hope it helps
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top