ASP.Net/User Control and Master Page/Basics

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

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

<%@ 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"    
>
<BR><BR>
<My:SimpleControl 
    id="MSC1" 
    runat="server"
/>
</form>
</BODY>
</HTML>
<%--UserControlControls.ascx
<Table style="font: 10pt verdana;border-width:1;
    border-style:solid;border-color:black;" cellspacing="15">
<TR>
<TD>
<asp:Label
    id="lbl1"
    runat="server"
    Font-Bold="True"
    Text="User Name: "
/>
</TD>
<TD>
<asp:TextBox 
    id="txtUserName"
    runat=server
/>
</TD>
</TR>
<TR>
<TD>
<asp:Label
    id="lbl2"
    runat="server"
    Font-Bold="True"
    Text="Password: "
/>
</TD>
<TD>
<asp:TextBox 
    id="txtPassword"
    runat=server
    TextMode="Password"
/>
</TD>
</TR>
</Table>
--%>



LoadControl user control

<%@ 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>
<h3><asp:Label id="AuthorLabel" runat="server" />



User control with asp control

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



User control with no user interface (VB.net)

<%@ 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 _
        & "<BR>PI Rounded: " _
        & CType(MyControl, UserControlNoInterface).RoundPI(3) _
        & "<BR>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>
--%>