473,666 Members | 2,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why adding runat=server to a div tag throws an exception of type 'System.Web.Htt pException 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.U I.HtmlControls. HtmlContainerCo ntrol)(this.Con trols._controls
[1])).InnerText' threw an exception of type 'System.Web.Htt pException'}

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 15015
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.U I.HtmlControls. HtmlContainerCo ntrol)(this.Con trols._controls
[1])).InnerText' threw an exception of type 'System.Web.Htt pException'}

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******** ***********@new sreader.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.saunder s at trizetto.comwro te:
"John Dalberg" <no****@nospam. ssswrote in message
news:20******** ***********@new sreader.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.s ss (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)
{
IterateThroughC hildren(c);
....your code here...
}

void IterateThroughC hildren(Control parent)
{
foreach (Control c in parent.Controls )
{
Response.Write( c.ClientID);
if (c.Controls.Cou nt 0)
{
IterateThroughC hildren(c);
}
}
}

you can also try to FindControl("my Div"), and cast it to
HtmlGenericCont rol.

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
1662
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 asp:Label does not need to have the form runat=server and that asp:Textbox requires it. Thanks, Gar
4
9952
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 'checkbox' control is just an example. Control 'CheckBox1' of type 'CheckBox' must be placed inside a form tag with runat=server. Description: An unhandled exception occurred during the execution of the current web request. Please review the...
3
3767
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" PlaceHolderLink.Controls.Add(linkbutton1) but how do i add runat="server" as in html code in code behind, im using vb.net
3
4175
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 export the data to excel I get the following error message: Control 'RoomReservationReport1_dgReservations__ctl2__ctl0' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server. My form tag is located in my web form...
4
1395
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 by having the control hidden and grabbing it's innerHTML to a new container in the HTML. i also change controls ID suffix in order to be able to track added controls like in old HTML.
0
3420
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 will do some database updates, then I will use reflection to call a method in the parent (this.page) to load the new user control. Its at this point after it loads the new user control that it throws the error "The control must be placed inside...
4
13357
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 just seen an example where javascript can access server side controls. Now javascript is about all things CLIENT, so how can I access server controls from my javascript, here is one such example <%@ Page Language="C#" AutoEventWireup="tue"...
1
6280
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 Can anyone please help. Thanks
1
1269
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 But if I put it asks throws up errors like ddl not declared. Can aybody please help me on this?
0
8785
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
8560
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,...
0
8644
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6200
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
5671
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
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
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.