473,320 Members | 1,713 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,320 software developers and data experts.

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

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 <formsection:
<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
May 9 '07 #1
4 14956
try putting your controls inside an asp:Panel 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 <formsection:
<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
May 9 '07 #2
"John Dalberg" <no****@nospam.ssswrote in message
news:20*******************@newsreader.com...
>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 <formsection:
<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]
May 9 '07 #3
"John Saunders [MVP]" <john.saunders at trizetto.comwrote:
"John Dalberg" <no****@nospam.ssswrote in message
news:20*******************@newsreader.com...
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 <formsection:
<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
May 9 '07 #4
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

May 10 '07 #5

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

Similar topics

1
by: Gary Bagen | last post by:
Hello, I am working on a page transition scheme and was wondering what the quickest way is to find out which ASP.NET controls need to be inside a form with runat=server. I've determined that...
4
by: Matthew Louden | last post by:
It happend to me more than once. When I create web controls or move the positions in VS.NET, I encountered the following run-time errors: It doesn't matter what controls I create, the following...
3
by: Ferryandi | last post by:
hi, i have created a button link like this and attach it to placeholder in code behind linkbutton1 = New LinkButton linkbutton1.Text = "Link Button linkbutton1.ID = "Link1"...
3
by: Russ | last post by:
I have a usercontrol that is loaded by a webform. The usercontrol populates a datagrid which users need the capability to export data from the grid to Excel. The problem is that when I attempt to...
4
by: z f | last post by:
hi, tough one? for me it is currently. i have a user control that contains other controls like text boxes. in the client i need to dynamically add the user control using DHTML. i achive this...
0
by: davidr | last post by:
Hi, I have a panel that I load user Control in no problem. The problem arrises when I do a post back on one of these user controls. I have button it does a click event. In this click event I...
4
by: Erland | last post by:
Hello everyone, I am have couple of bad concepts and would like to clear things up. As I understand ASP.NET server controls contain runat=server tag and they only run on the server. But I've...
1
by: waihung.wee | last post by:
Hi I got the following error in my xxx.cs file (code behind file) According to the error message, I should add a form tag in the file. But form tag should not appear in a code behind file ...
1
by: ckb | last post by:
Hi, I have a webservice on my asp.net page and I have some dropdown boxes that get populated using the webservice. I have created the droprdowns as simple html controls The javascript code is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.