Click here to Skip to main content
Licence CPOL
First Posted 2 Jan 2008
Views 14,740
Downloads 34
Bookmarked 16 times

How To Create Events with WebUserControl

By | 2 Jan 2008 | Article
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

License

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

About the Author

KennyPatel

Software Developer (Senior)

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalsource file download is not working Pinmembereran_20039:32 28 Oct '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 2 Jan 2008
Article Copyright 2008 by KennyPatel
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid