Re: user control - passing values

Discussion in 'ASP .Net' started by Steve C. Orr, MCSD, Jul 12, 2003.

  1. You could declare your the controls inside your user control as public
    instead of the default of protected.
    Then you can access them directly from your web form like this:
    MyControl.MyDropdown.ToString...

    But rather than unprotecting your controls you might want to take a more
    object oriented approach and make special methods on your user control to
    expose specific control values to the page.

    For example, if you have a public method in your user control like this:
    Public Function MyDropDownListValue() as String
    Return MyDropDownList.ToString
    End Function

    Then you can call the method from your main page like this:
    MyControl.MyDropDownListValue

    --
    I hope this helps,
    Steve C. Orr, MCSD
    http://Steve.Orr.net



    "Sabre" <> wrote in message
    news:OC5Srv$...
    > Hello All
    >
    > I've got a form with one user control embedded. Both contain textboxes,
    > dropdownlists and checkboxes. Short of using sessions (haven't tried
    > viewstate) what means can be used to pass values between? So if checkbox1

    is
    > checked on the form, I'd like the user control to recognise this.

    Similarly
    > if a textbox on my user control contains text, I'd like to use this text

    in
    > my form.
    >
    > TIA
    > Graeme
    >
    >
    Steve C. Orr, MCSD, Jul 12, 2003
    #1
    1. Advertising

  2. Steve C. Orr, MCSD

    Sabre Guest

    Thanks Steve. You certainly got all the answers! Regards, Graeme
    "Steve C. Orr, MCSD" <> wrote in message
    news:...
    > You could declare your the controls inside your user control as public
    > instead of the default of protected.
    > Then you can access them directly from your web form like this:
    > MyControl.MyDropdown.ToString...
    >
    > But rather than unprotecting your controls you might want to take a more
    > object oriented approach and make special methods on your user control to
    > expose specific control values to the page.
    >
    > For example, if you have a public method in your user control like this:
    > Public Function MyDropDownListValue() as String
    > Return MyDropDownList.ToString
    > End Function
    >
    > Then you can call the method from your main page like this:
    > MyControl.MyDropDownListValue
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD
    > http://Steve.Orr.net
    >
    >
    >
    > "Sabre" <> wrote in message
    > news:OC5Srv$...
    > > Hello All
    > >
    > > I've got a form with one user control embedded. Both contain textboxes,
    > > dropdownlists and checkboxes. Short of using sessions (haven't tried
    > > viewstate) what means can be used to pass values between? So if

    checkbox1
    > is
    > > checked on the form, I'd like the user control to recognise this.

    > Similarly
    > > if a textbox on my user control contains text, I'd like to use this text

    > in
    > > my form.
    > >
    > > TIA
    > > Graeme
    > >
    > >

    >
    >
    Sabre, Jul 12, 2003
    #2
    1. Advertising

  3. Steve C. Orr, MCSD

    Sabre Guest

    OK Steve. I've taken the easy route and declared controls as Public Shared
    WithEvents within the user control and then referenced them from aspx form
    like so:

    WebUserControl1.DropDownList4.SelectedIndex =
    WebUserControl1.DropDownList4.Items.IndexOf(WebUserControl1.DropDownList4.It
    ems.FindByText(Session("ID")))

    That eliminated the errors ... now to overcome the "Event handler must be
    shared because its WithEvents var is shared" in the ascx page it appears I
    need to change the associated event to Public Shared like so:

    Public Shared Sub DropDownList4_SelectedIndexChanged(ByVal sender As
    System.Object, ByVal e As System.EventArgs) Handles
    DropDownList3.SelectedIndexChanged

    This seems fine too, but now I get the error "cannot refer to an instance
    member of class within a shared method or shared member initializer without
    an explicit instance of class" with all subs; sessions etc within the
    procedure eg:
    Button6_Click(sender, e) within the proc now errs.

    I've tried a few things, but so far, to no avail. Can you or anyone else
    help further?

    Thanks again
    Graeme



    "Steve C. Orr, MCSD" <> wrote in message
    news:...
    > You could declare your the controls inside your user control as public
    > instead of the default of protected.
    > Then you can access them directly from your web form like this:
    > MyControl.MyDropdown.ToString...
    >
    > But rather than unprotecting your controls you might want to take a more
    > object oriented approach and make special methods on your user control to
    > expose specific control values to the page.
    >
    > For example, if you have a public method in your user control like this:
    > Public Function MyDropDownListValue() as String
    > Return MyDropDownList.ToString
    > End Function
    >
    > Then you can call the method from your main page like this:
    > MyControl.MyDropDownListValue
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD
    > http://Steve.Orr.net
    >
    >
    >
    > "Sabre" <> wrote in message
    > news:OC5Srv$...
    > > Hello All
    > >
    > > I've got a form with one user control embedded. Both contain textboxes,
    > > dropdownlists and checkboxes. Short of using sessions (haven't tried
    > > viewstate) what means can be used to pass values between? So if

    checkbox1
    > is
    > > checked on the form, I'd like the user control to recognise this.

    > Similarly
    > > if a textbox on my user control contains text, I'd like to use this text

    > in
    > > my form.
    > >
    > > TIA
    > > Graeme
    > >
    > >

    >
    >
    Sabre, Jul 12, 2003
    #3
    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. adam
    Replies:
    0
    Views:
    2,576
  2. Mad Scientist Jr
    Replies:
    0
    Views:
    635
    Mad Scientist Jr
    Mar 22, 2006
  3. dotnetguy

    Passing values from a web user control using a class - ???

    dotnetguy, Feb 8, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    141
    dotnetguy
    Feb 8, 2004
  4. kostas
    Replies:
    0
    Views:
    105
    kostas
    Jan 24, 2005
  5. hmikki
    Replies:
    1
    Views:
    141
    Nathan Sokalski
    Oct 19, 2007
Loading...

Share This Page