System.Web.HttpException FindContol exception

J

Jim Butler

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
 
S

Steven Cheng[MSFT]

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.)
 
J

Jim Butler

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
 
S

Steven Cheng[MSFT]

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.)
 
J

Jim Butler

Thanks for the help,

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

jim
 
S

Steven Cheng[MSFT]

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top