Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
RaisePostBackEvent not firing
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00777227
Message ID:
00777495
Vues:
19
Here is sample of one. You need to raise a postback event on the client to cause the postback to occur.
// (1) IPostBackEventHandler interface  allows PostBack event processing.
[DefaultEvent("Click")] public class AlertButtonThree : System.Web.UI.WebControls.WebControl, IPostBackEventHandler
{

	// (2) Declare postback event
	public event EventHandler Click;

	// (3) Render an HTML element that can detect user events.
	protected override void Render(HtmlTextWriter output)
	{
		// Write a title
		output.Write("<h3>Rendered Control</h3>");
		// Add some attributes.
		output.AddAttribute("value", "Custom Button");
		output.AddAttribute("type", "button");
		// (4) Add attribute to raise Postback event on client.
		output.AddAttribute("onclick", "javascript:alert('Howdy!');" +
			Page.GetPostBackEventReference(this));
		// Opens an Input HTML tag (inserts "<INPUT").
		output.RenderBeginTag("INPUT");
		// Close the Input HTML tag (inserts ">").
		output.RenderEndTag();
	}

	// Part of the IPostBackEventHandler interface.
	// you must create this method.
	public void RaisePostBackEvent(string eventArgument)
	{
		// (5) Call the event method to raise event.
		OnClick(EventArgs.Empty);
	}

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

}
>>Paul,
>>
>>You code seemed fine. I would create a new simple page and test use a simple control that implements the postback and see if that works.
>>
>
>I just created a new web project. I created a new class with the code:
>
>
>using System;
>using System.Web.UI;
>using System.Web.UI.WebControls;
>using System.ComponentModel;
>
>namespace SampleControls
>{
>	/// <summary>
>	/// Summary description for SimpleControl.
>	/// </summary>
>	[DefaultProperty("Text"),
>	ToolboxData("<{0}:kaSimpleControl runat=server></{0}:SimpleControl>")]
>	public class SimpleControl : System.Web.UI.WebControls.WebControl, IPostBackEventHandler
>	{
>		protected string text = "Sample";
>
>		[Bindable(true),
>		Category("Appearance"),
>		DefaultValue("")]
>		public string Text
>		{
>			get
>			{
>				return text;
>			}
>
>			set
>			{
>				text = value;
>			}
>		}
>
>		protected override void Render(HtmlTextWriter output)
>		{
>			string sampleHTML = "<b>" + this.text + "</b>";
>		
>			output.Write(sampleHTML);
>		}
>
>		public void RaisePostBackEvent(string eventArgument)
>		{			
>			this.text = "Postback occurred.";			
>		}
>	}
>}
>
>
>
>Then I modified the HTML code to add the control:
>
>
><%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TestPostback.WebForm1"   
><%@ Register TagPrefix="cc1" Namespace="SampleControls" Assembly="TestPostback"   
><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
><HTML>
>	<HEAD>
>		<title>WebForm1</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/ie5">
>	</HEAD>
>	<body MS_POSITIONING="GridLayout">
>		<form id="Form1" method="post" runat="server">
>			<asp:ImageButton id="ImageButton1" style="Z-INDEX: 101; LEFT: 241px; POSITION: absolute; TOP: 207px" runat="server" Width="60px" Height="26px"></asp:ImageButton>
>			<cc1:SimpleControl id="SimpleControl" runat="server"> </cc1:SimpleControl>
>		</form>
>	
>	</body>
></html>
>
>
>
>The form runs OK, but the PostBackEvent still doesn't fire (even with a breakpoint set). Any other ideas?
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform