Results 1 to 6 of 6

Thread: simple question, i hope... (custom controls)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2003
    Location
    Gent
    Posts
    166

    simple question, i hope... (custom controls)

    i have made a custom control (.ascx) "Hoofding" containing a label "Number"

    i added the custom control to an .aspx-page and now i want to read the value in the label Number into a label Number2 which is on my page

    how could i do this? I need one statement and i tried a lot...

    greetz
    never argue with an idiot, he will bring you down to his level and will beat you through experience

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    First in the code for your user control you need to change the accessor of the label from Protected (default) to Public so you can access it outside the class.

    Second, you will need to find the user control name on the .aspx page. In your form code, declare a user control with the same name as the control you dragged onto the form (I don't know why VS doesn't do this automatically.)

    So the declaration on your page would look something like
    Protected Control1 As ProjectName.UserControl1

    Then on the page you can just set

    Label1.Text = Control1.Label1.Text
    Last edited by wey97; Jun 24th, 2004 at 10:34 AM.

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You could use FindControl from your page passing the name of the UserControl... cast it, and retrieve the property.

    Note that you have to pass the name of the customcontrol as it is declared on your page to the FindControl function. In the sample below, the name is ReportMenu1.. Its not the name of the class, its the instance name on your page.
    VB Code:
    1. Dim x1 As myCustomControl = DirectCast(Me.FindControl("ReportMenu1"), myCustomControl)
    2.                 myPageLabel.text = x.MyCustomControl.Label.Text
    Last edited by nemaroller; Jun 24th, 2004 at 10:34 AM.

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    As far as the accessor, I believe Text is by default a Public property.

    Edit:nm, I see what wej97 was saying, because your label is a private member... you can however, make a public readonly property that simply retrieve the label's value:
    VB Code:
    1. Public ReadOnly Property GetMyLabelText() As String
    2.  Get
    3.    If Not Label1 Is Nothing Then Return Label1.TexT
    4.  End Get
    5. End Property

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Originally posted by nemaroller
    As far as the accessor, I believe Text is by default a Public property.
    Sorry I edited to include what I really meant. By default the accessor for the Label control on the user control will be protected.

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    lol, no you're correct, I was thinking he was just trying to retrieve a Text property of a usercontrol... I now see he's trying to reach a Label's text property.

    And then I realized Text is only a property of WebControl, not UserControl...

Posting Permissions

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



Click Here to Expand Forum to Full Width