Monday, February 10, 2014

Useful Delegates Example in C# and ASP.NET

Create a UserControl named Uc1.ascx (i.e. I have created insided Folder named Controls)

Step 1: Declare a Delegate and Events for it. In my case,i have created for button click and dropdownlist

Step 2: In .aspx page reference it like this :

<%@ Register Src="~/Controls/Uc1.ascx" TagName="Users" TagPrefix="Test" %>

Full Code :
Uc1.ascx
------------
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Uc1.ascx.cs" Inherits="Controls_Uc1" %>
This is User control1
<asp:Button ID="btnButton1" runat="server" Text="Click" OnClick="btnButton1_Click" />
<asp:DropDownList ID="drp1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="drp1_OnSelectedIndexChanged">
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
<asp:ListItem>Three</asp:ListItem>
<asp:ListItem>Four</asp:ListItem>
</asp:DropDownList>
Uc1.ascx.cs
------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Controls_Uc1 : System.Web.UI.UserControl
{
    #region DECLRATIONS
    public delegate void MyCustomHandler(object sender, EventArgs e);  //This is for button click
    public event MyCustomHandler SomethingClicked;
    
    public delegate void MyCustomSelectedHandler(object sender, EventArgs e); //This is for Dropdown selected 
    public event MyCustomSelectedHandler SomethingSelected;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnButton1_Click(object sender, EventArgs e)
    {
        //tell our parent page which is listening for the event that something was clicked
        //this null check and method invoke is the equivalent of raise event in VB
        if (SomethingClicked != null)
        {
            SomethingClicked(sender, e);
        }
        //do some other work specific to this button in the user control
    }
    protected void drp1_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (SomethingSelected != null)
        {
            SomethingSelected(sender, e);
        }
    }
}


Default4.aspx



---------------------



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<%@ Register Src="~/Controls/Uc1.ascx" TagName="Users" TagPrefix="Test" %>
<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:Button ID="btn1" runat="server" Text="Load UserControl1"  />
    <br />
    <Test:Users ID="Users1" runat="server"></Test:Users>
    </div>
    </form>
</body>
</html>


Default4.aspx.cs



----------------------------



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default4 : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Users1.SomethingClicked += new Controls_Uc1.MyCustomHandler(Users1_ButtonClicked); //Button Click
        Users1.SomethingSelected += new Controls_Uc1.MyCustomSelectedHandler(Users1_DropDownSelected); //DropDownList
    }
    protected void Users1_ButtonClicked(object senders, EventArgs e)
    {
        ScriptManager.RegisterClientScriptBlock(this, GetType(), "clientkey", "<script>alert('Button1 Clicked');</script>", false);
    }
    protected void Users1_DropDownSelected(object senders, EventArgs e)
    {
        ScriptManager.RegisterClientScriptBlock(this, GetType(), "clientkey", "<script>alert('Make Parent Pages Modal Popup visible when UserControls dropdownlist posted back');</script>", false);
     
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

0 comments:

Post a Comment