Monday, February 10, 2014

Asp.net c# Delegates

Uc1.ascx
----------
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Uc1.ascx.cs" Inherits="Controls_Uc1" %>
This is Uc1
<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);
        }
    }
}


 



Default3.aspx
---------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<%@ Register Src="~/Controls/Uc1.ascx" TagName="Users" TagPrefix="Magic" %>
<!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 Control1"  />
 
    <table>
    <tr>
    <td>
    <asp:PlaceHolder ID="phModule" runat="server"></asp:PlaceHolder>
    </td>
    </tr>
    </table>
    
    </div>
    </form>
</body>
</html>
Default3.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 Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        phModule.Controls.Clear();
        Control myUserControl = (Control)Page.LoadControl("~/Controls/Uc1.ascx");
        // Place web user control to place holder control
        if (myUserControl != null)
        {
            Button btnButton1 = (Button)myUserControl.FindControl("btnButton1");
            btnButton1.Click += new EventHandler(CustomerEmail1_RestoreModalDialog);
            DropDownList drp1 = (DropDownList)myUserControl.FindControl("drp1");
            drp1.SelectedIndexChanged += new EventHandler(drp1_SelectedIndexChanged);
        }
        phModule.Controls.Add(myUserControl);
    }
    protected void CustomerEmail1_RestoreModalDialog(object senders, EventArgs e)
    {
        ScriptManager.RegisterClientScriptBlock(this, GetType(), "clientkey", "<script>alert('Button1 Clicked');</script>", false);
    }
    protected void drp1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ScriptManager.RegisterClientScriptBlock(this, GetType(), "clientkey", "<script>alert('Make Parent Pages Modal Popup visible when UserControls dropdownlist posted back');</script>", false);
    }
}

0 comments:

Post a Comment