Help!!! - Why this doesnt raise an event

A

Alfred Tascon

Taken from an MSPress Book and modified a little:
// 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 MyWeb.Controls
{

// 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")
]
public class SimpleButton: WebControl, IPostBackEventHandler
{

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,"image");
writer.AddAttribute(HtmlTextWriterAttribute.Value, "");
writer.AddAttribute(HtmlTextWriterAttribute.Src, "images/or.bmp");
}

// Method of IPostBackEventHandler that raises postback events.
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
if (Click != null)
{
Click(this, EventArgs.Empty);
}
}

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);
}
}
}

******* The aspx page(called default.aspx):
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false"
Inherits="MyWeb.default" %>
<%@ Register TagPrefix="cc1" Namespace="MyWeb.Controls" Assembly="MyWeb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>default</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 MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<cc1:SimpleButton id="SimpleButton1" style="Z-INDEX: 101; LEFT: 32px;
POSITION: absolute; TOP: 16px"
runat="server"></cc1:SimpleButton>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 40px; POSITION:
absolute; TOP: 48px" runat="server">Nothing Clicked</asp:Label>
</form>
</body>
</HTML>


******** The aspx.cs file (called default.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 MyWeb
{
/// <summary>
/// Summary description for default.
/// </summary>
public class default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected MyWeb.Controls.SimpleButton SimpleButton1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
int i=0;
}

#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.SimpleButton1.Click += new
System.EventHandler(this.SimpleButton1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void SimpleButton1_Click(object sender, System.EventArgs e)
{
Label1.Text = "Clicked";
}
}
}


*************************
I have not been able to get the control to raise an event.
Page_Load is being called after I click on the control, but no event.
Funny if I change the "type" from "image" back to "submit", events are
raised again.

Does anyone know what else has to be done to get "image" types to work?
How does ImageButton do it?

Please help !!!!


Thanks in advance

Alfred
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top