程序演示用户控件得到父窗体的数据 进行身份验证.
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>");
}
}
}
}
下载演示:
https://files.cnblogs.com/zhangweiguo3984/UserControl添加事件.rar