System.Web.HttpException FindContol exception

Discussion in 'ASP .Net Web Controls' started by Jim Butler, Apr 27, 2006.

  1. Jim Butler

    Jim Butler Guest

    We have a set of web server controls that inherit directly from their parent
    web server control and implement INamingContainer. We need our code to be
    508 compliant but are having trouble with the FindControl method in
    AddAttributesToRender does not find the AssociatedControlID. Our test label
    class is completely devoid of custom [rendering] code. We use the
    INamingContainer within our web server controls for unique id generation for
    those controls when nested within user controls and so fourth. Problem when
    the code is rendered, an Exception is generated like the one below.

    System.Web.HttpException: Unable to find control with id 'hospital_id2' that
    is associated with the Label 'lbl_hospital_id2'.
    at System.Web.UI.WebControls.Label.AddAttributesToRender(HtmlTextWriter
    writer)
    at System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter
    writer)
    at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
    at .... for brevity

    From this aspx code
    <form runat="Server">
    <bl:TestLabel id="lbl_hospital_id2" Text="Hello World22"
    AssociatedControlID="hospital_id2" runat="server" />
    <bl:TestLabel id="hospital_id2" Text="Hello World2222" runat="server" />
    </form>

    Public Class TestLabel
    Inherits System.Web.UI.WebControls.Label
    Implements System.Web.UI.INamingContainer

    Private Sub TestLabel_PreRender(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.PreRender
    Dim x As String = ""
    End Sub
    End Class


    Thanks in advance,

    Jim Butler
     
    Jim Butler, Apr 27, 2006
    #1
    1. Advertising

  2. Jim Butler

    Jim Butler Guest

    Forgot to mention this is ASP.Net 2.0 on windows server 2003....

    "Jim Butler" <> wrote in message
    news:...
    > We have a set of web server controls that inherit directly from their
    > parent web server control and implement INamingContainer. We need our
    > code to be 508 compliant but are having trouble with the FindControl
    > method in AddAttributesToRender does not find the AssociatedControlID.
    > Our test label class is completely devoid of custom [rendering] code. We
    > use the INamingContainer within our web server controls for unique id
    > generation for those controls when nested within user controls and so
    > fourth. Problem when the code is rendered, an Exception is generated like
    > the one below.
    >
    > System.Web.HttpException: Unable to find control with id 'hospital_id2'
    > that is associated with the Label 'lbl_hospital_id2'.
    > at System.Web.UI.WebControls.Label.AddAttributesToRender(HtmlTextWriter
    > writer)
    > at System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter
    > writer)
    > at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
    > at .... for brevity
    >
    > From this aspx code
    > <form runat="Server">
    > <bl:TestLabel id="lbl_hospital_id2" Text="Hello World22"
    > AssociatedControlID="hospital_id2" runat="server" />
    > <bl:TestLabel id="hospital_id2" Text="Hello World2222" runat="server"
    > />
    > </form>
    >
    > Public Class TestLabel
    > Inherits System.Web.UI.WebControls.Label
    > Implements System.Web.UI.INamingContainer
    >
    > Private Sub TestLabel_PreRender(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.PreRender
    > Dim x As String = ""
    > End Sub
    > End Class
    >
    >
    > Thanks in advance,
    >
    > Jim Butler
    >
     
    Jim Butler, Apr 27, 2006
    #2
    1. Advertising

  3. Hi Jim,

    Welcome to the ASP.NET newsgroup.

    From your description, I understand you're developing a custom label
    control which will use the Render out some custom attributes, and you're
    using the "AddAttributesToRender" method to add some certain attributes
    into the control tag, and one of them is a string value reference to other
    control on the page. However, when you try using the FindControl method to
    locate other control on the page, you're getting error, correct?

    Based on my understanding, the FindControl method will only search the
    control (by id) in the current NamingContainer.

    #Control.FindControl Method (String)
    http://msdn2.microsoft.com/en-us/library/486wc64h.aspx

    And as for your scenario, your custom control has implemented the
    INamingContainer interface, then, the FindControl method of itself will
    only be able to find control in its own child control collection. And for
    other controls at the same level (in the same parent NamingContainer), it
    will not be able to find them. We have to call the FindControl on the
    parent NamingContainer, like:

    ===============================
    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {

    writer.AddAttribute(HtmlTextWriterAttribute.Id, ID);


    if (!string.IsNullOrEmpty(AssociatedControlID))
    {
    Control ctrl =
    this.NamingContainer.FindControl(AssociatedControlID);

    writer.AddAttribute(HtmlTextWriterAttribute.For,
    ctrl.ClientID);
    }

    }
    ============================

    Hope this helps.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Apr 28, 2006
    #3
  4. Jim Butler

    Jim Butler Guest

    Steven,

    Thanks for the response. We have implemented your suggestion and it works
    beautifully. However, we are still somewhat confused as to what may be the
    "side effects" of this. We couldn't come up with a good reason why ms's
    initial implementation of this would not have been coded that way in the
    first place, which would lead us to understand what may break by doing it as
    you have suggested below. After decompiling the built in method, we are
    concerned we might be losing added functionality, such as AccesKey,
    disabled, tabIndex and so on. The problem with trying to use the one's
    listed is that in the inherited class we don't seem to have access to all
    the variables to duplicate the code.

    Any thoughts?

    Thanks,

    jim



    "Steven Cheng[MSFT]" <> wrote in message
    news:eek:...
    > Hi Jim,
    >
    > Welcome to the ASP.NET newsgroup.
    >
    > From your description, I understand you're developing a custom label
    > control which will use the Render out some custom attributes, and you're
    > using the "AddAttributesToRender" method to add some certain attributes
    > into the control tag, and one of them is a string value reference to other
    > control on the page. However, when you try using the FindControl method to
    > locate other control on the page, you're getting error, correct?
    >
    > Based on my understanding, the FindControl method will only search the
    > control (by id) in the current NamingContainer.
    >
    > #Control.FindControl Method (String)
    > http://msdn2.microsoft.com/en-us/library/486wc64h.aspx
    >
    > And as for your scenario, your custom control has implemented the
    > INamingContainer interface, then, the FindControl method of itself will
    > only be able to find control in its own child control collection. And for
    > other controls at the same level (in the same parent NamingContainer), it
    > will not be able to find them. We have to call the FindControl on the
    > parent NamingContainer, like:
    >
    > ===============================
    > protected override void AddAttributesToRender(HtmlTextWriter writer)
    > {
    >
    > writer.AddAttribute(HtmlTextWriterAttribute.Id, ID);
    >
    >
    > if (!string.IsNullOrEmpty(AssociatedControlID))
    > {
    > Control ctrl =
    > this.NamingContainer.FindControl(AssociatedControlID);
    >
    > writer.AddAttribute(HtmlTextWriterAttribute.For,
    > ctrl.ClientID);
    > }
    >
    > }
    > ============================
    >
    > Hope this helps.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Community Support
    >
    >
    > ==================================================
    >
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    >
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
     
    Jim Butler, Apr 28, 2006
    #4
  5. Thanks for your response Jim,

    As for the INamingContainer, and FindControl method, it is the fixed by
    design behavior that the control searching will only be performed within
    the current NamingContainer scope.

    As for the further concerns you mentioned:
    =======================
    After decompiling the built in method, we are
    concerned we might be losing added functionality, such as AccesKey,
    disabled, tabIndex and so on. The problem with trying to use the one's
    listed is that in the inherited class we don't seem to have access to all
    the variables to duplicate the code.
    ======================

    Do you means the reuse of those existing properties such as AccessKey,
    TableIndex, Font ... on the WebControl class? Generally for those common
    properties or reusable ones, the classes will mark them as "public virtual"
    so that our dervied class can override them. And yes, some variables or
    properties, methods are marked as internal, that means the library designer
    do not want it open to other developers. In such scenario, we could not but
    implement our own according to the code logic or we can create a own code
    library to reuse them just like the framework fundamental library.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], May 1, 2006
    #5
  6. Jim Butler

    Jim Butler Guest

    Thanks for the help,

    sorry i didn't get back to you faster, we just removed implementing
    INamingContainer in our label class.

    jim

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Thanks for your response Jim,
    >
    > As for the INamingContainer, and FindControl method, it is the fixed by
    > design behavior that the control searching will only be performed within
    > the current NamingContainer scope.
    >
    > As for the further concerns you mentioned:
    > =======================
    > After decompiling the built in method, we are
    > concerned we might be losing added functionality, such as AccesKey,
    > disabled, tabIndex and so on. The problem with trying to use the one's
    > listed is that in the inherited class we don't seem to have access to all
    > the variables to duplicate the code.
    > ======================
    >
    > Do you means the reuse of those existing properties such as AccessKey,
    > TableIndex, Font ... on the WebControl class? Generally for those common
    > properties or reusable ones, the classes will mark them as "public
    > virtual"
    > so that our dervied class can override them. And yes, some variables or
    > properties, methods are marked as internal, that means the library
    > designer
    > do not want it open to other developers. In such scenario, we could not
    > but
    > implement our own according to the code logic or we can create a own code
    > library to reuse them just like the framework fundamental library.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Community Support
    >
    >
    > ==================================================
    >
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    >
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
     
    Jim Butler, May 5, 2006
    #6
  7. Thanks for the followup Jim.

    I'm glad to be of assistance and please always feel free to post here when
    there is anything we can help.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], May 8, 2006
    #7
    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.

Share This Page