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

Discussion in 'ASP .Net' started by John Dalberg, May 9, 2007.

  1. John Dalberg

    John Dalberg Guest

    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
    John Dalberg, May 9, 2007
    #1
    1. Advertising

  2. RE: Why adding runat=server to a div tag throws an exception of type '

    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 <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
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, May 9, 2007
    #2
    1. Advertising

  3. "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]
    John Saunders [MVP], May 9, 2007
    #3
  4. John Dalberg

    John Dalberg Guest

    "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
    John Dalberg, May 9, 2007
    #4
  5. On May 10, 12:44 am, (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
    Alexey Smirnov, May 10, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    0
    Views:
    5,313
    Steve
    Jul 1, 2003
  2. Jim in Arizona

    <div id="help" runat="server"></div>

    Jim in Arizona, Nov 17, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    12,505
    Kevin Spencer
    Nov 17, 2005
  3. Replies:
    0
    Views:
    384
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,754
    Smokey Grindel
    Dec 2, 2006
  5. mark4asp
    Replies:
    1
    Views:
    492
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Apr 3, 2007
Loading...

Share This Page