Vs.net 2005控件(Web Controls)小例----值外传

 

Default.apsx 前台代码:

<%@ Register TagPrefix="uc1" TagName="LogInOutControl" Src="LogInOutControl.ascx" %>
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<FONT face="宋体">
    
<uc1:LogInOutControl id="LogInOutControl1" runat="server">
    
</uc1:LogInOutControl>
    
<asp:Label id="LabelMsg" runat="server"></asp:Label>
    
<asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
     
<asp:ListItem Value="0" Selected="True">中文</asp:ListItem>
     
<asp:ListItem Value="1">英文</asp:ListItem>
    
</asp:DropDownList></FONT>
 
</div>
    
</form>
</body>
</html>

后台代码:

using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
this.LogInOutControl1.LogInOutClick += new LogInOutClickHandler(LogInOutControl1_LogInOutClick);
    }

    
private void LogInOutControl1_LogInOutClick(object sender, LogInOutEventArgs e)
    
{
        
switch (e.Type)
        
{
            
case LogInClickType.LongIn:
                
this.LabelMsg.Text = e.UserName + "你点击了登录按钮,操作结果:" + e.Result.ToString();
                
break;
            
case LogInClickType.LongOut:
                
this.LabelMsg.Text = "你点击了注销按钮,操作结果:" + e.Result.ToString();
                
break;
        }

    }

    
protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    
{
        
this.LogInOutControl1.Lg = (Language)this.DropDownList1.SelectedIndex;
    }

}

 

控件前台代码:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LogInOutControl.ascx.cs" Inherits="LogInOutControl" %>
<TABLE id="Table1" style="FONT-SIZE: 9pt; WIDTH: 183px; HEIGHT: 125px" cellSpacing="1"
 cellPadding
="1" width="183" align="center" border="1">
 
<TR>
  
<TD height="20">
   
<asp:Label id="LabelUser" runat="server">用户:</asp:Label>
   
<asp:TextBox id="TextBoxUserName" Width="128px" runat="server"></asp:TextBox></TD>
 
</TR>
 
<TR>
  
<TD height="20"><FONT face="宋体">
    
<asp:Label id="LabelPassword" runat="server">密码:</asp:Label>
    
<asp:TextBox id="TextBoxPassword" Width="128px" runat="server" TextMode="Password"></asp:TextBox></FONT></TD>
 
</TR>
 
<TR>
  
<TD align="center" height="20"><FONT face="宋体">
    
<asp:Button id="ButtonLogIn" Width="50px" Text="登录" runat="server" OnClick="ButtonLogIn_Click"></asp:Button>
    
<asp:Button id="ButtonLogOut" Width="49px" Text="注销" runat="server" OnClick="ButtonLogOut_Click"></asp:Button></FONT></TD>
 
</TR>
</TABLE>

控件后台代码:

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 delegate void LogInOutClickHandler(object sender, LogInOutEventArgs e);

public partial class LogInOutControl : System.Web.UI.UserControl
{
    
public event LogInOutClickHandler LogInOutClick;
    
private Language language;
    
public void ChangeLanguage(Language language)
    
{
        
this.Lg = language;
    }

    
public Language Lg
    
{
        
set
        
{
            
if (value != this.language)
            
{
                
if (value == Language.English)
                
{
                    
this.LabelUser.Text = "User:";
                    
this.LabelPassword.Text = "Password:";
                    
this.ButtonLogIn.Text = "LogIn";
                    
this.ButtonLogOut.Text = "LogOut";
                }

                
else
                
{
                    
this.LabelUser.Text = "用户:";
                    
this.LabelPassword.Text = "密码:";
                    
this.ButtonLogIn.Text = "登录";
                    
this.ButtonLogOut.Text = "注销";
                }

            }

        }

    }


    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (this.LabelUser.Text == "User:")
            
this.language = Language.English;
        
else
            
this.language = Language.Chinese;

    }


    
private void OnLogInOutClick(object sender, LogInOutEventArgs e)
    
{
        e.UserName 
= this.TextBoxUserName.Text;
        
if (LogInOutClick != null)
            LogInOutClick(
this, e);
    }


    
protected void ButtonLogIn_Click(object sender, EventArgs e)
    
{
        OnLogInOutClick(
thisnew LogInOutEventArgs(LogInClickType.LongIn, CustomValidate(this.TextBoxUserName.Text, this.TextBoxPassword.Text)));
    }


    
private bool CustomValidate(string p, string p_2)
    
{
        
return true;
    }

    
protected void ButtonLogOut_Click(object sender, EventArgs e)
    
{
        OnLogInOutClick(
thisnew LogInOutEventArgs(LogInClickType.LongOut, true));
    }

}

用于控件和主页面之间传值用的类

using System;
using System.Data;
using System.Configuration;
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;

/// <summary>
/// Summary description for LogInOutEventArgs
/// </summary>

public class LogInOutEventArgs : EventArgs
{
    
private LogInClickType type;
    
private bool result;
    
private string userName;

    
public LogInOutEventArgs(LogInClickType type,bool result):base()
    
{
        
this.type = type;
        
this.result = result;
    }

    
public LogInClickType Type
    
{
        
get return this.type; }
    }

    
public bool Result
    
{
        
get return this.result; }
    }


    
public string UserName
    
{
        
get return userName; }
        
set { userName = value; }
    }

}

public enum LogInClickType : int
{
    LongIn,
    LongOut
}

public enum Language
{
    Chinese,
    English
}




 

posted @ 2006-10-16 21:08  RicoRui  阅读(701)  评论(0编辑  收藏  举报