ASP.Net/User Control and Master Page/Control Action
Содержание
Clear textbox in a user defined control (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <%@ Register
TagPrefix="My" TagName="SimpleControl" Src="UserControlSub.ascx"
%> <script runat=server> Sub Clear_Click(Sender As Object, E As EventArgs)
MSC1.ClearText("All")
End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Implementing a User Control on an ASP.NET Page</TITLE> </HEAD> <BODY LEFTMARGIN="40"> <form
runat="server" id="MyForm"
>
<My:SimpleControl
id="MSC1" runat="server"
/>
<asp:button
id="butOK" text=" OK " runat="server"
/> <asp:button
id="butClear" text="Clear" onclick="Clear_Click" runat="server"
/>
<asp:label
id="lblMessage" runat="server"
/> </form> </BODY> </HTML> <%--UserControlSub.ascx <script language="VB" runat="server"> Public ReadOnly Property UserName() As String
Get UserName = txtUserName.Text End Get
End Property Public ReadOnly Property Password() As String
Get Password = txtPassword.Text End Get
End Property Public ReadOnly Property Version() As String
Get Version = "2.3.145" End Get
End Property Public WriteOnly Property FontName() As String
Set lbl1.Font.Name = value lbl2.Font.Name = value End Set
End Property Public WriteOnly Property FontBold() As Boolean
Set lbl1.Font.Bold = value lbl2.Font.Bold = value End Set
End Property Public Property UserNameLabel() As String
Get UserNameLabel = lbl1.Text End Get Set lbl1.Text = value End Set
End Property Public Property PasswordLabel() As String
Get PasswordLabel = lbl2.Text End Get Set lbl2.Text = value End Set
End Property Public Sub ClearText (TextToClear as String)
If TextToClear = "All" Then txtUserName.Text = "" txtPassword.Text = "" ElseIf TextToClear = "UserName" Then txtUserName.Text = "" ElseIf TextToClear = "Password" Then txtPassword.Text = "" Else Err.Raise(vbObjectError + 513, "User Control Error!", _ "TextToClear parameter must be set to " _ & "All, UserName or Password!") End If
End Sub </script>
<asp:Label id="lbl1" runat="server" Font-Bold="True" Text="User Name: " /> |
<asp:TextBox id="txtUserName" runat=server /> |
<asp:Label id="lbl2" runat="server" Font-Bold="True" Text="Password: " /> |
<asp:TextBox id="txtPassword" runat=server TextMode="Password" /> |
--%>
</source>
User control with event
<source lang="csharp">
<%@ Page language="c#" %> <%@ Register TagPrefix="uc1" TagName="Control" Src="Control.ascx" %> <script runat="server">
void MultipleReached(object sender, EventArgs e) { Message.Text="Congratulations!"; }
</script> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html>
<body MS_POSITIONING="FlowLayout"> <form id="Control" method="post" runat="server"> <uc1:Control id="Control1" runat="server" Multiple="5" OnMultipleReached="MultipleReached"></uc1:Control> <asp:Label ID="Message" Runat="server" EnableViewState="False" /> </form> </body>
</html> File: Control.ascx <%@ Control Language="c#" AutoEventWireup="false" Codebehind="Control.ascx.cs" Inherits="Control.Control" TargetSchema="http://schemas.microsoft.ru/intellisense/ie5" %> <asp:Label id="OutputLabel" runat="server"></asp:Label> File: Control.ascx.cs namespace Control {
using System; using System.Data; using System.Drawing; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; public abstract class Control : System.Web.UI.UserControl { protected static readonly object EventMultipleReached = new Object(); protected System.Web.UI.WebControls.Label OutputLabel; private int multiple = 10; public int Multiple { get { return multiple; } set { multiple = value; } } private void Page_Load(object sender, System.EventArgs e) { if(Application["count"] == null) Application["count"] = 0; Application.Lock(); Application["count"] = (int)Application["count"] + 1; Application.UnLock(); if((int)Application["count"] % Multiple == 0) OnMultipleReached(EventArgs.Empty); OutputLabel.Text = Application["count"].ToString(); } public event EventHandler MultipleReached { add { Events.AddHandler(EventMultipleReached, value); } remove { Events.RemoveHandler(EventMultipleReached, value); } } public virtual void OnMultipleReached(EventArgs e) { EventHandler MultipleReachedHandler = (EventHandler)Events[EventMultipleReached]; if (MultipleReachedHandler != null) { MultipleReachedHandler(this, e); } } override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } }
}
</source>
User Control with Events (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <%@ Register
TagPrefix="My" TagName="SimpleControl" Src="UserControlEvents.ascx"
%> <script runat=server> Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
If MSC1.ValidateLogin("Joe", "nfex") Then lblMessage.Text = "Entry allowed!" Else lblMessage.Text = "Entry denied!" End If
End Sub Sub Clear_Click(Sender As Object, E As EventArgs)
MSC1.ClearText("All")
End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Implementing a User Control on an ASP.NET Page</TITLE> </HEAD> <BODY LEFTMARGIN="40"> <form
runat="server" id="MyForm"
>
<My:SimpleControl
id="MSC1" runat="server"
/>
<asp:button
id="butOK" text=" OK " onclick="SubmitBtn_Click" runat="server"
/> <asp:button
id="butClear" text="Clear" onclick="Clear_Click" runat="server"
/>
<asp:label
id="lblMessage" runat="server"
/> </form> </BODY> </HTML> <%--UserControlEvents.ascx <script language="VB" runat="server"> Public ReadOnly Property UserName() As String
Get UserName = txtUserName.Text End Get
End Property Public ReadOnly Property Password() As String
Get Password = txtPassword.Text End Get
End Property Public ReadOnly Property Version() As String
Get Version = "2.3.145" End Get
End Property Public WriteOnly Property FontName() As String
Set lbl1.Font.Name = value lbl2.Font.Name = value End Set
End Property Public WriteOnly Property FontBold() As Boolean
Set lbl1.Font.Bold = value lbl2.Font.Bold = value End Set
End Property Public Property UserNameLabel() As String
Get UserNameLabel = lbl1.Text End Get Set lbl1.Text = value End Set
End Property Public Property PasswordLabel() As String
Get PasswordLabel = lbl2.Text End Get Set lbl2.Text = value End Set
End Property Public Sub ClearText (TextToClear as String)
If TextToClear = "All" Then txtUserName.Text = "" txtPassword.Text = "" ElseIf TextToClear = "UserName" Then txtUserName.Text = "" ElseIf TextToClear = "Password" Then txtPassword.Text = "" Else Err.Raise(vbObjectError + 513, "User Control Error!", _ "TextToClear parameter must be set to " _ & "All, UserName or Password!") End If
End Sub Public Function ValidateLogin (TestUserName as String, _
TestPassword as String) as Boolean If txtUserName.Text = TestUserName and _ txtPassword.Text = TestPassword Then ValidateLogin = True Else ValidateLogin = False End If
End Function Sub UserName_Changed(Sender As Object, E As EventArgs)
txtUserName.Text = UCase(txtUserName.Text)
End Sub </script>
<asp:Label id="lbl1" runat="server" Font-Bold="True" Text="User Name: " /> |
<asp:TextBox id="txtUserName" runat=server autopostback="True" ontextchanged="UserName_Changed" /> |
<asp:Label id="lbl2" runat="server" Font-Bold="True" Text="Password: " /> |
<asp:TextBox id="txtPassword" runat=server TextMode="Password" /> |
--%>
</source>
User control with event (VB)
<source lang="csharp">
<%@ Page language="vb" AutoEventWireUp="false" %> <%@ Register TagPrefix="uc1" TagName="Control" Src="Control.ascx"%> <script runat="server">
Sub MultipleReached(ByVal sender As Object, ByVal e As EventArgs) Message.Text="Congratulations! You were the 5th visitor!" End Sub
</script> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html>
<body MS_POSITIONING="FlowLayout"> <form id="Control" method="post" runat="server"> <uc1:Control id="Control1" runat="server" Multiple="5" OnMultipleReached="MultipleReached"></uc1:Control> <asp:Label ID="Message" Runat="server" EnableViewState="False" /> </form> </body>
</html> File: Control.ascx <%@ Control Language="vb" Src="Control.ascx.vb" Inherits="Control.Control" AutoEventWireup="false" %> <asp:Label id="OutputLabel" runat="server"></asp:Label> File: Control.ascx.vb Namespace Control
Public MustInherit Class Control Inherits System.Web.UI.UserControl Protected WithEvents OutputLabel As System.Web.UI.WebControls.Label Private _multiple As Integer = 10 Public Property Multiple() As Integer Get Return _multiple End Get Set(ByVal Value As Integer) _multiple = Value End Set End Property Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init InitializeComponent() End Sub Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Application("count") Is Nothing Then Application("count") = 0 End If Application.Lock() Application("count") = Application("count") + 1 Application.UnLock() If Application("count") Mod Multiple = 0 Then OnMultipleReached(System.EventArgs.Empty) End If OutputLabel.Text = Application("count").ToString() End Sub Public Event MultipleReached(ByVal sender As Object, ByVal e As System.EventArgs) Public Overridable Sub OnMultipleReached(ByVal e As System.EventArgs) RaiseEvent MultipleReached(Me, e) End Sub End Class
End Namespace
</source>