Friday, July 30, 2010

How to bubble up an event from usercontrol to parent page(control) (Part 2 - Without Delegates)- Asp.net

Without Delegates
Please use OnBubbleEvent of the page and caste the source (first argument) to required control (in this case a button) and compare Id or you can use object compare

UserControl (Code and Markup)
Markup
<%@ Control Language="C#" 
            AutoEventWireup="true" 
            CodeBehind="WebUserControl.ascx.cs" 
            Inherits="ActiveTest.WebUserControl" %>
<asp:Button runat="server" ID="btnUpdate" Text="Update" />
 
Code

namespace ActiveTest
{
    public partial class WebUserControl : UserControl
    {
        public string UpdateButton         
        { 
            get { return this.btnUpdate.ID; } 
        }    
    }
 
}

Consuming Page

<%@ Page Language="C#" %>
<%@ Register Src="~/WebUserControl.ascx" TagName="wuc" TagPrefix="active" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script runat="server"> 
        protected override bool OnBubbleEvent(object source, EventArgs args)
        {
            Button btnUpdate = source as Button;
            if (btnUpdate != null && btnUpdate.ID == this.wucTest.UpdateButton)
            {
                ///
                /// Update/ or do any requred actions
                ///
            }
            return base.OnBubbleEvent(source, args);
        }
    </script>
    <script language="javascript" type="text/javascript">
    </script>
</head>
<body>    
    <form id="form1" runat="server">        
        <div>
            <active:wuc runat="server" ID="wucTest" />
        </div>
    </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...