How to pass data to user controls?

Discussion in 'ASP .Net' started by Frank Rizzo, Mar 5, 2004.

  1. Frank Rizzo

    Frank Rizzo Guest

    I've got a number of user controls on the web page. How can I pass some
    data to it? I don't see where the user control is instantiated in the
    page code-behind page.

    Thanks.
    Frank Rizzo, Mar 5, 2004
    #1
    1. Advertising

  2. Frank Rizzo

    darrel Guest

    > I've got a number of user controls on the web page. How can I pass some
    > data to it? I don't see where the user control is instantiated in the
    > page code-behind page.


    I'm a complete newbie to .NET, but are you asking how to pass data via a
    Query string of via it's parent aspx page? If so, I can maybe help with
    that...

    -Darrel
    darrel, Mar 5, 2004
    #2
    1. Advertising

  3. Do you mean passing information in the constructor? You cannot since
    ASP.NET instantiates the control for you and binds the tag with some
    code-behind variable.

    You can however, use properties and methods to pass information in after it
    is created, maybe in the Page_Load event. Or you can set attributes on the
    page that get translated into property calls that ASP.NET sets.

    You should at least have a protected variable in your code-behind that is
    named that same as the tag on the page.

    "Frank Rizzo" <> wrote in message
    news:#...
    > I've got a number of user controls on the web page. How can I pass some
    > data to it? I don't see where the user control is instantiated in the
    > page code-behind page.
    >
    > Thanks.
    Peter Rilling, Mar 5, 2004
    #3
  4. Frank Rizzo

    Frank Rizzo Guest

    Peter Rilling wrote:
    > Do you mean passing information in the constructor? You cannot since
    > ASP.NET instantiates the control for you and binds the tag with some
    > code-behind variable.


    Either via the constructor or via properties. The problem is that the
    UserControl that I placed on the page is not instantiated in the
    code-behind file like a button control would be. So I am not sure about
    how to even reference it.

    For instance, the UserControl is called AddComments and my web page is
    main.aspx.cs

    I can't find anywhere in main.aspx.cs where AddComments is instantiated.
    I can instantiate it manually like

    AddComments oComment = new AddComments(...)
    But ASP.NET doesn't seem to take into account anything I do with the
    UserControl.

    I guess, I need to have a reference to the instance of the UC that's in
    use by ASP.NET.

    > You can however, use properties and methods to pass information in after it
    > is created, maybe in the Page_Load event. Or you can set attributes on the
    > page that get translated into property calls that ASP.NET sets.


    Could you give me an example of this?

    >
    > You should at least have a protected variable in your code-behind that is
    > named that same as the tag on the page.
    >
    > "Frank Rizzo" <> wrote in message
    > news:#...
    >
    >>I've got a number of user controls on the web page. How can I pass some
    >>data to it? I don't see where the user control is instantiated in the
    >>page code-behind page.
    >>
    >>Thanks.

    >
    >
    >
    Frank Rizzo, Mar 6, 2004
    #4
  5. Frank Rizzo

    Kunal Guest

    You can manipulate your User Controls if you add them programatically. (Note
    the code is for demo purposes only)

    Lets say you create a User Control, lets call it "sc.ascx".
    This control has a Label called Label1
    Set the text to "SomeLabel".
    Save the Control.
    Don't drag it on to your web form.

    Now open up a WebForm, and on the Page_Load , add the following code:

    Control c = LoadControl("sc.ascx");
    this.Controls.Add(c);
    Label lbl = new Label();
    lbl = (Label)c.FindControl("Label1");
    lbl.Text = "SomeOtherLabel";

    This will show your user Control on your web form when you run your web
    application, with the text modified.

    I don't know if this is your intent, however it is possible.

    --
    -------------------------------------------------------
    http://www.qnal.net/inkabletype



    "Frank Rizzo" <> wrote in message
    news:uBeYz$...
    > Peter Rilling wrote:
    > > Do you mean passing information in the constructor? You cannot since
    > > ASP.NET instantiates the control for you and binds the tag with some
    > > code-behind variable.

    >
    > Either via the constructor or via properties. The problem is that the
    > UserControl that I placed on the page is not instantiated in the
    > code-behind file like a button control would be. So I am not sure about
    > how to even reference it.
    >
    > For instance, the UserControl is called AddComments and my web page is
    > main.aspx.cs
    >
    > I can't find anywhere in main.aspx.cs where AddComments is instantiated.
    > I can instantiate it manually like
    >
    > AddComments oComment = new AddComments(...)
    > But ASP.NET doesn't seem to take into account anything I do with the
    > UserControl.
    >
    > I guess, I need to have a reference to the instance of the UC that's in
    > use by ASP.NET.
    >
    > > You can however, use properties and methods to pass information in after

    it
    > > is created, maybe in the Page_Load event. Or you can set attributes on

    the
    > > page that get translated into property calls that ASP.NET sets.

    >
    > Could you give me an example of this?
    >
    > >
    > > You should at least have a protected variable in your code-behind that

    is
    > > named that same as the tag on the page.
    > >
    > > "Frank Rizzo" <> wrote in message
    > > news:#...
    > >
    > >>I've got a number of user controls on the web page. How can I pass some
    > >>data to it? I don't see where the user control is instantiated in the
    > >>page code-behind page.
    > >>
    > >>Thanks.

    > >
    > >
    > >
    Kunal, Mar 6, 2004
    #5
  6. If I'm understanding you correctly, you need to do as suggested in Peter's note: declare a protected variable--e.g. protected withevents MyInstance as MyUserControlClass. Once you do that, from the page itself you can get at the properties, including the contained controls, etc

    ----- Frank Rizzo wrote: ----

    I've got a number of user controls on the web page. How can I pass some
    data to it? I don't see where the user control is instantiated in the
    page code-behind page

    Thanks
    =?Utf-8?B?QmlsbCBCb3Jn?=, Mar 6, 2004
    #6
  7. Frank Rizzo

    John Amick Guest

    If I understand you correctly, I believe all you need to do is declare the
    control in your code-behind page. You will then be able to reference it in
    code. Even though you've already added the control in design mode, you
    should still be able to declare and reference it like this:

    public class MyWebForm : System.Web.UI.Page
    {
    protected AddComments oAddComments;
    ...
    oAddComments.MyProperty = "Something";
    ...
    }

    HTH,

    John


    "Frank Rizzo" <> wrote in message
    news:uBeYz$...
    > Peter Rilling wrote:
    > > Do you mean passing information in the constructor? You cannot since
    > > ASP.NET instantiates the control for you and binds the tag with some
    > > code-behind variable.

    >
    > Either via the constructor or via properties. The problem is that the
    > UserControl that I placed on the page is not instantiated in the
    > code-behind file like a button control would be. So I am not sure about
    > how to even reference it.
    >
    > For instance, the UserControl is called AddComments and my web page is
    > main.aspx.cs
    >
    > I can't find anywhere in main.aspx.cs where AddComments is instantiated.
    > I can instantiate it manually like
    >
    > AddComments oComment = new AddComments(...)
    > But ASP.NET doesn't seem to take into account anything I do with the
    > UserControl.
    >
    > I guess, I need to have a reference to the instance of the UC that's in
    > use by ASP.NET.
    >
    > > You can however, use properties and methods to pass information in after

    it
    > > is created, maybe in the Page_Load event. Or you can set attributes on

    the
    > > page that get translated into property calls that ASP.NET sets.

    >
    > Could you give me an example of this?
    >
    > >
    > > You should at least have a protected variable in your code-behind that

    is
    > > named that same as the tag on the page.
    > >
    > > "Frank Rizzo" <> wrote in message
    > > news:#...
    > >
    > >>I've got a number of user controls on the web page. How can I pass some
    > >>data to it? I don't see where the user control is instantiated in the
    > >>page code-behind page.
    > >>
    > >>Thanks.

    > >
    > >
    > >
    John Amick, Mar 6, 2004
    #7
  8. Frank Rizzo

    Frank Rizzo Guest

    John Amick wrote:
    > If I understand you correctly, I believe all you need to do is declare the
    > control in your code-behind page. You will then be able to reference it in
    > code. Even though you've already added the control in design mode, you
    > should still be able to declare and reference it like this:
    >
    > public class MyWebForm : System.Web.UI.Page
    > {
    > protected AddComments oAddComments;
    > ...
    > oAddComments.MyProperty = "Something";
    > ...
    > }



    Thanks, I managed to figure it out yesterday. I have to say that's
    quite unintuitive, given that other controls on the form have code
    generated for them.

    Regardless, I am not quite understanding exactly how this works behind
    the scenes. I mean, how does ASP.NET figure that this is the object
    that it needs to work with?


    >
    > HTH,
    >
    > John
    >
    >
    > "Frank Rizzo" <> wrote in message
    > news:uBeYz$...
    >
    >>Peter Rilling wrote:
    >>
    >>>Do you mean passing information in the constructor? You cannot since
    >>>ASP.NET instantiates the control for you and binds the tag with some
    >>>code-behind variable.

    >>
    >>Either via the constructor or via properties. The problem is that the
    >>UserControl that I placed on the page is not instantiated in the
    >>code-behind file like a button control would be. So I am not sure about
    >>how to even reference it.
    >>
    >>For instance, the UserControl is called AddComments and my web page is
    >>main.aspx.cs
    >>
    >>I can't find anywhere in main.aspx.cs where AddComments is instantiated.
    >>I can instantiate it manually like
    >>
    >>AddComments oComment = new AddComments(...)
    >>But ASP.NET doesn't seem to take into account anything I do with the
    >>UserControl.
    >>
    >>I guess, I need to have a reference to the instance of the UC that's in
    >>use by ASP.NET.
    >>
    >>
    >>>You can however, use properties and methods to pass information in after

    >
    > it
    >
    >>>is created, maybe in the Page_Load event. Or you can set attributes on

    >
    > the
    >
    >>>page that get translated into property calls that ASP.NET sets.

    >>
    >>Could you give me an example of this?
    >>
    >>
    >>>You should at least have a protected variable in your code-behind that

    >
    > is
    >
    >>>named that same as the tag on the page.
    >>>
    >>>"Frank Rizzo" <> wrote in message
    >>>news:#...
    >>>
    >>>
    >>>>I've got a number of user controls on the web page. How can I pass some
    >>>>data to it? I don't see where the user control is instantiated in the
    >>>>page code-behind page.
    >>>>
    >>>>Thanks.
    >>>
    >>>
    >>>

    >
    >
    Frank Rizzo, Mar 6, 2004
    #8
  9. There are two things required for the web page and the code-behind to
    interact with each other.

    1) The tag on the web page must have the runat="server" defined.
    2) A variable in the code-behind must be defined, as a protected instance
    member, with the same name as the ID in the web page. This is how ASP.NET
    knows what variable to map to what tag.

    Once the tag is mapped to a variable, then you can operate on the variable
    with all actions affecting the tag when rendered.

    If you add something using the designer, it should place the variable
    reference for you in the code-behind. From my experience, the designer is a
    bit flaky when it comes to this code generation. More often then not, I
    have to define the variables myself, so knowing the interaction between the
    page and the code-behind is more then academic.


    "Frank Rizzo" <> wrote in message
    news:...
    > John Amick wrote:
    > > If I understand you correctly, I believe all you need to do is declare

    the
    > > control in your code-behind page. You will then be able to reference it

    in
    > > code. Even though you've already added the control in design mode, you
    > > should still be able to declare and reference it like this:
    > >
    > > public class MyWebForm : System.Web.UI.Page
    > > {
    > > protected AddComments oAddComments;
    > > ...
    > > oAddComments.MyProperty = "Something";
    > > ...
    > > }

    >
    >
    > Thanks, I managed to figure it out yesterday. I have to say that's
    > quite unintuitive, given that other controls on the form have code
    > generated for them.
    >
    > Regardless, I am not quite understanding exactly how this works behind
    > the scenes. I mean, how does ASP.NET figure that this is the object
    > that it needs to work with?
    >
    >
    > >
    > > HTH,
    > >
    > > John
    > >
    > >
    > > "Frank Rizzo" <> wrote in message
    > > news:uBeYz$...
    > >
    > >>Peter Rilling wrote:
    > >>
    > >>>Do you mean passing information in the constructor? You cannot since
    > >>>ASP.NET instantiates the control for you and binds the tag with some
    > >>>code-behind variable.
    > >>
    > >>Either via the constructor or via properties. The problem is that the
    > >>UserControl that I placed on the page is not instantiated in the
    > >>code-behind file like a button control would be. So I am not sure about
    > >>how to even reference it.
    > >>
    > >>For instance, the UserControl is called AddComments and my web page is
    > >>main.aspx.cs
    > >>
    > >>I can't find anywhere in main.aspx.cs where AddComments is instantiated.
    > >>I can instantiate it manually like
    > >>
    > >>AddComments oComment = new AddComments(...)
    > >>But ASP.NET doesn't seem to take into account anything I do with the
    > >>UserControl.
    > >>
    > >>I guess, I need to have a reference to the instance of the UC that's in
    > >>use by ASP.NET.
    > >>
    > >>
    > >>>You can however, use properties and methods to pass information in

    after
    > >
    > > it
    > >
    > >>>is created, maybe in the Page_Load event. Or you can set attributes on

    > >
    > > the
    > >
    > >>>page that get translated into property calls that ASP.NET sets.
    > >>
    > >>Could you give me an example of this?
    > >>
    > >>
    > >>>You should at least have a protected variable in your code-behind that

    > >
    > > is
    > >
    > >>>named that same as the tag on the page.
    > >>>
    > >>>"Frank Rizzo" <> wrote in message
    > >>>news:#...
    > >>>
    > >>>
    > >>>>I've got a number of user controls on the web page. How can I pass

    some
    > >>>>data to it? I don't see where the user control is instantiated in the
    > >>>>page code-behind page.
    > >>>>
    > >>>>Thanks.
    > >>>
    > >>>
    > >>>

    > >
    > >
    Peter Rilling, Mar 6, 2004
    #9
  10. Frank Rizzo

    Frank Rizzo Guest

    Peter Rilling wrote:
    > There are two things required for the web page and the code-behind to
    > interact with each other.
    >
    > 1) The tag on the web page must have the runat="server" defined.
    > 2) A variable in the code-behind must be defined, as a protected instance
    > member, with the same name as the ID in the web page. This is how ASP.NET
    > knows what variable to map to what tag.
    >
    > Once the tag is mapped to a variable, then you can operate on the variable
    > with all actions affecting the tag when rendered.
    >
    > If you add something using the designer, it should place the variable
    > reference for you in the code-behind. From my experience, the designer is a
    > bit flaky when it comes to this code generation. More often then not, I
    > have to define the variables myself, so knowing the interaction between the
    > page and the code-behind is more then academic.


    Thanks, I get it now.
    Frank Rizzo, Mar 7, 2004
    #10
    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?RGF2ZQ==?=

    Pass values between user controls?

    =?Utf-8?B?RGF2ZQ==?=, Jun 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,878
    Tanveer1909
    Sep 13, 2010
  2. Howard
    Replies:
    5
    Views:
    5,073
    Siva M
    Sep 4, 2005
  3. mehdi_mousavi
    Replies:
    1
    Views:
    338
    john_teague
    Sep 7, 2005
  4. Faizan Ahmed
    Replies:
    1
    Views:
    272
    John Saunders
    Jan 4, 2005
  5. Faizan Ahmed
    Replies:
    1
    Views:
    348
    John Saunders
    Jan 4, 2005
Loading...

Share This Page