How can I set the text of a label on a user control equal to whatever the user types into a textbox on a .aspx file? Is there any easy way of referencing the label in the .ascx file?
Create a property in the user control public property Text { get { return label1.text } set { label1.text = value; } } the property changes the value. Brian
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
Hi !! Is that code will make the label text as same as the textbox text on runtime like filling a textbox with some text amd click on button (as example) then i see the text of the label is what i typed in the box. Is that really the benefit of this code. If
there is a different idea or i got that wrong please advise!! Thanks
Fadil Alnassar
www.fadilalnassar.com | FREE Nodil Tab Control
http://www.mefranchising.com
Thanks for that. Can you tell me if that should be in the .ascx file or the .ascx.vb file? Will this work for setting the user control label text = text box on another form?
I think this is what you are looking for. If you are using code-behind, always put code in the code-behind (the .vb or .cs file); otherwise, you have no choice to put it in the .ascx file.
Brian
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
The property exposes a field in the user control without providing direct control, so if you want to encode the text or do something else to it (like trim it), you can. Brian
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
This is what I have done so far In the .ascx.vb class I have added the folowing class:
Public Class Logon
Inherits System.Web.UI.UserControl
Private username As String
Public Property Name() As String
Get
Return username
End Get
Set(ByVal Value As String)
username = Value
End Set
End Property
End Class
and within .aspx.vb file I have added this:
And this is where the label I want to set the text on is declared in the codebehind file of the usercontrol.
Public MustInherit Class WebUserControl1
Protected WithEvents lblUser As System.Web.UI.WebControls.Label
Now how do I actually set the text of the label - how do I call the class?
I'm a little confused of the layout. If Logon is your user control code-behind page, and you have the lblUser label within that user control (in the .ascx), then put the Protected WithEvents statement for the lblUser in the Logon code-behind, and do: Public
Property Name() As String Get Return lblUser.Text End Get Set(ByVal Value As String) lblUser.Text = Value End Set End Property Brian
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
Public MustInherit Class WebUserControl1
Inherits System.Web.UI.UserControl
Protected WithEvents lblUser As System.Web.UI.WebControls.Label
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
'Other code ...
Public Class Logon
Inherits System.Web.UI.UserControl
Protected WithEvents lblUser As System.Web.UI.WebControls.Label
Private username As String
Public Property Name() As String
Get
Return username
End Get
Set(ByVal Value As String)
username = Value
End Set
End Property
End Class
And the textbox is in an .aspx file. When the user clicks a button to Logon, I want their Logon name entered in the textbox to appear on the label lblUser in the User Control. Im just not sure how to call the Public
Property Name() As String part and to use what the user entered in the textbox.
What do you use this for: WebUserControl1? You don't have anything inheriting from it (as you declared it MustInherit). Also, a user control class that inherits from this cannot define controls in the base, and inherit them in that way. You need to define all
of the controls in the Logon user control, and each control must have a Protected WithEvents declaration in the code-behind. Whenever the login button is clicked, then you make that assignment. Is the lblUser label in the same user control as login, or are
we talking about a different user control (the first one)? I'm confused.. Brian
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
eclair21
Participant
980 Points
196 Posts
Setting label text = textbox text on other form
Jan 04, 2005 11:29 AM|LINK
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: Setting label text = textbox text on other form
Jan 04, 2005 02:50 PM|LINK
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
fadil1977
Star
7894 Points
1581 Posts
Re: Setting label text = textbox text on other form
Jan 04, 2005 02:57 PM|LINK
www.fadilalnassar.com | FREE Nodil Tab Control
http://www.mefranchising.com
eclair21
Participant
980 Points
196 Posts
Re: Setting label text = textbox text on other form
Jan 04, 2005 03:15 PM|LINK
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: Setting label text = textbox text on other form
Jan 04, 2005 04:40 PM|LINK
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: Setting label text = textbox text on other form
Jan 04, 2005 04:43 PM|LINK
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
eclair21
Participant
980 Points
196 Posts
Re: Setting label text = textbox text on other form
Jan 05, 2005 08:20 AM|LINK
Public Class Logon Inherits System.Web.UI.UserControl Private username As String Public Property Name() As String Get Return username End Get Set(ByVal Value As String) username = Value End Set End Property End Classand within .aspx.vb file I have added this: And this is where the label I want to set the text on is declared in the codebehind file of the usercontrol. Now how do I actually set the text of the label - how do I call the class?bmains
All-Star
29116 Points
5886 Posts
MVP
Re: Setting label text = textbox text on other form
Jan 05, 2005 12:06 PM|LINK
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
eclair21
Participant
980 Points
196 Posts
Re: Setting label text = textbox text on other form
Jan 05, 2005 12:22 PM|LINK
Public MustInherit Class WebUserControl1 Inherits System.Web.UI.UserControl Protected WithEvents lblUser As System.Web.UI.WebControls.Label #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here End Sub 'Other code ... Public Class Logon Inherits System.Web.UI.UserControl Protected WithEvents lblUser As System.Web.UI.WebControls.Label Private username As String Public Property Name() As String Get Return username End Get Set(ByVal Value As String) username = Value End Set End Property End ClassAnd the textbox is in an .aspx file. When the user clicks a button to Logon, I want their Logon name entered in the textbox to appear on the label lblUser in the User Control. Im just not sure how to call the Public Property Name() As String part and to use what the user entered in the textbox.bmains
All-Star
29116 Points
5886 Posts
MVP
Re: Setting label text = textbox text on other form
Jan 05, 2005 04:41 PM|LINK
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).