Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Why adding runat=server to a div tag throws an exception of type 'System.Web.HttpException in controls collection?

Reply
Thread Tools

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

 
 
John Dalberg
Guest
Posts: n/a
 
      05-09-2007
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      05-09-2007
try putting your controls inside an aspanel control instead. I believe it
renders a div anyway.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




"John Dalberg" wrote:

> 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
>

 
Reply With Quote
 
 
 
 
John Saunders [MVP]
Guest
Posts: n/a
 
      05-09-2007
"John Dalberg" <> wrote in message
news:20070509135949.045$...
>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.
--
John Saunders [MVP]


 
Reply With Quote
 
John Dalberg
Guest
Posts: n/a
 
      05-09-2007
"John Saunders [MVP]" <john.saunders at trizetto.com> wrote:
> "John Dalberg" <> wrote in message
> news:20070509135949.045$...
> >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.\


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
 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      05-10-2007
On May 10, 12:44 am, nos...@nospam.sss (John Dalberg) wrote:
> 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

 
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
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Select text within a div tag by clicking on content of div tag or a button? visu Javascript 4 11-22-2006 06:25 AM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM
Select text within a div tag by clicking on content of div tag? M Wells Javascript 0 10-06-2004 11:04 AM
Exception Exception is not compatible with throws clause in Runnable.run() Chris Miller Java 4 11-22-2003 03:11 PM



Advertisments