ASP.Net/User Control and Master Page/Basics

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Implementing a User Control on an ASP.NET Page (VB.net)

   <source lang="csharp">

<%@ Page Language=VB Debug=true %> <%@ Register

   TagPrefix="My" 
   TagName="SimpleControl" 
   Src="UserControlControls.ascx" 

%> <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"

/> </form> </BODY> </HTML> <%--UserControlControls.ascx

<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>
   
  


LoadControl user control

   <source lang="csharp">

<%@ Page %> <%@ Reference Control="control.ascx" %> <script runat="server" language="C#"> void Page_Load(Object sender, EventArgs e) {

   Control sampleControl = LoadControl("Control.ascx");
   Controls.Add(sampleControl);

} </script> C# version (control.ascx)

<%@ Control Language="C#" %> <script runat="server" language="C#"> void Page_Load(Object sender, EventArgs e) {

   AuthorLabel.Text = "Sample Control Text";

} </script>

<asp:Label id="AuthorLabel" runat="server" /> </source>

User control with asp control

   <source lang="csharp">

<%@ Page %> <%@ Register TagPrefix="user" TagName="SampleControl" Src="control.ascx" %> <user:SampleControl runat="server" />

C# version (control.ascx) <%@ Control Language="C#" %> <script runat="server" language="C#"> void Page_Load(Object sender, EventArgs e) {

   lblSample.Text = "Sample Control Text";

} </script> <asp:label runat="server" id="lblSample" /> VB version (control.ascx) <%@ Control Language="VB" %> <script runat="server" language="VB"> Sub Page_Load(sender as object, e as EventArgs)

   lblSample.Text = "Sample Control Text"

End Sub </script> <asp:label runat="server" id="lblSample" />

</source>
   
  


User control with no user interface (VB.net)

   <source lang="csharp">

<%@ Page Language=VB Debug=true %> <%@ Reference Control="UserControlNoInterface.ascx" %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)

   Dim MyControl as UserControl = _
       LoadControl("UserControlNoInterface.ascx")
   lblMessage.Text = "PI: " & _
       CType(MyControl, UserControlNoInterface).ReturnPI _
       & "
PI Rounded: " _ & CType(MyControl, UserControlNoInterface).RoundPI(3) _ & "
Circle Circumference: " _ & CType(MyControl, UserControlNoInterface). _ CircleCircumference(2)

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"    

> <asp:label

   id="lblMessage"
   runat="server"

/> </form> </BODY> </HTML> <%--UserControlNoInterface.ascx <%@ Control className="UserControlNoInterface" %> <script language="VB" runat="server"> Public ReadOnly Property ReturnPI() As Double

   Get
       ReturnPI = Math.PI
   End Get

End Property Public Function RoundPI(DecimalPlaces as Integer)

   RoundPI = Math.Round(Math.PI, DecimalPlaces)   

End Function Public Function CircleCircumference(TheRadius as Double) as Double

   CircleCircumference = TheRadius * TheRadius * Math.PI

End Function </script> --%>

</source>