用户控件的属性和事件使用

练习写了一个有关用户控件的东西。是有关用户控件的属性和事件的。

用户控件如下:

属性:设置用户控件的Lable1Text属性来改变控件在页面Label显示的text。如下,label1Text为登 录时:

效果如下:

事件:通过控件外的事件触发控件的事件

通过控件内的事件触发控件外的操作,点击控件里面的button如下:

 LogInOutControl.ascx代码如下:

<%@ 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" align="center">
       <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </TD>
</TR>
<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"></asp:Button>
     <asp:Button id="ButtonLogOut" Width="49px" Text="注销" runat="server"></asp:Button></FONT></TD>
</TR>
</TABLE>

 

LogInOutControl.ascx.cs如下:

 

/ 定义代理
public delegate void LogInOutClickHandler(object sender, LogInOutEventArgs e);


public partial class LogInOutControl : System.Web.UI.UserControl
{

    //protected System.Web.UI.WebControls.Button ButtonLogIn;
    //protected System.Web.UI.WebControls.TextBox TextBoxUserName;
    //protected System.Web.UI.WebControls.TextBox TextBoxPassword;
    //protected System.Web.UI.WebControls.Button ButtonLogOut;
    //protected System.Web.UI.WebControls.Label LabelUser;
    //protected System.Web.UI.WebControls.Label LabelPassword;
    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 = "注销";
                }
            }
        }
    }

    private string label1Text = "aa";
    //DescriptionAttribute:当用户在属性浏览器里选择属性的时候,
    //description里指定的文本会显示在属性浏览器的下边,向用户显示属性的功能。
    [DescriptionAttribute("label的文字")]
    //DefaultValueAttribute:为一个简单类型的属性设置一个默认值。
    [DefaultValueAttribute("登录")]
    //CategoryAttribute:描述一个属性或事件的类别,当使用类别的时候,
    //属性浏览器按类别将属性分组。
    [CategoryAttribute("Appearance")]


    public virtual string Lable1Text
    {
        get { return this.label1Text; }
        set { this.label1Text = value; }
    }  


    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = label1Text;
        if (this.LabelUser.Text == "User:")
            this.language = Language.English;
        else
            this.language = Language.Chinese;
    }
    private void OnLogInOutClick(object sender, LogInOutEventArgs e)
    {
        if (LogInOutClick != null)
            LogInOutClick(this, e);
    }
    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
        InitializeComponent();
        base.OnInit(e);

    }
    
private void InitializeComponent()
   {
    this.ButtonLogIn.Click += new System.EventHandler(this.ButtonLogIn_Click);
    this.ButtonLogOut.Click += new System.EventHandler(this.ButtonLogOut_Click);
    this.Load += new System.EventHandler(this.Page_Load);
   }
   #endregion
   private void ButtonLogIn_Click(object sender, System.EventArgs e)
   {
    OnLogInOutClick(this,new LogInOutEventArgs(LogInClickType.LongIn,CustomValidate(this.TextBoxUserName.Text,this.TextBoxPassword.Text)));
   }
   private void ButtonLogOut_Click(object sender, System.EventArgs e)
   {
    //注销代码省略
    OnLogInOutClick(this,new LogInOutEventArgs(LogInClickType.LongOut,true));
   }
   //验证函数
   private bool CustomValidate(string userName,string password)
   {
    //验证代码省略,假设通过
    return true;
   }

}

 

default.aspx代码如下:

 

<body>
    <form id="form1" runat="server">
    <div>
   
        <uc1:LogInOutControl ID="LogInOutControl1" runat="server" Lable1Text="登 录" />
   
    </div>
        <asp:Label id="LabelMsg" runat="server"></asp:Label>
     <asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True">
      <asp:ListItem Value="0" Selected="True">中文</asp:ListItem>
      <asp:ListItem Value="1">英文</asp:ListItem>
     </asp:DropDownList></FONT>

    </form>
</body>

 

 

default.aspx.cs如下:

public partial class _Default : System.Web.UI.Page
{
   // protected System.Web.UI.WebControls.Label LabelMsg;
   //protected System.Web.UI.WebControls.DropDownList DropDownList1;
   //protected LogInOutControl LogInOutControl1;
   private void Page_Load(object sender, System.EventArgs e)
   {
    //注册用户控件事件
    this.LogInOutControl1.LogInOutClick += new LogInOutClickHandler(LogInOutControl1_LogInOutClick);
   }
   #region Web 窗体设计器生成的代码
   override protected void OnInit(EventArgs e)
   {
    InitializeComponent();
    base.OnInit(e);
   }
   private void InitializeComponent()
   {   
    this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load);
   }
   #endregion
   private void LogInOutControl1_LogInOutClick(object sender, LogInOutEventArgs e)
   {
    switch(e.Type)
    {
     case LogInClickType.LongIn:
      this.LabelMsg.Text = "你点击了登录按钮,操作结果:"+e.Result.ToString();
      break;
     case LogInClickType.LongOut:
      this.LabelMsg.Text = "你点击了注销按钮,操作结果:"+e.Result.ToString();
      break;
    }
   }
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
   {
    this.LogInOutControl1.Lg = (Language)this.DropDownList1.SelectedIndex;
    //this.LogInOutControl1.ChangeLanguage((Language)this.DropDownList1.SelectedIndex);
   }
}

 

LogInOutEventArgs.cs

 

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

    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 enum LogInClickType : int
{
    LongIn,
    LongOut
}
//定义语言
public enum Language
{
    Chinese,
    English
}

 

 

 

 

posted @ 2010-01-15 18:04  哈仔  阅读(521)  评论(0编辑  收藏  举报