¿·¤·¤¤µ»ö¤ò½ñ¤¯¤³¤È¤Ç¹¹ð¤ò¾Ã¤¹¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¿·¤·¤¤µ»ö¤ò½ñ¤¯¤³¤È¤Ç¹¹ð¤ò¾Ã¤¹¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
Now I will show you how to raise event in Page and consume it in user
control.
This is much the same like consuming events in Page, but a little different, can
you find it yourself?
ValueChanger.ascx
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="ValueChanger.ascx.cs" Inherits="Jn.Events.ValueChanger" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:Label id="labelValue" runat="server"></asp:Label>
¡¡
ValueChanger.ascx.cs
namespace Jn.Events
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for ValueChanger.
/// </summary>
public class ValueChanger : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label labelValue;
private string _changeMode;
public string ChangeMode
{
get
{
return this._changeMode;
}
set
{
this._changeMode = value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
IValueChange valueChanger = Page as IValueChange;
if(valueChanger != null)
{
valueChanger.ValueChange += new ValueChangeHandler(valueChanger_ValueChange);
}
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void valueChanger_ValueChange(object sender, ValueChangeEventArgs e)
{
const int changer = 5;
if(this.ChangeMode == "Add")
labelValue.Text = (changer + e.NewValue).ToString();
else
labelValue.Text = (changer * e.NewValue).ToString();
}
}
}
¡¡
ValueChangePage.aspx
<%@ Page language="c#" Codebehind="ValueChangePage.aspx.cs"
AutoEventWireup="false" Inherits="Jn.Events.ValueChangePage" %>
<%@ Register TagPrefix="jn" TagName="ValueChanger" Src="ValueChanger.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ValueChange</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox id="textBoxValue" runat="server">3</asp:TextBox>
<asp:Button id="buttonChange" runat="server" Text="Button"></asp:Button><br>
<jn:ValueChanger id="valueChanger1" runat="server" ChangeMode="Add"></jn:ValueChanger><br>
<jn:ValueChanger id="valueChanger2" runat="server"></jn:ValueChanger>
</form>
</body>
</HTML>
¡¡
ValueChangePage.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Jn.Events
{
/// <summary>
/// Summary description for ValueChange.
/// </summary>
public class ValueChangePage : System.Web.UI.Page, IValueChange
{
protected System.Web.UI.WebControls.TextBox textBoxValue;
protected System.Web.UI.WebControls.Button buttonChange;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonChange.Click += new System.EventHandler(this.buttonChange_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void buttonChange_Click(object sender, System.EventArgs e)
{
this.OnValueChange(new ValueChangeEventArgs(int.Parse(textBoxValue.Text)));
}
protected virtual void OnValueChange(ValueChangeEventArgs e)
{
if(this.ValueChange != null)
this.ValueChange(this, e);
}
#region IValueChange Members
public event Jn.Events.ValueChangeHandler ValueChange;
#endregion
}
public delegate void ValueChangeHandler(object sender, ValueChangeEventArgs e);
public class ValueChangeEventArgs : EventArgs
{
private int _newValue;
public int NewValue
{
get
{
return (this._newValue);
}
}
public ValueChangeEventArgs(int newValue)
{
_newValue = newValue;
}
}
public interface IValueChange
{
event ValueChangeHandler ValueChange;
}
}
¡¡
Got the clue?
Raising event is the common tasks in windows-based programming, for example,
onclick, onchange, etc.
With ASP .NET, you can also consume this events as well as create the custom
events.
I will show how easy it is to create and consume the event raised from user
control and consume it in parent control (Page).
Here is the user control (LoginUserControl.ascx):
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="LoginUserControl.ascx.cs" Inherits="Jn.Events.LoginUserControl"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<table border="0">
<tr>
<td>Name</td>
<td><asp:TextBox Runat="server" ID="textBoxName"></asp:TextBox></td>
</tr>
<tr>
<td>Password</td>
<td><asp:TextBox Runat="server" ID="textboxPassword" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td> </td>
<td><asp:Button Runat="server" ID="buttonSubmit" Text="Submit"></asp:Button></td>
</tr>
</table>
And the code-behind (LoginUserControl.ascx.cs):
namespace Jn.Events
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public delegate void LoginEventHandler(object sender, LoginEventArgs e);
public enum LoginStatus
{
Success,
Fail
}
public class LoginEventArgs : EventArgs
{
private LoginStatus _loginStatus;
private string _reason;
public string Reason
{
get
{
return (this._reason);
}
}
public LoginStatus LoginStatus
{
get
{
return (this._loginStatus);
}
}
public LoginEventArgs(LoginStatus loginStatus) : this(loginStatus, string.Empty)
{
}
public LoginEventArgs(LoginStatus loginStatus, string reason)
{
_loginStatus = loginStatus;
_reason = reason;
}
}
/// <summary>
/// Summary description for LoginUserControl.
/// </summary>
public class LoginUserControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.TextBox textBoxName;
protected System.Web.UI.WebControls.TextBox textboxPassword;
protected System.Web.UI.WebControls.Button buttonSubmit;
public event LoginEventHandler Login;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonSubmit.Click += new System.EventHandler(this.buttonSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void buttonSubmit_Click(object sender, System.EventArgs e)
{
if(this.textBoxName.Text == "name" && this.textboxPassword.Text == "password")
this.OnLogin(new LoginEventArgs(LoginStatus.Success));
else
this.OnLogin(new LoginEventArgs(LoginStatus.Fail, "Invalid password or user
name."));
}
protected virtual void OnLogin(LoginEventArgs e)
{
if(this.Login != null)
this.Login(this, e);
}
}
}
¡¡
Then, the Page (LoginPage.aspx)
<%@ Page language="c#" Codebehind="LoginPage.aspx.cs"
AutoEventWireup="false" Inherits="Jn.Events.LoginPage" %>
<%@ Register TagPrefix="jn" TagName="LoginUserControl" Src="LoginUserControl.ascx"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>LoginPage</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label id="labelStatus" runat="server" Font-Bold="True" ForeColor="Red"
EnableViewState="False"></asp:Label><br>
<jn:LoginUserControl id="loginUserControl" runat="server"></jn:LoginUserControl>
</form>
</body>
</HTML>
¡¡
and its code-behind (LoginPage.aspx.cs)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Jn.Events
{
/// <summary>
/// Summary description for LoginPage.
/// </summary>
public class LoginPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label labelStatus;
protected LoginUserControl loginUserControl;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
loginUserControl.Login += new LoginEventHandler(loginUserControl_Login);
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void loginUserControl_Login(object sender, LoginEventArgs e)
{
if(e.LoginStatus == LoginStatus.Fail)
{
labelStatus.Text = e.Reason;
}
else
{
labelStatus.Text = "Success!!";
}
}
}
}
¡¡
Now, compile and run to see the result.


