An simple AjaxButton using Ajax.NET Professional

Michael Schwarz on Friday, November 4, 2005

I have created an simple AjaxButton that will only call a method on the server-side code. There are no values sent to the server or back to the client. A new AjaxButton could serialize the form and send the data to the AjaxMethod.

Here is the code I used for a simple AjaxButton:

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

namespace AJAXDemo.App_Code.Examples.Special { [DefaultProperty("Text"), ToolboxData("<{0}:AjaxButton runat=server></{0}:AjaxButton>")] public class AjaxButton : System.Web.UI.HtmlControls.HtmlButton { public delegate void OnClickHandler();

public OnClickHandler OnClick = null;

private string nameOfMethod = "";

private void Page_Load(object sender, EventArgs e) { if(OnClick != null) { if(OnClick.Method.GetCustomAttributes(typeof(AjaxPro.AjaxMethodAttribute), true).Length > 0) AjaxPro.Utility.RegisterTypeForAjax(OnClick.Target.GetType(), this.Page);

nameOfMethod = AjaxPro.Utility.GetClientMethod(OnClick.Method); } }

protected override void CreateChildControls() { base.CreateChildControls ();

if(nameOfMethod != null && nameOfMethod.IndexOf(",") > 0) { this.Attributes.Add("onclick", "if(this.callback)" + nameOfMethod.Replace(",", ".") + "(this.callback.bind(this));" + " else " + nameOfMethod.Replace(",", ".") + "();"); } }

protected override void OnInit(EventArgs e) { this.Load += new System.EventHandler(this.Page_Load); base.OnInit (e); } } }

I will put several simple controls to the next version of Ajax.NET Professional. Please visit this blog or my website at http://www.schwarz-interactive.de [1] during the weekend for updates.