65.9K
CodeProject is changing. Read more.
Home

How To Create Events with WebUserControl

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.12/5 (14 votes)

Jan 2, 2008

CPOL
viewsIcon

42103

downloadIcon

34

Create Events/Properties with WebUserControl in C#

Introduction

This article will explain to you about custom events for WebUserControl. The method which I am trying to explain to you is simply known as "Event Bubbling". You might have heard of this. I am not a good writer, I will try to explain to you. Thanks again. In this example, I have explained the Navigation Control [like: First-Prev-Next-Last] which we are using frequently in our web project. I made a simple navigation User Control and created events which you can fire when you are using this User Control. So let's start:

//
// Sample Code
//

Step 1

[Let's create a WebUserControl first]
File Name: WebUserControl.ascx in zip file.

Code Behind for above ascx should be:
File Name: WebUserControl.ascx.cs

public partial class WebUserControl : System.Web.UI.UserControl
{
    public delegate void EventHandler(Object obj, EventArgs e);
    public event EventHandler BubbleClickFirst;
    public event EventHandler BubbleClickPrev;
    public event EventHandler BubbleClickNext;
    public event EventHandler BubbleClickLast;

    protected void OnBubbleClick_First()
    {
        if (BubbleClickFirst != null)
        {
            BubbleClickFirst(this, new EventArgs());
        }
    }
    protected void OnBubbleClick_Prev()
    {
        if (BubbleClickPrev != null)
        {
            BubbleClickPrev(this, new EventArgs());
        }
    }
    protected void OnBubbleClick_Next()
    {
        if (BubbleClickNext != null)
        {
            BubbleClickNext(this, new EventArgs());
        }
    }
    protected void OnBubbleClick_Last()
    {
        if (BubbleClickLast != null)
        {
            BubbleClickLast(this, new EventArgs());
        }
    }

    protected void btFirst_Click(object sender, EventArgs e)
    {
        OnBubbleClick_First();
    }
    protected void btPrev_Click(object sender, EventArgs e)
    {
        OnBubbleClick_Prev();
    }
    protected void btNext_Click(object sender, EventArgs e)
    {
        OnBubbleClick_Next();
    }
    protected void btLast_Click(object sender, EventArgs e)
    {
        OnBubbleClick_Last();
    }
}

Step 2

[Let's use this WebUserControl in our ASPX page]
File Name: Default.aspx attached in ZIP file.

The code behind file for Default.aspx.cs:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Init(object sender, EventArgs e)
    {
        //WebUserControl1.BubbleClick +=
        //	new WebUserControl.EventHandler(WebUserControl1_BubbleClick);

        WebUserControl1.BubbleClickFirst += 
		new WebUserControl.EventHandler(WebUserControl1_BubbleClickFirst);
        WebUserControl1.BubbleClickPrev += 
		new WebUserControl.EventHandler(WebUserControl1_BubbleClickPrev);
        WebUserControl1.BubbleClickNext += 
		new WebUserControl.EventHandler(WebUserControl1_BubbleClickNext);
        WebUserControl1.BubbleClickLast += 
		new WebUserControl.EventHandler(WebUserControl1_BubbleClickLast);
    }
    private void WebUserControl1_BubbleClickFirst(object sender, EventArgs e)
    {
        Label1.Text = "First Button Press....";
    }
    private void WebUserControl1_BubbleClickPrev(object sender, EventArgs e)
    {
        Label1.Text = "Previous Button Pressed....";
    }
    private void WebUserControl1_BubbleClickNext(object sender, EventArgs e)
    {
        Label1.Text = "Next Button Pressed....";
    }
    private void WebUserControl1_BubbleClickLast(object sender, EventArgs e)
    {
        Label1.Text = "Last Button Pressed....";
    }
}		

Points of Interest

The important things here that I used are delegate events. I hope you will find something productive in my article. Please vote for this article. Thanks for spending your time reading. Again I say that I am not a good writer so.... thanks JSKS. Thank you.

History

  • 2nd January, 2008: Initial post