演示事件(Event)怎样使用以及怎样为用户控件添加一个事件(示例代码下载)

(一).说明

程序演示用户控件得到父窗体的数据进行身份验证.

(二) .图片演示

(三). 核心代码部分

namespace UserControl添加事件
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
///Login 的摘要说明。
/// </summary>
public class Login : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox txtUserName;
protected System.Web.UI.WebControls.TextBox txtPassword;

public event System.EventHandler EventGetData; //验证事件


protected System.Web.UI.WebControls.Button Button4;

public string SetUserName
{
set
{
this.txtUserName.Text = value;
}
}
public string SetPassword
{
set
{
this.txtPassword.Text = value;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
///设计器支持所需的方法 - 不要使用代码编辑器
///修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button4.Click += new System.EventHandler(this.Button4_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

//从父窗体取得值
private void Button1_Click(object sender, System.EventArgs e)
{
if(EventGetData != null)
{
EventGetData(sender,e);
}
}

//校验
private void Button4_Click(object sender, System.EventArgs e)
{
if(this.txtUserName.Text == "ChengKing" && this.txtPassword.Text == "123")
{
Response.Write("<script language=javascript> window.alert('输入正确!');</script>");
}
else
{
Response.Write("<script language=javascript> window.alert('错误的用户名和密码!');</script>");
}
}
}
}

(四).源代码下载

http://www.cnblogs.com/Files/ChengKing/UserControl添加事件.rar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值