You must Sign In to post a response.
  • Category: ASP.NET

    Events and Delegates in

    Hai All,

    May i know where and when to use Events and Delegates in page

    My Doubt is
    When it added more benefit Ex: It wont come for page life cycle etc......

    Please explain with any simple example

    Thanks and Regards,
  • #764161
    Hi Chellapan.

    This is the fifth time you have been asking the same question regarding delegates and events.
    many times we provided you the solutions for it.

    When we want the controls logic and forms logic to be executed at one shot, we create a custom event to fire both logics at a time.

    While working in real time you will get requirement like when predefined events are not enough to handle the logic then we create custom events on fly.
    Sample code:

    Class Sample
    public void dosomething();
    MessageBox.Show("Am from dosomething
    public delegate void delegate_name();
    code for btn_Click
    Sample obj=new Sample();// creating object for
    delegate with address of method.
    delegate_name obj2=new delegate_name
    obj2(); // call delegate object.

    Refer below link for writing custom events using delegates.

    Sridhar Thota.
    Editor: DNS Forum.

  • #764162
    Following is the simple example to create user control using delegate and even

    1. Create user control and have one button

    <asp:Button ID="BtnOK" runat="server" CommandArgument="" UseSubmitBehavior="false" Text="Ok" OnClick="BtnOK_Click" />

    2. Following are the syndex for delegate and event.

    public delegate void OnOKButtonClick(object sender, EventArgs e);
    public event OnOKButtonClick OKButton;
    protected void BtnOK_Click(object sender, EventArgs e)
    OKButton(sender, e);

    3. You can use the user control in any of your page. And you can handle the event "OKButton".

    By Nathan
    Direction is important than speed

  • #764166
    Hai Cheallapn,
    The thumb rule for event delegate is that when we are not having inbuilt events, we can create the delegate and then use the custom event to handle it.
    You can search inside the DNS and find many examples of it.
    Below are few links, I am providing, please go through them:

    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #764176
    When you want to customize the behavior of an object you can use delegate, A delegate is a reference to a method. Whereas objects can easily be sent as parameters into methods, constructor or whatever, methods are a bit more tricky.

    - Delegates are similar to C++ function pointers, but are type safe.
    - Delegates allow methods to be passed as parameters.
    - Delegates can be used to define callback methods.
    - Delegates can be chained together; for example, multiple methods can be called on a single event.

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • Sign In to post your comments