Web 窗體用戶控件(3)

loginTest.aspx 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="loginTest.aspx.cs" Inherits="loginTest" %>

<%@ Register Src="mylogin.ascx" TagName="mylogin" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>無標題頁</title>
    
<style type="text/css">
        td
{
            font-size
:12px;
        
}

    
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div style="text-align: center">
        
<uc1:mylogin ID="Mylogin1" runat="server" />
        
<br />
        
<asp:Label ID="Label1" runat="server"></asp:Label></div>
    
</form>
</body>
</html>


loginTest.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class loginTest : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (Page.IsPostBack)
        
{
            
//注意這下面一行Page.Validate(),如果不寫就出錯
            
//因爲驗證控件是在自定義控件 mylogin.ascx 上面的
            
//故在login.aspx 這一頁面還要讓頁面驗證一下(觸發頁面驗證)
            Page.Validate();
            
            
//下面這個處理事件,其實應該是放在自定義控件裏面的代碼中的。
            if (Mylogin1.IsValid)
            
{
                Label1.Text 
+= "歡迎:" + Mylogin1.UserID + ","
                            
+ "密碼是:" + Mylogin1.UserPwd;
            }

        }

    }


}

 

 

自定義控件 mylogin.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="mylogin.ascx.cs" Inherits="mylogin" %>
<table border="1" cellpadding="3">
    
<tr>
        
<td colspan="3" style="background-color: <% =BackColor%>; text-align: center">
            用戶登錄
</td>
    
</tr>
    
<tr>
        
<td style="width: 100px; height: 32px">
            用戶名:
</td>
        
<td style="width: 100px; height: 32px">
            
<asp:TextBox ID="txtUserID" runat="server"></asp:TextBox></td>
        
<td style="width: 100px; height: 32px">
            
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtUserID"
                Display
="Dynamic" ErrorMessage="用戶名不能爲空"></asp:RequiredFieldValidator></td>
    
</tr>
    
<tr>
        
<td style="width: 100px; height: 27px">
            密碼:
</td>
        
<td style="width: 100px; height: 27px">
            
<asp:TextBox ID="txtUserPwd" runat="server"></asp:TextBox></td>
        
<td style="width: 100px; height: 27px">
            
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtUserPwd"
                Display
="Dynamic" ErrorMessage="密碼不能爲空" Height="18px"></asp:RequiredFieldValidator>
            
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtUserPwd"
                Display
="Dynamic" ErrorMessage="密碼不能小於五個字符" ValidationExpression="[0-9a-zA-Z]{5,}"></asp:RegularExpressionValidator></td>
    
</tr>
    
<tr>
        
<td colspan="3" style="text-align: center; height: 89px;">
            
<asp:Button ID="Button1" runat="server" Text="登錄" OnClick="Button1_Click" />
            
<br />
            
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
        
</td>
    
</tr>
</table>
<asp:Label ID="Label1" runat="server"></asp:Label>

 

mylogin.ascx.cs

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class mylogin : System.Web.UI.UserControl
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
public string BackColor = "red";
    
public string UserID
    
{
        
get {
            
return txtUserID.Text;
        }

        
set {
            txtUserID.Text 
= value;
        }

    }


    
public string UserPwd
    
{
        
get {
            
return txtUserPwd.Text;
        }

        
set {
            txtUserPwd.Text 
= value;
        }

    }


    
public bool IsValid {
        
get {
            
return Page.IsValid;
        }

    }



    
//下面這個處理方法我又寫在這裏面了
    
//是應該寫在這裏面的
    protected void Button1_Click(object sender, EventArgs e)
    
{
        
//寫在裏面了就不用再去 Page.Validate()一下了。
        if (Page.IsValid)
        
{
            Label1.Text 
+= "歡迎:" + UserID + ","
                        
+ "密碼是:" + UserPwd;
        }

    }

}

發佈了28 篇原創文章 · 獲贊 3 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章