Thursday, July 20, 2017

Delegates Events Ex-3

public partial class buttonEvent : System.Web.UI.Page
{
    
    public delegate void ButtonEventHandler(object sender, CustomEventArgs args);
    public event ButtonEventHandler ButtonClicked;

    protected void Page_Load(object sender, EventArgs e)
    {
        ButtonClicked += clsSubscriber.OnButtonClicked;

        if (!Page.IsPostBack)
        {
             ViewState["Counter"] = 0;
             Response.Write(ViewState["Counter"] ?? 0);
        }
        else
        {
           // Response.Write(ViewState["Counter"] ?? 0);
        }
    }

    public virtual void OnButtonClicked(object sender, CustomEventArgs args)
    {
        ButtonClicked?.Invoke(this, args);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
      
        ViewState["Counter"] = Convert.ToInt32(ViewState["Counter"]) + 1;
        CustomEventArgs x = new CustomEventArgs();
        x.strButtonID = this.ClientID;
        x.MyCounter = Convert.ToInt32(ViewState["Counter"]);
        x.Message = x.strButtonID + "Button Clicked.";

        OnButtonClicked(this, x);
    }
}


public class CustomEventArgs : EventArgs
{
    public string strButtonID { get; set; }
    public int MyCounter { get; set; }
    public string Message { get; set; }

}

public static class clsSubscriber
{
    public static void OnButtonClicked(object sender, CustomEventArgs args)
    {
        HttpContext.Current.Response.Write("The Counter is : " + args.MyCounter);
        HttpContext.Current.Response.Write(args.Message);
    }
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More