Finding Controls in User Control

Discussion in 'ASP .Net' started by Fred Chateau, Aug 3, 2007.

  1. Fred Chateau

    Fred Chateau Guest

    I am trying to reference a server control in a user control, from the
    containing page. Is there a way to do that? I have tried
    "UserControl1.FindControl("ControlName")" but I get a null reference.

    --
    Regards,

    Fred Chateau
    fchateauAtComcastDotNet
    Fred Chateau, Aug 3, 2007
    #1
    1. Advertising

  2. Fred Chateau

    Fred Chateau Guest

    I should mention, in case it makes a difference, that I'm loading the user
    control dynamically in Page_Init.

    --
    Regards,

    Fred Chateau
    fchateauAtComcastDotNet


    "Fred Chateau" <fchateau@127.0.0.1> wrote in message
    news:%23MY8Q$...
    >I am trying to reference a server control in a user control, from the
    >containing page. Is there a way to do that? I have tried
    >"UserControl1.FindControl("ControlName")" but I get a null reference.
    >
    > --
    > Regards,
    >
    > Fred Chateau
    > fchateauAtComcastDotNet
    >
    >
    Fred Chateau, Aug 3, 2007
    #2
    1. Advertising

  3. Fred Chateau

    Guest

    UserControl1.FindControl() will search in your user control, try
    searching in the page object?
    , Aug 3, 2007
    #3
  4. Hi Fred,

    This is not proper way of solving the problem. It may not work because if
    the control is nested within a container control, you have to call
    FindControl recursively or use "$" separator when passing the control id to
    FindControl method. There's another, much more effective and cleaner way of
    doing this type of task, by exposing a property from the user control, which
    then can be set or read by the containing page. Let's imagine your user
    control has got a text box for extering a user's first name. Containing page
    does not know the internal structure of the user control (well it shouldn't
    know), therefore it's better to create a property to abstract the First Name:

    -- user control --

    public string FirstName
    {
    get
    {
    return txtFirstName.Text;
    }
    set
    {
    txtFirstName.Text = value;
    }
    }


    now, on the containing page set / get the value of the property,
    1. declaratively in the aspx code
    <uc1:MyCustomControl runat="server" id="myControl" FirstName="Fred"/>
    2. or programatically in the code behind:

    protected void btn_Click(object sender, EventArgs e)
    {
    SaveUserDataToDataBase(myControl.FirstName);
    }

    Hope this helps
    --
    Milosz


    "Fred Chateau" wrote:

    > I should mention, in case it makes a difference, that I'm loading the user
    > control dynamically in Page_Init.
    >
    > --
    > Regards,
    >
    > Fred Chateau
    > fchateauAtComcastDotNet
    >
    >
    > "Fred Chateau" <fchateau@127.0.0.1> wrote in message
    > news:%23MY8Q$...
    > >I am trying to reference a server control in a user control, from the
    > >containing page. Is there a way to do that? I have tried
    > >"UserControl1.FindControl("ControlName")" but I get a null reference.
    > >
    > > --
    > > Regards,
    > >
    > > Fred Chateau
    > > fchateauAtComcastDotNet
    > >
    > >

    >
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Aug 4, 2007
    #4
  5. Fred Chateau

    Fred Chateau Guest

    I still have the same problem. When I load the User Control dynamically, I
    can't seem to get a reference to it.

    --
    Regards,

    Fred Chateau
    fchateauAtComcastDotNet


    "Milosz Skalecki [MCAD]" <> wrote in message
    news:...
    > Hi Fred,
    >
    > This is not proper way of solving the problem. It may not work because if
    > the control is nested within a container control, you have to call
    > FindControl recursively or use "$" separator when passing the control id
    > to
    > FindControl method. There's another, much more effective and cleaner way
    > of
    > doing this type of task, by exposing a property from the user control,
    > which
    > then can be set or read by the containing page. Let's imagine your user
    > control has got a text box for extering a user's first name. Containing
    > page
    > does not know the internal structure of the user control (well it
    > shouldn't
    > know), therefore it's better to create a property to abstract the First
    > Name:
    >
    > -- user control --
    >
    > public string FirstName
    > {
    > get
    > {
    > return txtFirstName.Text;
    > }
    > set
    > {
    > txtFirstName.Text = value;
    > }
    > }
    >
    >
    > now, on the containing page set / get the value of the property,
    > 1. declaratively in the aspx code
    > <uc1:MyCustomControl runat="server" id="myControl" FirstName="Fred"/>
    > 2. or programatically in the code behind:
    >
    > protected void btn_Click(object sender, EventArgs e)
    > {
    > SaveUserDataToDataBase(myControl.FirstName);
    > }
    >
    > Hope this helps
    > --
    > Milosz
    >
    >
    > "Fred Chateau" wrote:
    >
    >> I should mention, in case it makes a difference, that I'm loading the
    >> user
    >> control dynamically in Page_Init.
    >>
    >> --
    >> Regards,
    >>
    >> Fred Chateau
    >> fchateauAtComcastDotNet
    >>
    >>
    >> "Fred Chateau" <fchateau@127.0.0.1> wrote in message
    >> news:%23MY8Q$...
    >> >I am trying to reference a server control in a user control, from the
    >> >containing page. Is there a way to do that? I have tried
    >> >"UserControl1.FindControl("ControlName")" but I get a null reference.
    >> >
    >> > --
    >> > Regards,
    >> >
    >> > Fred Chateau
    >> > fchateauAtComcastDotNet
    >> >
    >> >

    >>
    >>
    >>
    Fred Chateau, Aug 4, 2007
    #5
  6. Fred Chateau

    Fred Chateau Guest

    I figured it out.

    PlaceholderControl.Controls[0].FindControl("ControlName")

    --
    Regards,

    Fred Chateau
    fchateauAtComcastDotNet


    "Fred Chateau" <fchateau@127.0.0.1> wrote in message
    news:...
    >I still have the same problem. When I load the User Control dynamically, I
    >can't seem to get a reference to it.
    >
    > --
    > Regards,
    >
    > Fred Chateau
    > fchateauAtComcastDotNet
    >
    >
    > "Milosz Skalecki [MCAD]" <> wrote in message
    > news:...
    >> Hi Fred,
    >>
    >> This is not proper way of solving the problem. It may not work because if
    >> the control is nested within a container control, you have to call
    >> FindControl recursively or use "$" separator when passing the control id
    >> to
    >> FindControl method. There's another, much more effective and cleaner way
    >> of
    >> doing this type of task, by exposing a property from the user control,
    >> which
    >> then can be set or read by the containing page. Let's imagine your user
    >> control has got a text box for extering a user's first name. Containing
    >> page
    >> does not know the internal structure of the user control (well it
    >> shouldn't
    >> know), therefore it's better to create a property to abstract the First
    >> Name:
    >>
    >> -- user control --
    >>
    >> public string FirstName
    >> {
    >> get
    >> {
    >> return txtFirstName.Text;
    >> }
    >> set
    >> {
    >> txtFirstName.Text = value;
    >> }
    >> }
    >>
    >>
    >> now, on the containing page set / get the value of the property,
    >> 1. declaratively in the aspx code
    >> <uc1:MyCustomControl runat="server" id="myControl" FirstName="Fred"/>
    >> 2. or programatically in the code behind:
    >>
    >> protected void btn_Click(object sender, EventArgs e)
    >> {
    >> SaveUserDataToDataBase(myControl.FirstName);
    >> }
    >>
    >> Hope this helps
    >> --
    >> Milosz
    >>
    >>
    >> "Fred Chateau" wrote:
    >>
    >>> I should mention, in case it makes a difference, that I'm loading the
    >>> user
    >>> control dynamically in Page_Init.
    >>>
    >>> --
    >>> Regards,
    >>>
    >>> Fred Chateau
    >>> fchateauAtComcastDotNet
    >>>
    >>>
    >>> "Fred Chateau" <fchateau@127.0.0.1> wrote in message
    >>> news:%23MY8Q$...
    >>> >I am trying to reference a server control in a user control, from the
    >>> >containing page. Is there a way to do that? I have tried
    >>> >"UserControl1.FindControl("ControlName")" but I get a null reference.
    >>> >
    >>> > --
    >>> > Regards,
    >>> >
    >>> > Fred Chateau
    >>> > fchateauAtComcastDotNet
    >>> >
    >>> >
    >>>
    >>>
    >>>

    >
    >
    Fred Chateau, Aug 4, 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. =?Utf-8?B?REo=?=
    Replies:
    2
    Views:
    470
    =?Utf-8?B?REo=?=
    Dec 1, 2005
  2. Mad Scientist Jr
    Replies:
    0
    Views:
    635
    Mad Scientist Jr
    Mar 22, 2006
  3. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    543
    Jonathan Wood
    Feb 2, 2010
  4. Faizan Ahmed
    Replies:
    1
    Views:
    282
    John Saunders
    Jan 4, 2005
  5. Faizan Ahmed
    Replies:
    1
    Views:
    362
    John Saunders
    Jan 4, 2005
Loading...

Share This Page