Easy example on working with "UserControl and Event/Delegate"

The example below contains 3 UserControls and it highlights the way these 3 user-controls sends information across, using custom event. All the event and their respective event-handlers are defined in separate class files.

UserControl1 contains a Dropdownlist.
UserControl2 contains a textBox & a button.
UserControl3 just contains 2 label controls.
Now when the dropdown selection is changed, the other 2 UserControls are notified.
Similarly when user enters some text in the textBox & hits the button, UserControl3 is notified.

uc

//Events

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for UC1ddlEvent
/// </summary>
public class UC1ddlEvent
{
    public delegate void OnMyEvent1(object sender, EventArgs e);
    public static event OnMyEvent1 MyEvent1;

    public virtual void onRefresh2Event(object sender, EventArgs e)
    {
        if (MyEvent1 != null)
        {
            MyEvent1(sender, e);
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for UC2BtnClickEvent
/// </summary>
public class UC2BtnClickEvent
{
    public delegate void OnMyEvent3(object sender, EventArgs e, string strTextBox);
    public static event OnMyEvent3 MyEvent3;

    public virtual void onRefresh2Event3(object sender, EventArgs e, string strTextBox)
    {
        if (MyEvent3 != null)
        {
            MyEvent3(sender,e,strTextBox);
        }

    }
}

//userControls

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs" Inherits="UserControls_WebUserControl1" %>

<br />
<b>User Control 1:</b>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem Selected="True" Value="" Text="Please select"></asp:ListItem>
    <asp:ListItem  Value="1" Text="First"></asp:ListItem>
    <asp:ListItem  Value="2" Text="Second"></asp:ListItem>
    <asp:ListItem  Value="3" Text="Third"></asp:ListItem>
    <asp:ListItem  Value="4" Text="Fourth"></asp:ListItem>
</asp:DropDownList>
&nbsp;<span style="background-color:Lime;">(Notify UserControl2 and UserControl3 when selection changes.)</span>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_WebUserControl1 : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        UC1ddlEvent ddlEvnt1 = new UC1ddlEvent();
        ddlEvnt1.onRefresh2Event(sender, e);
    }
}
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.cs" Inherits="UserControls_WebUserControl2" %>

-----------------------------------------------------------------------------------------------------------
<br />
<b>User Control 2:</b>
<br />
Obtained Data From UserControl1= &nbsp;&nbsp; <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br /><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
    Text="Send Data to UserControl3" />

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_WebUserControl2 : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Interested in dropdownlist change event.
        UC1ddlEvent.MyEvent1 += new UC1ddlEvent.OnMyEvent1(UC1ddlEvent_MyEvent1);
    }

    void UC1ddlEvent_MyEvent1(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        DropDownList ddl = (DropDownList)sender;
        Label1.Text = "UC1 ddl selected Text is " + ddl.SelectedItem.Text;
    }

    
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Raise the event that button has been clicked & send the textbox contents as one of the argument
        UC2BtnClickEvent bEvent = new UC2BtnClickEvent();
        bEvent.onRefresh2Event3(sender, e, TextBox1.Text);
    }

    
}
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl3.ascx.cs" Inherits="UserControls_WebUserControl3" %>


<br />
-----------------------------------------------------------------------------------------------------------
<br />
<b>User Control 3:</b>
<br />
Obtained Data From UserControl1=&nbsp;&nbsp;<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br /><br />
Obtained Data From UserControl2=&nbsp;&nbsp;</asp:Label><asp:Label ID="Label2" runat="server" Text=""></asp:Label>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_WebUserControl3 : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Interested in dropdownlist change event.
        UC1ddlEvent.MyEvent1 += new UC1ddlEvent.OnMyEvent1(UC1ddlEvent_MyEvent1);

        //Interested in btn click event.
        UC2BtnClickEvent.MyEvent3 += new UC2BtnClickEvent.OnMyEvent3(UC2BtnClickEvent_MyEvent3);
    }

    void UC1ddlEvent_MyEvent1(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        DropDownList ddl = (DropDownList)sender;
        Label1.Text = "UC1 ddl selected Text is! " + ddl.SelectedItem.Text;
    }

    
    void UC2BtnClickEvent_MyEvent3(object sender, EventArgs e, string strTextBox)
    {
        //throw new NotImplementedException();
        //Label1.Text = "UC1 ddl was selected! " + ddl.SelectedItem.Text;
        Label2.Text = "UC2 button clicked. TextBox value is= " + strTextBox;
    }
}

//Container Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>


<!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>
      
    </div>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    <br /><br />
    <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
    <br /><br />
    <asp:PlaceHolder ID="PlaceHolder3" runat="server"></asp:PlaceHolder>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : MyBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UserControl uc1 = (UserControl)this.LoadControl("~/UserControls/WebUserControl1.ascx");
        PlaceHolder1.Controls.Add(uc1);

        UserControl uc2 = (UserControl)this.LoadControl("~/UserControls/WebUserControl2.ascx");
        PlaceHolder2.Controls.Add(uc2);

        UserControl uc3 = (UserControl)this.LoadControl("~/UserControls/WebUserControl3.ascx");
        PlaceHolder3.Controls.Add(uc3);
    }
}

Hope this helps. Thanks for reading

Advertisements
This entry was posted in General ASP.Net C#. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s