Capturing postback events in a custom control rendering
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcontrolscs' list.


Michael
Hi all....

The code below doesn't work, RaisePostBackEvent never gets called. If I
change the type from image to submit it works fine, there's only a problem
when the type is set to image.

I've also tried implementing the IPostBackDataHandler interface instead,
also with no luck. I'm just not understanding whats going on here as the
Webcontrols.Imagebutton renders almost identical html and it seems to be
able to process postback data fine. Why isn't my control's
RaisePostBackEvent being called ? Can someone help me out here ?

MyInputButton.cs:-

using System;
using System.Web.UI;

namespace TestControl
{
/// <summary>
/// Summary description for MyInputButton.
/// </summary>
public class MyInputButton : System.Web.UI.Control, IPostBackEventHandler
{
// Defines the Click event.
public event EventHandler Click;

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

// Method of IPostBackEventHandler that raises change events.
public void RaisePostBackEvent(string eventArgument)
{
OnClick(EventArgs.Empty);
}

protected override void Render(HtmlTextWriter output)
{
output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
output.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
output.AddAttribute(HtmlTextWriterAttribute.Type, "image");
output.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
// output.AddAttribute(HtmlTextWriterAttribute.Onclick,
Page.GetPostBackClientEvent(this, "tstValue"));
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
}
}
}

webform6.aspx:-
<%@ Page language="c#" Codebehind="WebForm6.aspx.cs" AutoEventWireup="false"
Inherits="testforms.WebForm6" %>
<%@ Register TagPrefix="cc1" Namespace="TestControl" Assembly="TestControl"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm6</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/nav4-0";>
</HEAD>
<body>
<form id="WebForm6" method="post" runat="server">
<P>
<cc1:MyInputButton id="MyInputButton1"
runat="server"></cc1:MyInputButton></P>
<P>
<asp:Label id="Label1" runat="server">Label</asp:Label></P>
<P>
<asp:ImageButton id="ImageButton1" runat="server"
ImageUrl="sbluezi.gif"></asp:ImageButton></P>
</form>
</body>
</HTML>

webform6.aspx.cs:-
using System;

namespace testforms
{
/// <summary>
/// Summary description for WebForm6.
/// </summary>
public class WebForm6 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected TestControl.MyInputButton MyInputButton1;
protected System.Web.UI.WebControls.ImageButton ImageButton1;

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
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.ImageButton1.Click += new
System.Web.UI.ImageClickEventHandler(this.testit);
this.MyInputButton1.Click += new System.EventHandler(this.testit);
}
#endregion

private void testit(object sender, System.Web.UI.ImageClickEventArgs e)
{
Label1.Text = "It Works!";
}

private void testit(object sender, System.EventArgs e)
{
Label1.Text = "This works too!";
}
}
}
Reply to this message...
 
    
Vandana Datye (DevCepts)
When the type attribute is "submit", the name (UniqueID) is posted by =
the browser. On the server, the page then uses the UniqueID to find your =
control and invoke its RaisePostBackEvent method.

When the type attribute is "image", the posted form data contains =
co-ordinates of the click appended to the name, i.e., the form data does =
not contain the name(UniqueID) but UniqueID.x and UniqueID.y. On the =
server, the page is unable to find your control using the posted data.=20

If you want to render an image attribute to get the image click =
co-ordinates, you'll have to do a bit more work. Implement =
IPostBackDataHandler to recover the coordinates from the =
NameValueCollection. In addition, invoke =
Page.RegisterRequiresPostBack(this) in PreRender so that LoadPostData is =
invoked by the page. Also, from LoadPostData invoke =
Page.RegisterRequiresRaiseEvent(this) to make sure that =
RaisePostBackEvent gets called.

Vandana=20
-----Original Message-----
From: Michael [mailto:Click here to reveal e-mail address]=20
Sent: Friday, April 26, 2002 8:58 AM
To: aspngcontrolscs
Subject: [aspngcontrolscs] Capturing postback events in a custom control =
rendering <Input type=3Dimage>

