<input type="image"... based custom control

A

Alfred Tascon

I have created one of these controls but arent getting any call backs.

I've got the source from the MSPress book "Developing Microsoft ASP.NET
Server Controls and Components" for SimpleButton.cs (off Chapter 9) and
modified the "type" attribute to "image" plus added "src" attrinbute.

After clicking on the image the page is being called (I can breakpoint on
Page_Load) but the event is lost.

I was reading MSDN and it mentions about using
"System.Web.UI.ImageClickEventHandler" for any custom image based controls,
but no more information. I tried changing EventHandler to this (and
everything else that must be changed to cope with this type) but the event
is still lost.

I was wondering if anyone knew what more does one have to do in the control
to get events to work?


Thanks in advance,

Alfred
 
D

Danny Bloodworth

Alfred,

Is autopostback set to true on the control?
Post some code and maybe I will be able to help. Kinda tough to tell
without seeing it.


DB
 
A

Alfred Tascon

Danny Bloodworth said:
Alfred,

Is autopostback set to true on the control?
Post some code and maybe I will be able to help. Kinda tough to tell
without seeing it.


DB

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
 
D

Danny Bloodworth

Alfred,

The only thing I can offer is that when you change the type to image,
you have to specify that the control should postback where the submit
button is inherently going to postback. Try adding an attribute to
the control for autopostback and set the value to True. I am a VB
guy, and I'm having a bit of trouble getting the C# stuff in the code,
but it seems that with an image as the base control, you would have to
tell it to postback.

HTH,

Danny
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top