Thursday, 28 February 2013

How to Create Custom events in Asp.Net

In this example I will show you how to create double click event for a button class .
  • The first thing that you need to do is create a class library and then add a reference of System.Web namespace. Then you create a class and inherit from the Button class
    public class MyCustomButton : Button
  • Now  create a delegate
    public delegate void MyDelegate(object o, EventArgs e);
  • Then create an event
    public event MyDelegate DblClick;

Do you know why did we  create a delegate?
The main reason for creating a delegate is that a delegate is a function pointer, and you can call the function in your aspx file using that delegate.

  • Now you need to override the RenderBeginTag function and if you have created a double click event then you have add attributes to the button control

public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
                      {
 if (DblClick != null)
           {
                                    writer.AddAttribute("onclick", "javascript:return false;");
        writer.AddAttribute("ondblclick",       "javascript:__doPostBack('" + this.UniqueID + "','DblClick')");
                                 }
                                                base.RenderBeginTag(writer);
                       }


  • Now the last thing you need to do Is to decide when to call the event. Here in example I decided that I had to override the RaisePostBackEvent .


protected override void RaisePostBackEvent(string eventArgument)
 {
            if (eventArgument == "DblClick")
            {
                DblClick(this, new EventArgs());
            }
            base.RaisePostBackEvent(eventArgument);
 }


  • Create a web application and added the custom button dll to my web application and added the following line in my aspx

 <%@ Register assembly="MyButton" namespace="MyButton" tagprefix="cc1" %>


  •  Now create a button using the following and also the event for double click

<cc1:MyCustomButton ID="MyCustomButton1" runat="server"
                                      ondblclick="MyCustomButton1_DblClick" />


The following in the code file

protected void MyCustomButton1_DblClick(object o, EventArgs e)
{

}

Which would excute on the happening of that event

The following is the HTML that is geneated

<input name="MyCustomButton1" id="MyCustomButton1" onclick="javascript:return false;" ondblclick="javascript:__doPostBack('MyCustomButton1','DblClick')" type="submit" value=""/>

Well you can debugg  the application and find more on how does it work
There Is another way where you can eliminate the code of writing the delegate
public event EventHandler DblClick;

EventHandler is a delegate which has two parameters you can eleminate the following line of code 
public delegate void MyDelegate(object o, EventArgs e);


So here is the code of my class file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
namespace MyButton
{
    public class MyCustomButton : Button
    {
        public event EventHandler DblClick;
        public delegate void MyDelegate(object o, EventArgs e);
        protected override void RaisePostBackEvent(string eventArgument)
        {
            if (eventArgument == "DblClick")
            {
                DblClick(this, new EventArgs());
            }
            base.RaisePostBackEvent(eventArgument);
        }
        public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
        {
            if (DblClick != null)
            {
                writer.AddAttribute("onclick", "javascript:return false;");
                writer.AddAttribute("ondblclick", "javascript:__doPostBack('" + this.UniqueID + "','DblClick')");
            }
            base.RenderBeginTag(writer);
         
        }
    }
}
Enjoy Coding

About Author:
Steven Pinto is technology geek and loves to write on technology. He works in systems Plus and actively contributes to technology. To more of interesting topics written by Steven, follow http://mad4teck.blogspot.in/

No comments:

Post a Comment