Click here to Skip to main content
Click here to Skip to main content

How To Create Events with WebUserControl

, 2 Jan 2008
Rate this:
Please Sign up or sign in to vote.
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
No Biography provided

Comments and Discussions

 
QuestionThanks!! PinmemberMember 109349289-Jul-14 1:45 
Generalsource file download is not working Pinmembereran_200328-Oct-09 9:32 
BugRe: source file download is not working Pinmembermohanjune19874-Oct-12 2:20 

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140718.1 | Last Updated 2 Jan 2008
Article Copyright 2008 by KennyPatel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid