共用方式為


Login Web Server Control Declarative Syntax 

Provides user interface elements to log on to a Web site.

<asp:Login
    AccessKey="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderPadding="integer"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    CreateUserIconUrl="uri"
    CreateUserText="string"
    CreateUserUrl="uri"
    CssClass="string"
    DestinationPageUrl="uri"
    DisplayRememberMe="True|False"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    FailureAction="Refresh|RedirectToLoginPage"
    FailureText="string"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
                       Large|X-Large|XX-Large"
        Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    HelpPageIconUrl="uri"
    HelpPageText="string"
    HelpPageUrl="uri"
    ID="string"
    InstructionText="string"
    LoginButtonImageUrl="uri"
    LoginButtonText="string"
    LoginButtonType="Button|Image|Link"
    MembershipProvider="string"
    OnAuthenticate="Authenticate event handler"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnLoggedIn="LoggedIn event handler"
    OnLoggingIn="LoggingIn event handler"
    OnLoginError="LoginError event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    Orientation="Horizontal|Vertical"
    PasswordLabelText="string"
    PasswordRecoveryIconUrl="uri"
    PasswordRecoveryText="string"
    PasswordRecoveryUrl="uri"
    PasswordRequiredErrorMessage="string"
    RememberMeSet="True|False"
    RememberMeText="string"
    runat="server"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    TextLayout="TextOnLeft|TextOnTop"
    TitleText="string"
    ToolTip="string"
    UserName="string"
    UserNameLabelText="string"
    UserNameRequiredErrorMessage="string"
    Visible="True|False"
    VisibleWhenLoggedIn="True|False"
    Width="size"
>
        <CheckBoxStyle />
        <FailureTextStyle />
        <HyperLinkStyle />
        <InstructionTextStyle />
        <LabelStyle />
        <LayoutTemplate>
            <!-- child controls -->
        </LayoutTemplate>
        <LoginButtonStyle />
        <TextBoxStyle />
        <TitleTextStyle />
        <ValidatorTextStyle />
</asp:Login>

Remarks

The Login control is a composite control that provides all the common user interface elements that are needed to authenticate a user on a Web site. For more information on using the Login control as well as other login controls, see Login ASP.NET Controls.

Example

The following code example demonstrates how to use a Login control to provide a user interface to log on to a Web site.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.ComponentModel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Function IsValidEmail(ByVal strIn As String) As Boolean
    ' Return true if strIn is in valid e-mail format.
    Return Regex.IsMatch(strIn, ("^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"))
End Function

Sub OnLoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs)
    If Not IsValidEmail(Login1.UserName) Then
        Login1.InstructionText = "Enter a valid e-mail address."
        Login1.InstructionTextStyle.ForeColor = System.Drawing.Color.RosyBrown
        e.Cancel = True
    Else
        Login1.InstructionText = String.Empty
    End If
End Sub

Sub OnLoginError(ByVal sender As Object, ByVal e As EventArgs)
    Login1.HelpPageText = "Help with logging in..."
    Login1.PasswordRecoveryText = "Forgot your password?"
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
        <form id="form1" runat="server">
            <asp:Login id="Login1" runat="server" 
                BorderStyle="Solid" 
                BackColor="#F7F7DE" 
                BorderWidth="1px"
                BorderColor="#CCCC99" 
                Font-Size="10pt" 
                Font-Names="Verdana" 
                CreateUserText="Create a new user..."
                CreateUserUrl="newUser.aspx" 
                HelpPageUrl="help.aspx"
                PasswordRecoveryUrl="getPass.aspx" 
                UserNameLabelText="E-mail address:" 
                OnLoggingIn="OnLoggingIn"
                OnLoginError="OnLoginError" >
                <TitleTextStyle Font-Bold="True" 
                    ForeColor="#FFFFFF" 
                    BackColor="#6B696B">
                </TitleTextStyle>
            </asp:Login>

        </form>
    </body>
</html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.ComponentModel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
bool IsValidEmail(string strIn)
{
    // Return true if strIn is in valid e-mail format.
    return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); 
}

void OnLoggingIn(object sender, System.Web.UI.WebControls.LoginCancelEventArgs e)
{
    if (!IsValidEmail(Login1.UserName))
    {
        Login1.InstructionText = "Enter a valid e-mail address.";
        Login1.InstructionTextStyle.ForeColor = System.Drawing.Color.RosyBrown;
        e.Cancel = true;
    }
    else
    {
        Login1.InstructionText = String.Empty;
    }
}

void OnLoginError(object sender, EventArgs e)
{
    Login1.HelpPageText = "Help with logging in...";
    Login1.PasswordRecoveryText = "Forgot your password?";
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
        <form id="form1" runat="server">
            <asp:Login id="Login1" runat="server" 
                BorderStyle="Solid" 
                BackColor="#F7F7DE" 
                BorderWidth="1px"
                BorderColor="#CCCC99" 
                Font-Size="10pt" 
                Font-Names="Verdana" 
                CreateUserText="Create a new user..."
                CreateUserUrl="newUser.aspx" 
                HelpPageUrl="help.aspx"
                PasswordRecoveryUrl="getPass.aspx" 
                UserNameLabelText="E-mail address:" 
                OnLoggingIn="OnLoggingIn"
                OnLoginError="OnLoginError" >
                <TitleTextStyle Font-Bold="True" 
                    ForeColor="#FFFFFF" 
                    BackColor="#6B696B">
                </TitleTextStyle>
            </asp:Login>

        </form>
    </body>
</html>

See Also

Reference

Login

Other Resources

Login ASP.NET Controls