473,411 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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

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
Jul 2 '07 #1
5 1510
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/subscripti...ult.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/subscripti...t/default.aspx.

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


Jul 3 '07 #2
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?

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:lN******************@TK2MSFTNGHUB02.phx.gbl.. .
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/subscripti...ult.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/subscripti...t/default.aspx.

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



Jul 3 '07 #3
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.



Jul 5 '07 #4
TS
thanks

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:SN**************@TK2MSFTNGHUB02.phx.gbl...
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.
>




Jul 5 '07 #5
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.

Jul 6 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
2
by: Laurent Bugnion | last post by:
Hi, I like to develop custom controls for a number of webpages. These controls are often customizable, so that they can be reused in a number of situations. My question is: What is the best...
0
by: ChopStickr | last post by:
I have a custom control that is embedded (using the object tag) in an html document. The control takes a path to a local client ini file. Reads the file. Executes the program specified in...
9
by: =?Utf-8?B?VGVycnk=?= | last post by:
Think it is great the way that you can set up a datsource, value member, and display member for a combobox, and map a 'code' to a 'description' and back again by binding the combobox to a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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,...
0
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...
0
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...

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.