Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 3092 |  Welcome, Guest!   Register  Login
Home > Articles > C# > Simple custom Event & Delegates demonstration.

Simple custom Event & Delegates demonstration.

Article posted by Puneet20884 on 1/11/2010 | Views: 2856 | Category: C# | Level: Beginner red flag


Hi,
The article will help you to fire your events (custom events) from the control's events of a Web User Control from the Web Page.

Not going into Much of the theories,I am mentioning the code below to be used for User Control and Web Page.

In my case here i want to fire a Button's Click event from my web page.
You can in mean way can achieve any event like Drop down's SelectedIndexChanged, Data Grid's Paging , Sorting, Command event or any one of any control by just changing the parameter types of the arguments in the Delegate that you are creating the object of.

Here the Code goes of User Control:

public partial class WebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public delegate void MyDel(object sender, EventArgs e);
    //The argument's types above will be as of the event you want to call your manual event in.

    public event MyDel evn; // Event of the above delegate
    protected void Button1_Click(object sender, EventArgs e)
    {
        evn(sender, e);
        //Call this manual event inside the event you want to handle from the Web Page.
    }
}

Here the Code goes of Web Page:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        WebUserControl1.evn += new WebUserControl.MyDel(WebUserControl1_evn);
        // Define the function pointer in which you will create the custom rules or business rule as per requirement
        //So when ever the event of the button in the user control will fire, this event handler will be called
    }

    void WebUserControl1_evn(object sender, EventArgs e)
    {
        if(Request["a"] == null) //Condition 1
        {
            Response.Write("asdfasdf");
        }
        else
        {
            Response.Write("asdfasdfadfasdfasdf");
        }
    }
}

I wish this will give you a good idea and will solve the problems of many people of controling the events of Web User Control's Controls from the page.

Also please try for other events of other controls and let me know your feedback.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Latest Articles from Puneet20884
Experience:0 year(s)
Home page:http://www.sumitgarg.net/post/2010/04/07/Image-Resigning-in-ASP-Dot-with-C-using-httphandler.aspx
Member since:Tuesday, December 08, 2009
Level:Bronze
Status: [Member]
Biography:Working as Tech Analyst for Infosys Tech - Pune (India)
 Responses
Posted by: Abhi2434 | Posted on: 15 Jan 2010 03:37:29 AM

It is almost the same one that I posted in my blog few days back:

http://dotnetricks.blogspot.com/2010/01/how-to-expose-events-from-usercontrol.html

Cheers.

>> Write Response - Respond to this post and get points
Related Posts

This article covers the detailed feature an interface.

From this Article you can know which is the best way in performance to concatenate a string.

This article explains to us , how to connect with MS Access 2007 database from C# Windows application.

We will see The C# threading model.

This article deals with executing SSIS package stored in a SQL Server database using C# code.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:36:54 AM