Click here to Skip to main content
12,827,276 members (47,311 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


21 bookmarked
Posted 2 Jan 2008

How To Create Events with WebUserControl

, 2 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Create Events/Properties with WebUserControl in C#


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)
    protected void btPrev_Click(object sender, EventArgs e)
    protected void btNext_Click(object sender, EventArgs e)
    protected void btLast_Click(object sender, EventArgs e)

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.


  • 2nd January, 2008: Initial post


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


About the Author

Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionThanks!! Pin
Member 109349289-Jul-14 2:45
memberMember 109349289-Jul-14 2:45 
Generalsource file download is not working Pin
eran_200328-Oct-09 10:32
membereran_200328-Oct-09 10:32 
BugRe: source file download is not working Pin
mohanjune19874-Oct-12 3:20
membermohanjune19874-Oct-12 3: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170326.1 | Last Updated 2 Jan 2008
Article Copyright 2008 by KennyPatel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid