但是這樣還不夠,我們需要獲取用戶輸入的用戶名和密碼,其中比較好的一種做法是自定義擴展EventArgs類:
Code
public class LoginEventArgs : EventArgs
{
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
private string passWord;
public string PassWord
{
get { return passWord; }
set { passWord = value; }
}
}
定義一個登錄委托類:
Code
public delegate void LoginEventHandler(object sender, LoginEventArgs e);
修改一下剛才的代碼:
Code
public partial class WebUserControl : System.Web.UI.UserControl
{
public event LoginEventHandler LoginClick;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
LoginEventArgs loginE = new LoginEventArgs();
loginE.UserName = TextBox1.Text;
loginE.PassWord = TextBox2.Text;
LoginClick(sender, loginE);
}
}
Code
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Login_Click(object sender, LoginEventArgs e)
{
Response.Write("<script>alert('UserName:"+e.UserName+",Password:"+e.PassWord+"')</script>");
}
}
運行結果如圖: