Event Hander Question

C

Chris Fink

I have a question about the following code. I don't understand how and where
the delegate gets created that allows for the calling of the OnClick when the
button is clicked? For example,

// Invokes delegate registered with the Click event.
protected virtual void OnClick(EventArgs e) {
if (Click != null) {
Click(this, e);
}
}

Where is the Click method defined and where is the delegate?


// SimpleButton.cs
// Developing Microsoft ASP.NET Server Controls and Components
// Copyright © 2002, Nikhil Kothari and Vandana Datye
//

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MSPress.ServerControls {

// The SimpleButton control participates
// in postback event handling by implementing
// the IPostBackEventHandler interface. It
// exposes a Click event which it raises in the
// IPostBackEventHandler.RaisePostBackEvent method.

[
// The DefaultEventAttribute allows a page
// developer to attach a handler to the default
// event by double-clicking on the control.
DefaultEvent("Click"),
DefaultProperty("Text")
]
public class SimpleButton: WebControl, IPostBackEventHandler {

[
Bindable(true),
Category("Behavior"),
DefaultValue(""),
Description("The text to display on the button")
]
public virtual string Text {
get {
string s = (string)ViewState["Text"];
return((s == null) ? String.Empty : s);
}
set {
ViewState["Text"] = value;
}
}

protected override HtmlTextWriterTag TagKey {
get {
return HtmlTextWriterTag.Input;
}
}

[
Category("Action"),
Description("Raised when the button is clicked")
]
public event EventHandler Click;

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Name,this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Type,"Submit");
writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);

}

// Method of IPostBackEventHandler that raises postback events.
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument){

OnClick(EventArgs.Empty);
}

// Invokes delegate registered with the Click event.
protected virtual void OnClick(EventArgs e) {
if (Click != null) {
Click(this, e);
}
}

protected override void Render(HtmlTextWriter writer) {
// Ensures that this control is nested in a server form.
if (Page != null) {
Page.VerifyRenderingInServerForm(this);
}
base.Render(writer);
}
}
}

// simpleButtonTest.aspx
<%@ Page Language="C#" %>
<%@ Register TagPrefix="mspuc" TagName="SiteHeader"
Src="~/UserControls/SiteHeader.ascx" %>
<%@ Register TagPrefix="mspuc" TagName="SiteFooter"
Src="~/UserControls/SiteFooter.ascx" %>
<%@ Register TagPrefix="msp" Namespace="MSPress.ServerControls"
Assembly="MSPress.ServerControls" %>
<html>
<head>
<link href="../Default.css" type="text/css" rel="stylesheet" />
<script runat="server">
void simpleButton1_Click(object sender, EventArgs e) {
label1.Text = "You clicked the button.";
}
</script>
</head>
<body>
<form runat="server">
<br>
<mspuc:SiteHeader id="SiteHeader1" runat="server"
SubHeading="SimpleButton test page" Heading="Chapter 9" />
<br>
Click the button to raise its server-side Click event.
<br>
<msp:SimpleButton Text="Submit" Runat="server"
OnClick="simpleButton1_Click" id="simpleButton1" />
<br>
<asp:Label Font-Size="10pt" Font-Bold="true" id="label1"
runat="server" />
<br>
<mspuc:SiteFooter id="SiteFooter1" runat="server" />
</form>
</body>
</html>
 
J

John Saunders

Chris Fink said:
I have a question about the following code. I don't understand how and
where
the delegate gets created that allows for the calling of the OnClick when
the
button is clicked? For example,

// Invokes delegate registered with the Click event.
protected virtual void OnClick(EventArgs e) {
if (Click != null) {
Click(this, e);
}
}

Where is the Click method defined and where is the delegate?

Click isn't a method. It's an event. You declared it with "public event
EventHandler Click;". You can think of it as a property of type
EventHandler, which is a delegate type.


John Saunders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top