question on best way to get a control's property from custom control

T

TS

I have a custom textbox that i need to access a label's text property. the
label and textbox are 2 separate controls that will be on my page. Inside my
custom control i want to have access to this label's text. I was thinking of
having a property on my textbox that was something like LabelControlID. the
textbox could then do a .Findcontrol on its parent control to find this
label and access its text. i thought that this might be an expensive
operation using Findcontrol (has to do some kind of search algorithm to find
the control) and i don't want to burden the processor, especially if theres
200 textboxes on a page.

Don't know if it fits in here, but I saw the IDReferencePropertyAttribute
and wondered if it was applicable.

Also, I was wondering if i could make this attribute required like the
following?
[IsRequired=true]
public bool LabelControlID{
get { return _LabelControlID; }
set { _LabelControlID= value; }
}

thanks
 
S

Steven Cheng[MSFT]

Hi TS,

From your description, you're developing a custom web server control and
the custom control will need to get reference to another control(on the
same page) at runtime so as to retrieve some property value, correct?

Based on my experience, for this scenario, you can consider use the
following means to locate external control:

** If the control is at the top level of the aspx page, you can directly
use "Page.Form.FindControl" to locate the control in the HtmlForm's control
collection

** Or if the control will always be put in the same
container(NamingContainer) of your customer control, you can use the
following code to locate the external control within the same parent
namingcontainer:

this.NamingContainer.FindControl("controlid");

#note that what you need to use is the "ID" (not clientID or uniqueID) of
the target control you want to locate

Actually, generally, you do not need to loop all the controls collection on
the page(every controls) and it's not realistic, ony the Namingcontainer is
enough, just like what the Validator controls do(find the ones they'll
validate).

In addition, for the "IDReferencePropertyAttribute", it is mainly used for
design-time UI to help choose a control from the same namingcontainer, but
it won't help at runtime.

Hope this helps. If there is any further questions on this, please feel
free to let me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



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

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

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


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

TS

What is the internal implementation of .Findcontrol, does it do a binary
tree search?

how expensive is it, say compared to using a 1 regular expression validator
or reflection once?
 
S

Steven Cheng[MSFT]

Hi TS,

Here is the Control.FindControl's implemention code logic (picked from
reflector). You can see that it will always use the NamingContainer of
current control to look for the target control. If no parent
namingContainer, it will parse ID to to look through down level path. And
it will choose a certain path and go through it rather than recursively
loop through all the control hierarchy(which is quite expensive):

=========================
protected virtual Control FindControl(string id, int pathOffset)
{
string text;
this.EnsureChildControls();
if (!this.flags[0x80])
{
Control namingContainer = this.NamingContainer;
if (namingContainer != null)
{
return namingContainer.FindControl(id, pathOffset);
}
return null;
}
if (this.HasControls() && (this._occasionalFields.NamedControls ==
null))
{
this.EnsureNamedControlsTable();
}
if ((this._occasionalFields == null) ||
(this._occasionalFields.NamedControls == null))
{
return null;
}
char[] anyOf = new char[] { '$', ':' };
int num = id.IndexOfAny(anyOf, pathOffset);
if (num == -1)
{
text = id.Substring(pathOffset);
return (this._occasionalFields.NamedControls[text] as Control);
}
text = id.Substring(pathOffset, num - pathOffset);
Control control2 = this._occasionalFields.NamedControls[text] as
Control;
if (control2 == null)
{
return null;
}
return control2.FindControl(id, num + 1);
}
==============================

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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

Steven Cheng[MSFT]

You're welcome.

If there is any other questions I can help, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top