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:

    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:
    Steve C. Orr, MCSD, Jul 12, 2003
    1. Advertisements

  2. Steve C. Orr, MCSD

    Sabre Guest

    Thanks Steve. You certainly got all the answers! Regards, Graeme
    Sabre, Jul 12, 2003
    1. Advertisements

  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 =

    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

    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
    Sabre, Jul 12, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.