|
-
Jun 24th, 2004, 04:12 AM
#1
Thread Starter
Addicted Member
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
-
Jun 24th, 2004, 10:29 AM
#2
Frenzied Member
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.
-
Jun 24th, 2004, 10:30 AM
#3
I wonder how many charact
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:
Dim x1 As myCustomControl = DirectCast(Me.FindControl("ReportMenu1"), myCustomControl)
myPageLabel.text = x.MyCustomControl.Label.Text
Last edited by nemaroller; Jun 24th, 2004 at 10:34 AM.
-
Jun 24th, 2004, 10:33 AM
#4
I wonder how many charact
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:
Public ReadOnly Property GetMyLabelText() As String
Get
If Not Label1 Is Nothing Then Return Label1.TexT
End Get
End Property
-
Jun 24th, 2004, 10:35 AM
#5
Frenzied Member
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.
-
Jun 24th, 2004, 10:37 AM
#6
I wonder how many charact
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|