Hi all....=A0=20
=A0
The code below doesn't work, RaisePostBackEvent never gets called.=A0 If =
I change the type from image to submit it works fine, there's only a =
problem when the type is set to image.=A0
=A0
I've also tried implementing the IPostBackDataHandler interface instead, =
also with no luck.=A0 I'm just not understanding whats going on here as =
the Webcontrols.Imagebutton renders almost identical html and it seems =
to be able to process postback data fine.=A0 Why isn't my control's =
RaisePostBackEvent being called ? Can someone help me out here ?
=A0
MyInputButton.cs:-
=A0
using System;
using System.Web.UI;
=A0
namespace TestControl
{
=A0/// <summary>
=A0/// Summary description for MyInputButton.
=A0/// </summary>
=A0public class MyInputButton : System.Web.UI.Control, =
IPostBackEventHandler
=A0{
=A0=A0// Defines the Click event.
=A0=A0public event EventHandler Click;
=A0=A0=A0=A0=A0=20
=A0=A0// Invokes delegates registered with the Click event.
=A0=A0protected virtual void OnClick(EventArgs e)=20
=A0=A0{=A0=A0=A0=A0=20
=A0=A0=A0if (Click !=3D null)=20
=A0=A0=A0{
=A0=A0=A0=A0Click(this, e);
=A0=A0=A0}=A0=20
=A0=A0}
=A0=A0=A0=A0=A0=20
=A0=A0// Method of IPostBackEventHandler that raises change events.
=A0=A0public void RaisePostBackEvent(string eventArgument)
=A0=A0{=A0=A0=A0=A0=20
=A0=A0=A0OnClick(EventArgs.Empty);
=A0=A0}
=A0=A0=A0=A0=A0=20
=A0=A0protected override void Render(HtmlTextWriter output)=20
=A0=A0{
=A0=A0=A0output.AddAttribute(HtmlTextWriterAttribute.Name, =
this.UniqueID);
=A0=A0=A0output.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
=A0=A0=A0output.AddAttribute(HtmlTextWriterAttribute.Type, "image");
=A0=A0=A0output.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
//=A0=A0=A0output.AddAttribute(HtmlTextWriterAttribute.Onclick, =
Page.GetPostBackClientEvent(this, "tstValue"));
=A0=A0=A0output.RenderBeginTag(HtmlTextWriterTag.Input);
=A0=A0=A0output.RenderEndTag();=A0
=A0=A0}
=A0}
}
webform6.aspx:-
<%@ Page language=3D"c#" Codebehind=3D"WebForm6.aspx.cs" =
AutoEventWireup=3D"false" Inherits=3D"testforms.WebForm6" %>
<%@ Register TagPrefix=3D"cc1" Namespace=3D"TestControl" =
Assembly=3D"TestControl" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
=A0<HEAD>
=A0=A0<title>WebForm6</title>
=A0=A0<meta name=3D"GENERATOR" Content=3D"Microsoft Visual Studio 7.0">
=A0=A0<meta name=3D"CODE_LANGUAGE" Content=3D"C#">
=A0=A0<meta name=3D"vs_defaultClientScript" content=3D"JavaScript">
=A0=A0<meta name=3D"vs_targetSchema" =
content=3D"http://schemas.microsoft.com/intellisense/nav4-0";>
=A0</HEAD>
=A0<body>
=A0=A0<form id=3D"WebForm6" method=3D"post" runat=3D"server">
=A0=A0=A0<P>
=A0=A0=A0=A0<cc1:MyInputButton id=3D"MyInputButton1" =
runat=3D"server"></cc1:MyInputButton></P>
=A0=A0=A0<P>
=A0=A0=A0=A0<asp:Label id=3D"Label1" =
runat=3D"server">Label</asp:Label></P>
=A0=A0=A0<P>
=A0=A0=A0=A0<asp:ImageButton id=3D"ImageButton1" runat=3D"server" =
ImageUrl=3D"sbluezi.gif"></asp:ImageButton></P>
=A0=A0</form>
=A0</body>
</HTML>
webform6.aspx.cs:-
using System;
=A0
namespace testforms
{
=A0/// <summary>
=A0/// Summary description for WebForm6.
=A0/// </summary>
=A0public class WebForm6 : System.Web.UI.Page
=A0{
=A0=A0protected System.Web.UI.WebControls.Label Label1;
=A0=A0protected TestControl.MyInputButton MyInputButton1;
=A0=A0protected System.Web.UI.WebControls.ImageButton ImageButton1;
=A0
=A0=A0#region Web Form Designer generated code
=A0=A0override protected void OnInit(EventArgs e)
=A0=A0{
=A0=A0=A0InitializeComponent();
=A0=A0=A0base.OnInit(e);
=A0=A0}
=A0=A0
=A0=A0/// <summary>
=A0=A0/// Required method for Designer support - do not modify
=A0=A0/// the contents of this method with the code editor.
=A0=A0/// </summary>
=A0=A0private void InitializeComponent()
=A0=A0{=A0=A0=A0=20
=A0=A0=A0this.ImageButton1.Click +=3D new =
System.Web.UI.ImageClickEventHandler(this.testit);
=A0=A0=A0this.MyInputButton1.Click +=3D new =
System.EventHandler(this.testit);
=A0=A0}
=A0=A0#endregion
=A0
=A0=A0private void testit(object sender, =
System.Web.UI.ImageClickEventArgs e)
=A0=A0{
=A0=A0=A0Label1.Text =3D "It Works!";
=A0=A0}
=A0
=A0=A0private void testit(object sender, System.EventArgs e)
=A0=A0{
=A0=A0=A0Label1.Text =3D "This works too!";
=A0=A0}
=A0}
}
=A0
=A0
| [aspngcontrolscs] member Click here to reveal e-mail address =3D YOUR ID | =
http://www.asplists.com/asplists/aspngcontrolscs.asp =3D JOIN/QUIT | =
http://www.asplists.com/search =3D SEARCH Archives=20

Reply to this message...
 
 
System.Attribute
System.Collections.Specialized.NameValueCollection
System.EventArgs
System.EventHandler
System.Web.UI.Control
System.Web.UI.HtmlTextWriter
System.Web.UI.HtmlTextWriterAttribute
System.Web.UI.HtmlTextWriterTag
System.Web.UI.ImageClickEventArgs
System.Web.UI.ImageClickEventHandler
System.Web.UI.IPostBackDataHandler
System.Web.UI.IPostBackEventHandler
System.Web.UI.Page
System.Web.UI.WebControls.ImageButton
System.Web.UI.WebControls.Label




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com