Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,
In all the blogs i read how to raise custom events, by calling <EventName>(<senderObject>,<eventArguments>) in a child control's postback event like, Button_Click events and etc..., in user control. But I want raise this event without calling in any child control's postback event.
Please guide me. Just I am thinking that, if we call our custom event <EventName>(<senderObject>,<eventArguments>) in any child control's postback, no use of it. Instead of this, directly we can write our code in Child controls postback event. Then what is the use of custom events?
I read the sameples like, developed a user control

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MySamples
{
    public delegate void customDeligate(object sender, CustomArgs args);
    public class MyClass
    {
        public event customDeligate customEvent;
        protected virtual void OnCustomEvent(CustomArgs e)
        {
            if (customEvent != null)
                customEvent(this, e);
        }
        protected void OnCustomEvent(object sender, CustomArgs e)
        {
            if (customEvent != null)
                customEvent(sender, e);
        }
    }
    public class CustomArgs : EventArgs
    {
        string _msg;

        public string Message
        {
            get { return _msg; }
            set { _msg = value; }
        }
        public CustomArgs(string message)
            : base()
        {
            this._msg = message;
        }
        public CustomArgs()
            : base()
        {
        }
    }
 public partial class Wall_Papers : System.Web.UI.UserControl
 {
    
        protected void Page_Load(object sender, EventArgs e)
        {
            MyClass objClass = new MyClass();
            objClass.customEvent += new customDeligate(objClass_customEvent);
            
        }

        static void objClass_customEvent(object sender, CustomArgs args)
        {
            Response.Write("Hi Welcome to Custome Events");
        }
    }
}


How can I raise my event, without using any postback child controls like button,dropdown etc., I want create my own control which is working as Regular controls.

Thanks & Regards
Chaitanya
Posted
Updated 2-Mar-12 2:12am
v2

Just call the onCustomEvent method ...

Also, in C# you should start method, event, public property and delegate names with a capital letter (CustomEvent, CustomDelegate etc).
 
Share this answer
 
You can invoke the event:
C#
public class MyClass {

//...

    private void InvokeCustomEvent(string myMessage) {
        if (customEvent != null)
            customEvent.Invoke(this, new CustomArgs(myMessage));
    }
    private void InvokeCustomEvent() {
        if (customEvent != null)
            customEvent.Invoke(this, new CustomArgs());
    }

} //class MyClass


Also, change access modifiers of those two constructors of CustomArgs to internal, because there is no situation where you can call them outside of the assembly. This is because, even by syntax, you can only invoke any event in the declaring class, not even in derived classes.

I even demonstrated that the methods InvokeCustomEvent should be used only in this class, so I've shown the private access modifiers.

Please see my recent answer for more detail:
Since we have multicast delegates, why do we need events?[^]

The users of the class instance only have access to the event object to use it to add one or more event handlers via "+=" (or remove via "-=" which is relatively rarely used). The invocation of the even always calls the whole list of event handles from the invocation list of the event.

—SA
 
Share this answer
 
you shuold modify your code here
C#
public class MyClass
    {
        //public event customDeligate customEvent;
        public event EventHandler<CustomArgs> customEvent;


    }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900