[ Download Source Code ]
Handling the Click Event
In order to be able to process the data entered by the user, we need to have a method that handles the Click event of the Submit button. The following code is used to register an event handler for the button’s Click event.
_SubmitButton.Click += new EventHandler(OnBtnLink_Click);
The Click event is defined as follows:
/* The sole event hook that the button exposes. */
public event EventHandler Click;
The button’s Click event handler is defined as follows:
/* Event handling for the Login Button */
protected virtual void OnBtnLink_Click(object sender, EventArgs e)
{
if (Click != null)
{
Click(this, e);
UserName = string.Empty;
Password = string.Empty;
}
}
This event handler means that when the user clicks the Submit button, the Click event of the Login control is bubbled up to the page. This way, the page developer can define a method to handle its Click event:
private void Login1_Click(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Response.Write("UserName : s" + Login1.UserName);
Response.Write("<br />Password : " + Login1.Password);
}
/// <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);
}
In the InitializeComponent() method, you need to attach an event handler for the Login control’s Click event. In the above example, we are just writing out to the page whatever the user enters in the UserName and Password text boxes. In a real application, these values would be validated against a data store of valid credentials.