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
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
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
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
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.UserControlPrivate username As String
Public Property Name() As String
Get
Return username
End Get
Set(ByVal Value As String)
username = Value
End Set
End PropertyEnd Class
and within .aspx.vb file I have added this:
<ACME:GETEST id="GETEST1" runat="server" username="xyz" OnNavigate="TestControl_OnNavigate"></ACME:GETEST
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 WebUserControl1Protected 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
Heres the file named GENav.ascx.vb:
Public MustInherit Class WebUserControl1
Inherits System.Web.UI.UserControlProtected WithEvents lblUser As System.Web.UI.WebControls.Label
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> 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 hereEnd Sub
'Other code ...
Public Class Logon
Inherits System.Web.UI.UserControl
Protected WithEvents lblUser As System.Web.UI.WebControls.LabelPrivate username As String
Public Property Name() As String
Get
Return username
End Get
Set(ByVal Value As String)
username = ValueEnd Set
End PropertyEnd 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
There is only one user control this is where the lblUser label is.
WebUserControl1 was code I didnt write - added by the system and I dont really know what its for but it was working fine so I just went with it.
The button I want to do the assignment of the textbox text to the label is just in a .aspx file so its on a web form.
I added the code in the Logon class below trying to do what you said earlier so the Logon class didnt exist before this.
Public Class Logon
Inherits System.Web.UI.UserControl
Protected WithEvents lblUser As System.Web.UI.WebControls.LabelPrivate username As String
Public Property Name() As String
Get
Return username
End Get
Set(ByVal Value As String)
username = ValueEnd Set
End PropertyEnd Class
SO if the button is in the user control, you need a Protected WithEvents definition for that too; that should be generated by VS.NET if you are using that; are you? If you have that to, for the link button's click event, then if the user logs in, assign the user name text in the textbox (also needing a protected withevents statement) to the label. You wouldn't need the property if you didn't want to, or could use it as readonly to state what the username was. I wouldn't assign the value to a string, but would assign it directly to the label.
Brian
Also in continuing, it may help to post the entire .ascx.cs file for login control and the entire HTML code .ascx. Leave out the other code that you aren't using; that's generated by the .NET editor.
Brian
The button is not in the user control it is in the web form.
Yes I am using VS.NET.
Here is all the code as it was before I started trying to assign the textbox to label text:
GENav.ascx.vb file:
Public MustInherit Class WebUserControl1
Inherits System.Web.UI.UserControl
Protected WithEvents btnHome As System.Web.UI.WebControls.Button
Protected WithEvents btnSearch As System.Web.UI.WebControls.Button
Protected WithEvents btnUpdate As System.Web.UI.WebControls.Button
Protected WithEvents btnView As System.Web.UI.WebControls.Button
Protected WithEvents btnLogout As System.Web.UI.WebControls.Button
Protected WithEvents btnGuide As System.Web.UI.WebControls.Button
Protected WithEvents lblUser As System.Web.UI.WebControls.Label
Protected WithEvents lblTime As System.Web.UI.WebControls.Label
Protected WithEvents PnlHeader As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents PnlNav As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
Protected WithEvents lblHeader As System.Web.UI.WebControls.Label#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> 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'Display date and time
Dim dtNow As DateTime = DateTime.Now
lblTime.Text = dtNow.ToStringEnd Sub
Sub Login_OnNavigate(ByVal sender As Object, ByVal e As EventArgs)
'lblUser.Text = ((CType(sender, Control).ID))
End Sub
Private Sub lkAdmin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub btnHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHome.Click
End Sub
Private Sub btnLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogout.Click
End Sub
Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click
End Sub
End Class
The GENav.ascx file:
<%@. Control Language="vb" AutoEventWireup="false" Codebehind="GENav.ascx.vb" Inherits="WebApplication1.WebUserControl1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<script type="text/javascript"
window.moveTo(0,0);if (document.all) {
top.window.resizeTo(
screen.availWidth,
screen.availHeight);
} else if (document.layers
|| document.getElementById) {
if (top.window.outerHeight < screen.availHeight ||
top.window.outerWidth < screen.availWidth){top.window.outerHeight = screen.availHeight;
top.window.outerWidth = screen.availWidth;
}
}
</script>
<script runat="server"
Event Navigate As EventHandlerSub btnHome_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnHome.Click
OnNavigate(sender, e)
End SubSub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
OnNavigate(sender, e)
End SubSub btnView_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnView.Click
OnNavigate(sender, e)
End SubSub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
OnNavigate(sender, e)
End SubSub btnGuide_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGuide.Click
OnNavigate(sender, e)
End SubSub btnLogout_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogout.Click
OnNavigate(sender, e)btnLogout.Text = "Logout"
End Sub
Overridable Sub OnNavigate(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent Navigate(sender, e)
End Sub</script>
<DIV id="PnlHeader" style="LEFT: 15%; WIDTH: 88%; POSITION: absolute; TOP: 0%; HEIGHT: 20%; BACKGROUND-COLOR: #2b4d96" onclick="BtnHome_Click" runat="server" ms_positioning="GridLayout"><asp:label id="lblHeader" style="LEFT: 5%; POSITION: absolute; TOP: 40%" runat="server" BackColor="#2B4D96" Width="95%" ForeColor="White" Font-Bold="True" Height="40%" Font-Size="Large" Font-Names="Rockwell Extra Bold">Header</asp:label></DIV>
<DIV id="PnlNav" style="LEFT: -14px; WIDTH: 20.69%; POSITION: absolute; TOP: -14px; HEIGHT: 100%; BACKGROUND-COLOR: #2b4d96" runat="server" ms_positioning="GridLayout"><asp:button id="btnHome" style="Z-INDEX: 102; LEFT: 22px; POSITION: absolute; TOP: 111px" runat="server" BackColor="White" Width="80%" ForeColor="#2B4D96" Font-Bold="True" Runat="server" Text="Home"></asp:button><asp:button id="btnSearch" style="Z-INDEX: 103; LEFT: 20px; POSITION: absolute; TOP: 145px" runat="server" BackColor="White" Width="80%" ForeColor="#2B4D96" Font-Bold="True" Text="Search"></asp:button><asp:button id="btnUpdate" style="Z-INDEX: 104; LEFT: 21px; POSITION: absolute; TOP: 211px" runat="server" BackColor="White" Width="80%" ForeColor="#2B4D96" Font-Bold="True" Text=" Update Orders"></asp:button><asp:button id="btnView" style="Z-INDEX: 105; LEFT: 22px; POSITION: absolute; TOP: 180px" runat="server" BackColor="White" Width="80%" ForeColor="#2B4D96" Font-Bold="True" Text="View Orders"></asp:button><asp:button id="btnLogout" style="Z-INDEX: 106; LEFT: 21px; POSITION: absolute; TOP: 279px" runat="server" BackColor="White" Width="80%" ForeColor="#2B4D96" Font-Bold="True" Text="Log In"></asp:button><asp:button id="btnGuide" style="Z-INDEX: 107; LEFT: 20px; POSITION: absolute; TOP: 242px" runat="server" BackColor="White" Width="80%" ForeColor="#2B4D96" Font-Bold="True" Text="Online Guide"></asp:button><asp:label id="lblUser" style="Z-INDEX: 108; LEFT: 29px; POSITION: absolute; TOP: 332px" runat="server" Width="80%" ForeColor="White" Font-Bold="True" Height="24px"></asp:label><asp:label id="lblTime" style="Z-INDEX: 109; LEFT: 29px; POSITION: absolute; TOP: 368px" runat="server" Width="80%" ForeColor="White" Font-Bold="True" Height="36px"></asp:label><asp:hyperlink id="HyperLink1" style="Z-INDEX: 110; LEFT: 21px; POSITION: absolute; TOP: 310px" runat="server" ForeColor="White" NavigateUrl="Admin.aspx">Administrator</asp:hyperlink></DIV>
This is the web form:
<%@. Page Language="vb" AutoEventWireup="false" Codebehind="GELogin.aspx.vb" Inherits="WebApplication1.WebForm2"%>
<%@. Register TagPrefix="ACME" TagName="GETEST" src="http://pics.10026.com/?src=GENav.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>GELogin</title>
<script runat="server"
Sub TestControl_OnNavigate(ByVal sender As Object, ByVal e As EventArgs)
Response.Write((CType(sender, Control).ID + " was clicked"))
lblNavClick.Text = ((CType(sender, Control).ID))if lblNavClick.Text = "btnLogout" Then
PnlPassword.Visible = True
txtUsername.Text = ""
txtPassword.Text = ""
End IfEnd If
End Sub 'TestControl_OnNavigate
</script>
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<BODY>
<form id="Form1" method="post" runat="server">
<ACME:GETEST id="GETEST1" runat="server" OnNavigate="TestControl_OnNavigate"></ACME:GETEST
<DIV id="PnlPassword" style="Z-INDEX: 106; LEFT: 20%; WIDTH: 60%; BORDER-TOP-STYLE: groove; BORDER-RIGHT-STYLE: groove; BORDER-LEFT-STYLE: groove; POSITION: absolute; TOP: 22%; HEIGHT: 70%; BACKGROUND-COLOR: #ffffff; BORDER-BOTTOM-STYLE: groove" runat="server" ms_positioning="GridLayout">
<DIV id="lblUsername" style="DISPLAY: inline; LEFT: 5%; WIDTH: 30%; POSITION: absolute; TOP: 30%; HEIGHT: 10%" ms_positioning="FlowLayout">User
Name:</DIV>
<DIV id="lblPassword" style="DISPLAY: inline; LEFT: 5%; WIDTH: 20%; POSITION: absolute; TOP: 50%; HEIGHT: 10%" ms_positioning="FlowLayout">Password:</DIV>
<asp:button id="btnGo" style="LEFT: 50%; POSITION: absolute; TOP: 80%" runat="server" Height="10%" Width="20%" Text="Go >"></asp:button><asp:textbox id="txtUsername" style="LEFT: 30%; POSITION: absolute; TOP: 30%" runat="server" Height="10%" Width="40%"></asp:textbox><asp:textbox id="txtPassword" style="LEFT: 30%; POSITION: absolute; TOP: 50%" runat="server" Height="10%" Width="40%" TextMode="Password"></asp:textbox><asp:label id="lblDirections" style="LEFT: 10%; POSITION: absolute; TOP: 5%" runat="server" Height="10%" Width="90%" Font-Size="Medium" BackColor="White" Font-Bold="True" ForeColor="Navy">Enter your User Name and Password
below:</asp:label><asp:linkbutton id="lkNewAccount" style="LEFT: 75%; POSITION: absolute; TOP: 20%" runat="server" Height="10%" Width="20%" Font-Size="X-Small">New User</asp:linkbutton><asp:button id="btnClear" style="LEFT: 25%; POSITION: absolute; TOP: 80%" runat="server" Height="10%" Width="20%" Text="Clear"></asp:button>
<P><asp:linkbutton id="lkChangePwd" style="LEFT: 75%; POSITION: absolute; TOP: 40%" runat="server" Height="10%" Width="20%" Font-Size="X-Small">Change Password</asp:linkbutton><asp:linkbutton id="lkForgotten" style="LEFT: 75%; POSITION: absolute; TOP: 60%" runat="server" Height="10%" Width="20%" Font-Size="X-Small">Forgotten your
Password?</asp:linkbutton></P>
</DIV>
</form>
</BODY>
</HTML>
And this is the GELogin.aspx.vb file (codebehind for Web Form)
Imports System.Data
Imports System.Data.SqlClientPublic Class WebForm2
Inherits System.Web.UI.PageProtected WithEvents txtnewPwd2 As System.Web.UI.WebControls.TextBox
Protected WithEvents lblnewPwd2 As System.Web.UI.WebControls.Label
Protected WithEvents txtnewPwd1 As System.Web.UI.WebControls.TextBox
Protected WithEvents lblnewPwd1 As System.Web.UI.WebControls.Label
Protected WithEvents txtUserpwdchange As System.Web.UI.WebControls.TextBox
Protected WithEvents lblPwdChangeuser As System.Web.UI.WebControls.Label
Protected WithEvents txtoldpwd As System.Web.UI.WebControls.TextBox
Protected WithEvents lbloldpwd As System.Web.UI.WebControls.Label
Protected WithEvents lblGivenemail As System.Web.UI.WebControls.Label
Protected WithEvents btnchangePwdBack As System.Web.UI.WebControls.Button
Protected WithEvents lblChangepwd As System.Web.UI.WebControls.Label
Protected WithEvents btnGoChangePwd As System.Web.UI.WebControls.ButtonProtected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents txtemail As System.Web.UI.WebControls.TextBox
Protected WithEvents lblemail As System.Web.UI.WebControls.Label
Protected WithEvents btnGo As System.Web.UI.WebControls.Button
Protected WithEvents txtUsername As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
Protected WithEvents lblDirections As System.Web.UI.WebControls.Label
Protected WithEvents lkNewAccount As System.Web.UI.WebControls.LinkButton
Protected WithEvents lblResponse As System.Web.UI.WebControls.Label
Protected WithEvents btnClear As System.Web.UI.WebControls.Button
Protected WithEvents lkChangePwd As System.Web.UI.WebControls.LinkButton
Protected WithEvents lkForgotten As System.Web.UI.WebControls.LinkButton
Protected WithEvents PnlPassword As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents lblWelcome As System.Web.UI.WebControls.Label
Protected WithEvents lblNavClick As System.Web.UI.WebControls.Label
Protected WithEvents lkLogon As System.Web.UI.WebControls.LinkButton
Protected WithEvents lblUserLoggedIn As System.Web.UI.WebControls.Label
#Region " Web Form Designer Generated Code "'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> 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 hereEnd Sub
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
If txtUsername.Text = "" Or txtPassword.Text = "" Then
PnlPassword.Visible = True
'Its here I want the text from txtUserName.Text to be assigned to the label in the user control
'Generates the message:
Dim strMessage As StringstrMessage = "Enter a User Name & Password to proceed"
'finishes server processing, returns to client.
Dim strScript As String = "<script language=JavaScript>"
strScript += "alert(""" & strMessage & """);"
strScript += "</script>"If (Not Page.IsStartupScriptRegistered("clientScript")) Then
Page.RegisterStartupScript("clientScript", strScript)
End IfElse
PnlScreen.Visible = True
PnlPassword.Visible = FalseEnd If
End SubPrivate Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtUsername.Text = ""
txtPassword.Text = ""
PnlPassword.Visible = True
PnlScreen.Visible = FalseEnd Sub
End Class
Can you see what I am trying to achieve a bit clearer now?
i will assume the following, correct me if i'm wrong.
1) you have an aspx page with a textbox and a button and a usercontrol containing a label
2) the user enters a name, and clicks submit, the page then does a postback
3) the usercontrol label changes to the textbox text.
to do this...
1) add this to the aspx page
Protected WithEvents m_UserControl As WebUserControl1 (your control type)
2) in design view of the aspx page, click the button and add the following code.. change the findcontrol string to the name of the control in the TagName attribute at the top of the aspx page.
m_UserControl= CType(FindControl("Webusercontrol11"), WebUserControl1)
m_UserControl.TextValue = txtname.Text
this should do what you need.
i think it has all been said in the post earlier, but it looks like things got confusing.
0 comments:
Post a Comment