Click here to Skip to main content
15,894,720 members
Articles / Programming Languages / C#

How To Create Events with WebUserControl

Rate me:
Please Sign up or sign in to vote.
4.12/5 (15 votes)
2 Jan 2008CPOL 41.9K   21   3
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:

C#
//
// 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

C#
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:

C#
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks!! Pin
Member 109349289-Jul-14 1:45
Member 109349289-Jul-14 1:45 
Generalsource file download is not working Pin
eran_200328-Oct-09 9:32
eran_200328-Oct-09 9:32 
BugRe: source file download is not working Pin
mohanjune19874-Oct-12 2:20
mohanjune19874-Oct-12 2:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.