Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3300546
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:40:20+00:00 2026-05-17T20:40:20+00:00

In my last post I have asked feedback of my code.Today I have also

  • 0

In my last post I have asked feedback of my code.Today I have also crated a simple dummy project.If you think that now I have basic knowledge of delegates and events, then please tell me some difficult task for delegates and events. Below is my code-

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DelegatesAndEvents.aspx.cs" Inherits="WebProjects.DelegatesAndEvents" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btn1" runat="server" onclick="btn1_Click" Text="ONE" /><br />
        <asp:Button ID="btn2" runat="server" onclick="btn2_Click" Text="TWO" /><br />
        <asp:Button ID="btn3" runat="server" onclick="btn3_Click" Text="THREE" /><br />

        <asp:Label ID="lblText" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>


using System;

namespace WebProjects
{
    public partial class DelegatesAndEvents : System.Web.UI.Page
    {
        public delegate void MyDelegate(string val);

        public event MyDelegate myEvent;
        protected void Page_Load(object sender, EventArgs e)
        {
            myEvent += SetLabelValue;
        }

        public void SetLabelValue(string val)
        {
            lblText.Text = val;
        }

        protected void btn1_Click(object sender, EventArgs e)
        {
            if(myEvent!=null)
            {
                string value = "button one is clicked.";
                myEvent(value);
            }
        }

        protected void btn2_Click(object sender, EventArgs e)
        {
            if (myEvent != null)
            {
                string value = "button two is clicked.";
                myEvent(value);
            }
        }

        protected void btn3_Click(object sender, EventArgs e)
        {
            if (myEvent != null)
            {
                string value = "button three is clicked.";
                myEvent(value);
            }
        }
    }
}

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:40:21+00:00Added an answer on May 17, 2026 at 8:40 pm

    My first advice is that don’t write these self-study programs under an asp.net project, because you must understand the life-cycle of asp.net pages first. A console application is easier to write and test.

    And in your question, the code seems to be correct. It will be better if you give significant names to the variables, which can also help you understand delegates and events better.

    Your code is improvable, by integrating 3 click events into one. Actually in the code, myEvent is used like an ordinary delegate, no necessary to be an event. This answer may helpful to understand differences between delegate/event better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay this will be my last post here for today, have asked you guys
I have a question concerning my last post But I think at first I
Ref. to my last post and sellibitze's comment to that post on passing array
I have a simple (actually simplified :) ) scenario that is possibly the cause
I have this code for creating a topic and post in a forum application
I have made a simple Rails application that allows people to comment on posts.
This is similar to my last post but with a different purpose. I have
I have asked this question before and followed the feedback as best as I
Ok, so I failed to post my code properly last time ... I am
I have some posts that look like this in HTML: <article class=post> <h3>Lorem ipsum

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.