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

Discussion in 'ASP .Net' started by TS, Jul 2, 2007.

  1. TS

    TS Guest

    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
     
    TS, Jul 2, 2007
    #1
    1. Advertising

  2. 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.
     
    Steven Cheng[MSFT], Jul 3, 2007
    #2
    1. Advertising

  3. TS

    TS Guest

    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]" <> wrote in message
    news:lN%23G%...
    > 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.
    >
    >
    >
    >
    >
    >
     
    TS, Jul 3, 2007
    #3
  4. 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.
     
    Steven Cheng[MSFT], Jul 5, 2007
    #4
  5. TS

    TS Guest

    thanks

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > 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.
    >
    >
    >
    >
    >
    >
    >
    >
    >
     
    TS, Jul 5, 2007
    #5
  6. 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.
     
    Steven Cheng[MSFT], Jul 6, 2007
    #6
    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. Hussein B
    Replies:
    10
    Views:
    447
    Bruno Desthuilliers
    Aug 26, 2008
  2. Ryan Liu
    Replies:
    1
    Views:
    267
    Alexey Smirnov
    Jan 30, 2009
  3. Rob Joosen

    Keep custom property-value in custom rendered control

    Rob Joosen, May 12, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    157
    Rob Joosen
    May 12, 2004
  4. Zarko Gajic

    Custom Attributes (AttributeTargets.Property) and Property Type

    Zarko Gajic, Apr 7, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    454
    Zarko Gajic
    Apr 7, 2005
  5. anon
    Replies:
    0
    Views:
    219
Loading...

Share This Page