Thursday, August 12, 2010

How to override a functionality of user control in consuming page using events and delegates

UserControl
Snippet
namespace ActiveTest
{
    public partial class WebUserControl : UserControl
    {
        public delegate void SayHelloHandler(Label label);
        public event SayHelloHandler SayHello;
        Label label = new Label();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);            
            this.Controls.Add(label);
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.SayHello != nullthis.SayHello(label);
            else label.Text = "<h2>Hello from base class</h2>";
        }
    }
}
Page
Snippet
<%@ Register Src="~/WebUserControl.ascx" TagName="WebUserControl" TagPrefix="asp" %>
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script runat="server">
        protected void SayHello(Label label)
        {
            label.Text = "<h1>Hello from consuming page</h1>";
        }
    </script>
</head>
<body>
    <form id="form2" runat="server">    
        <asp:WebUserControl ID="WebUserControl1" runat="server" />
        <asp:WebUserControl ID="WebUserControl2" OnSayHello="SayHello" runat="server" />
    </form>
</body>
</html>

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...