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:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...