Handling Custom Event generated in UserControl, inside ‘Base Page class’

It’s always a good practice to derive all the aspx pages from a ‘base page class’. Another reason, as how this helps in controlling the visibility at control-level, application-wide. In this example below, I have an aspx page derived from a base page class. My aspx page contains 2 UserControls. To keep this simple , each UserControl has just 1 submit button respectively.Now what I want is , when I click the button present on UserControl1 , — then the button present in UserControl2 should get hidden.
Also, what if I want, exact same functionality in few more aspx pages, in my application. That is, when UserControl1->Button1 is clicked , then UserControl2->Button2 gets hidden, throughout the app. A close practical situation would be , based on certain rights/permissions, certain functionality/control on every page getting on/off. Notice that it’s on control/functionality level & not just page level.
Below is how I acheived it. Whenever the UserControl1->Button1 is clicked , it raises a custom event, which is handled in the base page class.In the event argument for this event, I pass the container page’s — Contentplaceholder ID. Now in the base page class, event handler, I obtain the UserControl2->Button2 using ‘FindControl on event arguments’ & make it’s visible property as false. This example just demonstrates the concept of base page class handling custom events, application wide. Here’s the relevant code-sample.

//Default.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Folder1/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Folder1_Default" %>
<%@ Register Src="WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>
<%@ Register Src="WebUserControl2.ascx" TagName="WebUserControl2" TagPrefix="uc2" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
    <br />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br /><br />
    <uc2:WebUserControl2 ID="WebUserControl22" runat="server" />
</asp:Content>


//UserControl1

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

<asp:Button ID="btnTest" runat="server" Text="User Control1" OnClick="btnTest_Click" />

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

public partial class Folder1_WebUserControl1 : System.Web.UI.UserControl
{
ExampleEvent ee1 = null;

protected void Page_Load(object sender, EventArgs e)
{
    
}

protected void btnTest_Click(object sender, EventArgs e)
{
    ee1 = new ExampleEvent();
    ee1.onRefresh2Event(this.Parent, e);
}
}

//Base Page Class

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

/// <summary>
/// Summary description for MyBasePage
/// </summary>
public class MyBasePage : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e); //let the content page load first.
    if (!IsPostBack)
    {
        ExampleEvent.onRefresh2 += new ExampleEvent.RefreshPostBackHandler2(ExampleEvent_onRefresh2);
    }
}

void ExampleEvent_onRefresh2(object sender, EventArgs e)
{
    Button b1 = (Button)(((ContentPlaceHolder)sender).FindControl("WebUserControl22").FindControl("btnTest"));
    if (b1 != null)
    {
        b1.Visible = false;
    }
    //throw new NotImplementedException();
}
}

//ExampleEvent.cs

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

/// <summary>
/// Summary description for ExampleEvent
/// </summary>
public class ExampleEvent
{
    public delegate void RefreshPostBackHandler2(object sender, EventArgs e);
    public static event RefreshPostBackHandler2 onRefresh2;

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

    }
}

//UserControl2

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

<asp:Button ID="btnTest" runat="server" Text="User Control2" OnClick="btnTest_Click" />

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