Access Events via Methods or Handlers


Today, we will dig on simple well known concepts called Events. The simple definition is message sent by some particular object to intimate that there is some action will be generated.

  1. Publisher:

    The publisher class is that which does some logic and sends or fires the events.
     
  2. Subscribers:

    The Subscriber class is that which handles the sent events where they are subscribed for specific action.

    Question Arises: Please Explain accordingly as per the situation of this article. ??? What does Publisher Person do and what does Subscriber Person do????
  1. Publisher Person Do:

    • Publisher replies, I will create and specify some delegate initially.
    • I will declare and specify some event based on the delegate which I have created previously.
    • Then I would initiate and expect some action to be performed after I eventually fire the event.
     
  2. Subscriber Person Do:

    • Subscriber replies, I will look and received the events for which I am subscribed for.
    • I will be then firing off some handlers to notify occurrence of some action towards client level.
    • Eventually, it's up to my owner whether he will keep me in subscribed mode or unsubscribed mode.

Question Arises: Where Can We Actually Use This Stuff???

  • You would use Events when you want to respond with many subscribers.
  • You would use Events when you communicate to with specific method to expect some action need to be performed.
  • You would use Events when you want to synchronize threads.
  • You would use Events when client who is accessing the application expects some operation to be performed when controls on page are fired.
  • You would use when you expect the delegate to respond application level basis operations.
  • You would use when you want to wrap your code with some asynchronous operation.
  • I think we are all now good to go and Implement Simple Event accessing C# 4.0 Ver Features.
  • I would explain you in simple terms rather than taking multiple classes and making you simple concept to be complex to grasp.

Pre Requirements:

  1. Visual Studio 2010, ASP.NET C# 4.0, ADO.NET and CSS
  2. SQL Server 2008.

I have created simple webform.aspx with simple grid on it. To enhance look and feel of App I have just created very basic CSS and added the class to Grid.

So The Complete Code of WebForm1.aspx looks like this:

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

<!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 id="Head1" runat="server">
<title></title>
<
style type="text/css">
.grid
{
font-family: Cambria;
font-size: medium;
color: #006699;
background-color: #C0C0C0;
border: medium solid #008080
}
</style>
</
head>
<
body>
<
form id="form1" runat="server">
<div><center>
<
asp:GridView ID="GridView1" runat="server" BackColor="White" CssClass="grid"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
<
br />
<br />

</center>
</
div>
</
form>
</
body>
</
html>

Now, it's time for us to go and work on Code-Behind File:

Let's Declare Simple Delegates:

public delegate void VijayDelegate();
public delegate void SampleDelegate(object sender, EventArgs e);


And Simultaneously Events for this:

public event VijayDelegate vijay;
 
public event SampleDelegate sample;

We will now create simple method accessing database with ver 4.0 Features. Where the code looks like this:


       public void Bind()
        {
            dynamic p = new ExpandoObject();
            p.Title = "Simple Grid View using Events";
            p.Data = new Action(() =>
            {
                SqlConnection con = new SqlConnection(@"Data Source=VIJAY-PC\SQLEXPRESS;Initial Catalog=Candidate;Integrated Security=True");
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("Select Id, FirstName, LastName, Age from Student ", con);
                da.Fill(ds, "s");
                GridView1.DataSource = ds;
                GridView1.DataBind();

            });
            p.Data();

        }


We will now create Simple handler to pass on some data. Where the code looks like this:

        protected void Letter_Changed(object sender, EventArgs e)
        {
            Response.Write("<center><b><i><h1>Hey I am Letter Changed <h1></center></b></i>");
        }


The Complete Source Code of WebForm1.aspx.cs File looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Dynamic;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication2
{
    public delegate void VijayDelegate();
    public delegate void SampleDelegate(object sender, EventArgs e);

    public partial class WebForm1 : System.Web.UI.Page
    {
        public event VijayDelegate vijay;

        public event SampleDelegate sample;
        protected void Page_Load(object sender, EventArgs e)
        {
            vijay += new VijayDelegate(Bind);
            vijay.Invoke();

            sample += new SampleDelegate(Letter_Changed);
            sample.Invoke(sender, e);

        }
        public void Bind()
        {
            dynamic p = new ExpandoObject();
            p.Title = "Simple Grid View using Events";
            p.Data = new Action(() =>
            {
                SqlConnection con = new SqlConnection(@"Data Source=VIJAY-PC\SQLEXPRESS;Initial Catalog=Candidate;Integrated Security=True");
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("Select Id, FirstName, LastName, Age from Student ", con);
                da.Fill(ds, "s");
                GridView1.DataSource = ds;
                GridView1.DataBind();

            });
            p.Data();

        }
        protected void Letter_Changed(object sender, EventArgs e)
        {
            Response.Write("<center><b><i><h1>Hey I am Letter Changed <h1></center></b></i>");
        }

    }
}


The Output of this Program looks like this:

events.jpg

I hope this article is useful for you...


MVC Corporation
MVC Corporation is consulting and IT services based company.