How to reference UserControl in server code

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default How to reference UserControl in server code

    I can't seem to figure out how to get a reference to a UserControl in the
    code-behind for the page that contains the control. All the examples I've
    seen show how to pass property values from the containing page's HTML to the
    UserControl but nothing I've seen shows how to reference the UserControl's
    properties (or subcontrols) from the containing page's server-side code.
    Note that I'm not creating the UserControl prgrammatically via LoadControl -
    I'm creating the UserControl declaratively in the page's HTML.

    Thanks in advance for any help.

    -Mark


    Mark Friedman Guest

  2. Similar Questions and Discussions

    1. Event not firing in usercontrol inside usercontrol
      I'm stumped on this problem. I've created a user control that dynamically creates 5 linkbuttons in the CreateChildControls method. Each of these...
    2. Rendering an UserControl inside a mail body without any page object reference
      Hi I would like to use a User Control to render the body of the mail I send. I know how to do that using Page.LoadControl The problem is that my...
    3. using a usercontrol from code behind
      I have a test usercontrol, It works fine from the HTML page of the webform. If i want to manipulate the control (properties, methods) I can get...
    4. Can you share a code behind file with a page and usercontrol?
      Tying not to spaghetti code which seems to be easy to do in .net, im trying to do my main .net html in index.aspx, use repeated .net html in an...
    5. Use LoadControl to load a usercontrol but the webcontrol in the usercontrol can not AutoPostBack
      a uscontrol test.ascx have a dropdownlist web control the dropdownlist's AutoPostBack property is set "true" but when i use...
  3. #2

    Default Re: How to reference UserControl in server code

    I think you can do it using a
    System.Web.UI.HtmlControls.HtmlGenericControl...


    "Mark Friedman" <bingster@yahoo.com> wrote in message
    news:eUiaq5lRDHA.1924@TK2MSFTNGP12.phx.gbl...
    > I can't seem to figure out how to get a reference to a UserControl in the
    > code-behind for the page that contains the control. All the examples I've
    > seen show how to pass property values from the containing page's HTML to
    the
    > UserControl but nothing I've seen shows how to reference the UserControl's
    > properties (or subcontrols) from the containing page's server-side code.
    > Note that I'm not creating the UserControl prgrammatically via
    LoadControl -
    > I'm creating the UserControl declaratively in the page's HTML.
    >
    > Thanks in advance for any help.
    >
    > -Mark
    >
    >

    Kenn Ghannon Guest

  4. #3

    Default Re: How to reference UserControl in server code

    I'm not sure what you mean here, Kenn. Could you be a little more specific?

    -Mark

    "Kenn Ghannon" <kennyg@ameritech.net> wrote in message
    news:tg0Pa.7215$Vx2.3454635@newssvr28.news.prodigy .com...
    > I think you can do it using a
    > System.Web.UI.HtmlControls.HtmlGenericControl...
    >
    >
    > "Mark Friedman" <bingster@yahoo.com> wrote in message
    > news:eUiaq5lRDHA.1924@TK2MSFTNGP12.phx.gbl...
    > > I can't seem to figure out how to get a reference to a UserControl in
    the
    > > code-behind for the page that contains the control. All the examples
    I've
    > > seen show how to pass property values from the containing page's HTML to
    > the
    > > UserControl but nothing I've seen shows how to reference the
    UserControl's
    > > properties (or subcontrols) from the containing page's server-side code.
    > > Note that I'm not creating the UserControl prgrammatically via
    > LoadControl -
    > > I'm creating the UserControl declaratively in the page's HTML.
    > >
    > > Thanks in advance for any help.
    > >
    > > -Mark
    > >
    > >
    >
    >

    Mark Friedman Guest

  5. #4

    Default Re: How to reference UserControl in server code

    How do you know which control is the one you want, since the IDs get
    mangled?

    -Mark

    "Iain" <iainmccoy@optusnet.com.au> wrote in message
    news:247c2a5d.0307092158.5e1f9473@posting.google.c om...
    > "Mark Friedman" <bingster@yahoo.com> wrote in message
    news:<eUiaq5lRDHA.1924@TK2MSFTNGP12.phx.gbl>...
    > > I can't seem to figure out how to get a reference to a UserControl in
    the
    > > code-behind for the page that contains the control. All the examples
    I've
    > > seen show how to pass property values from the containing page's HTML to
    the
    > > UserControl but nothing I've seen shows how to reference the
    UserControl's
    > > properties (or subcontrols) from the containing page's server-side code.
    > > Note that I'm not creating the UserControl prgrammatically via
    LoadControl -
    > > I'm creating the UserControl declaratively in the page's HTML.
    > >
    > > Thanks in advance for any help.
    > >
    > > -Mark
    >
    > I've been having the same problem. To work around it, I have some code
    > that iterates through the controls on the page looking for a control
    > with a particular id, so I have a function that iterates across
    > Page.Controls[1].Controls checking each control to see if it's id is
    > the desired one, and returning it when found. It's a really terrible
    > way to do things, but it seems to be a suitable placeholder until I
    > find out how it's supposed to be done.
    >
    > -Iain

    Mark Friedman Guest

  6. #5

    Default Re: How to reference UserControl in server code

    I discovered the answer to my own question. You just need to add a member
    to your page behind class with the same name as the ID of the UserControl
    and with a type of your UserControl, which is usually the same as the first
    part of the UserControl's ascx file name. For example, if your UserControl
    is defined in Foo.ascx and you place it in Bar.aspx as:

    <uc1:Foo id="Foo1" runat="server"></uc1:Foo>

    then in your code-behind page for Bar.aspx you just need to have:


    Public Class Bar
    Inherits System.Web.UI.Page
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    ...

    ' This must be put in manually, even though it ought to be done
    automatically
    ' by VS.NET when you put the user control on the page, just like it
    did for
    ' the button above
    Protected Foo1 As Foo


    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As
    System.EventArgs) Handles Button1.Click
    Foo1.MyProperty = "Whatever"
    End Sub
    ...

    End Class

    and then you can refer to the properties, etc. of the UserControl.

    Not my comment above that I think that it's a bug that VS.NET doesn't
    automatically put the member variable in there for you the way it does for
    other server controls.

    -Mark

    "Mark Friedman" <bingster@yahoo.com> wrote in message
    news:eUiaq5lRDHA.1924@TK2MSFTNGP12.phx.gbl...
    > I can't seem to figure out how to get a reference to a UserControl in the
    > code-behind for the page that contains the control. All the examples I've
    > seen show how to pass property values from the containing page's HTML to
    the
    > UserControl but nothing I've seen shows how to reference the UserControl's
    > properties (or subcontrols) from the containing page's server-side code.
    > Note that I'm not creating the UserControl prgrammatically via
    LoadControl -
    > I'm creating the UserControl declaratively in the page's HTML.
    >
    > Thanks in advance for any help.
    >
    > -Mark
    >
    >

    Mark Friedman Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139