Bubble Event from UserControl

Rate It (1)

Last post 03-20-2008 2:44 PM by JazzMan6180. 7 replies.

Sort Posts:

  • Bubble Event from UserControl

    07-25-2007, 4:54 PM
    • Loading...
    • officialboss
    • Joined on 09-13-2006, 8:15 PM
    • California
    • Posts 138

    When I click on the Command button in a GridView in a UserControl the event is bubbled up to the parent page, which is a child of a MasterPage. I am getting this message on the screen: "Still working on previous request.". Anyone have any idea why? Thanks.

    User Control

    1        public delegate void EditClientDelegate(string num);
    2        public event EditClientDelegate EditClientEvent;
    3    	
    4    protected void gvGrid1_RowCommand(object sender, GridViewCommandEventArgs e)
    5        {
    6    	// Raise the event
    7            if (EditClientEvent != null)
    8            {
    9                EditClientEvent("I am raised");
    10           }
    11   	}
    

     Parent Page

    1    <script type="text/javascript" language="javascript">
    2    function ActiveTabChanged(sender, e) 
    3    {    
    4        __doPostBack('<%= tcDefault.ClientID %>', sender.get_activeTab().get_headerText());
    5    }
    6    </script>
    7    <asp:UpdatePanel ID="upMain" runat="server" UpdateMode="Conditional">
    8    <Triggers> 
    9        <asp:AsyncPostBackTrigger ControlID="tcDefault" EventName="ActiveTabChanged" />
    10   </Triggers>
    11   <ContentTemplate>
    12   <!-- BEGIN TABBED CONTROL -->
    13       <ajaxToolkit:TabContainer ID="tcDefault" runat="server" OnClientActiveTabChanged="ActiveTabChanged" 
    14       OnActiveTabChanged="ActiveTabChangedServer">
    15           <ajaxToolkit:TabPanel ID="tab1" runat="server" HeaderText="Tab 1" EnableViewState="true">
    16               <ContentTemplate>
    17               </ContentTemplate>
    18           </ajaxToolkit:TabPanel>
    19           <ajaxToolkit:TabPanel ID="tab2" runat="server" HeaderText="Tab 2" EnableViewState="true">
    20               <ContentTemplate>
    21                   <uc2:Client ID="Client1" runat="server" />
    22               </ContentTemplate>
    23           </ajaxToolkit:TabPanel>
    24           <ajaxToolkit:TabPanel ID="tab3" runat="server" HeaderText="iPortal" />
    25       </ajaxToolkit:TabContainer>
    26   </ContentTemplate>
    27   </asp:UpdatePanel>
    
     
     Parent PageBehind
    1        protected override void OnInit(EventArgs e)
    2        {
    3    		this.Load += new EventHandler(this.Page_Load);
    4    		Client1.EditClientEvent += new Forms_Client.EditClientDelegate(Client1_EditClientEvent);
    5    	}
    6    	
    7    	void Client1_EditClientEvent(string num)
    8        {
    9    	Response.Write("I have been bubbled by: " + num);
    10   	}
    
     
    Please be sure to click "Mark as Answer" on the post that helped you.
  • Re: Bubble Event from UserControl

    07-25-2007, 11:47 PM

    Have you solved your problem?

    Got any solution, if yes post it on forum, I m interested to see the solution.

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: Bubble Event from UserControl

    07-26-2007, 1:12 PM
    • Loading...
    • officialboss
    • Joined on 09-13-2006, 8:15 PM
    • California
    • Posts 138

    Haven't found the solution yet, still working on it. Hopefully someone here will shed some light on it.

    Please be sure to click "Mark as Answer" on the post that helped you.
  • Re: Bubble Event from UserControl

    07-26-2007, 11:42 PM

    Yss sure, hopefully someone will help.

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: Bubble Event from UserControl

    07-30-2007, 6:04 AM
    Answer

    Hi

    There are many ways to do that. The best way is to bubble up the event of the control to a custom event of the user control, then, register the custom event of the user control as the trigger of the Update Panel.In order to do this, following these steps: 
    1.    bubble up the event of the controlto a custom event of the user control; 
    In the following sample, we bubbled up the Command events of the LinkButton controls to the TestCustomEvent custom event of the TestUc user control: 
    <%@ Control Language="C#" ClassName="TestUc" %>
    <script runat="server">
        public delegate void ClickEventHandler(object sender, CommandEventArgs e);
        public event ClickEventHandler TestCustomEvent;
        protected void lb_Command(object sender, CommandEventArgs e)
        {
            if (TestCustomEvent != null) TestCustomEvent(this, e);
        }
    </script>

    <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="LinkButton1" OnCommand="lb_Command">LinkButton1</asp:LinkButton>
    <asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="LinkButton2" OnCommand="lb_Command">LinkButton2</asp:LinkButton>

    2.    register the custom event of the user control as the trigger of the Update Panel just like register the click event of a button; 
    In the following sample, we registered the TestCustomEvent custom event of the TestUc user control as an AsyncPostBackTrigger of the Update Panel:
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="TestUc1" EventName="TestCustomEvent" />
                    </Triggers>

    3.    handle the custom event of the user control:

        protected void TestUc1_TestCustomEvent(object sender, CommandEventArgs e)
        {
            Label1.Text = DateTime.Now.ToString();
        }

    Here is the full sample code: 

    <%@ Page Language="C#" %>
    <%@ Register Src="TestUc.ascx" TagName="TestUc" TagPrefix="uc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
        protected void TestUc1_TestCustomEvent(object sender, CommandEventArgs e)
        {
            Label1.Text = DateTime.Now.ToString();
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager>
                <uc1:TestUc id="TestUc1" runat="server" OnTestCustomEvent="TestUc1_TestCustomEvent">
                </uc1:TestUc>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="TestUc1" EventName="TestCustomEvent" />
                    </Triggers>
                </asp:UpdatePanel>
            </div>
        </form>
    </body>
    </html>

    <%@ Control Language="C#" ClassName="TestUc" %>
    <script runat="server">
        public delegate void ClickEventHandler(object sender, CommandEventArgs e);
        public event ClickEventHandler TestCustomEvent;
        protected void lb_Command(object sender, CommandEventArgs e)
        {
            if (TestCustomEvent != null) TestCustomEvent(this, e);
        }
    </script>

    <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="LinkButton1" OnCommand="lb_Command">LinkButton1</asp:LinkButton>
    <asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="LinkButton2" OnCommand="lb_Command">LinkButton2</asp:LinkButton>

    Related threads:

    Let me know if you need more info. 

    Best Regards

    Sincerely,
    Jin-Yu Yin
    Microsoft Online Community Support
  • Re: Bubble Event from UserControl

    07-30-2007, 11:36 PM

    Thanks Jin-Yu Yin Smile

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: Bubble Event from UserControl

    12-13-2007, 6:08 AM
    • Loading...
    • Emrikol
    • Joined on 12-07-2007, 10:25 AM
    • Posts 4

    Thank you for this information.

     

     

    Best regards,

    Emrikol

  • Re: Bubble Event from UserControl

    03-20-2008, 2:44 PM
    • Loading...
    • JazzMan6180
    • Joined on 03-20-2008, 2:34 PM
    • Posts 1

    I have a solution that works well for me. I had a problem where I was getting a fully Postback when I wanted to affect another updatepanel. Bubbling as many events as my nested user controls have is unrealistic.  Instead I register the postback handler in the code-behind.  Assuming your main page has the script manager named "ScriptManager1" all you need to do is add an "AjaxPostback" attribute to the controls you wish to make as an ajax postback.  This approach works well even for controls that are added dynamically.

    // ASP.NET code
    <asp:Repeater ID="CategoryItems" OnItemCommand="GenreSelected" runat="server">
    <HeaderTemplate>
    <ul class="categories">
    </HeaderTemplate>
    <ItemTemplate>
    <li><asp:LinkButton CommandName="GenreSelected" AjaxPostback="True" CommandArgument='<%#Eval("GenreID")%>' runat="server">
    <%#Eval("GenreName")%>
    </asp:LinkButton></li>
    </ItemTemplate>
    <FooterTemplate>
    </ul>
    </FooterTemplate>
    </
    asp:Repeater>
    // C# code in your main page.
    protected override void OnPreRender( EventArgs e ) {
    RegisterControlsRecursive( Drawers1 );
    base.OnPreRender( e );
    }
    private void RegisterControlsRecursive( Control ctrl ) {
    //ScriptManager1.RegisterAsyncPostBackControl( ctrl );
    foreach ( Control c in ctrl.Controls ) {
    if ( c is LinkButton ) {
    LinkButton lb = (LinkButton)c;
    if ( !string.IsNullOrEmpty(lb.Attributes["AjaxPostback"]) )
    ScriptManager1.RegisterAsyncPostBackControl( lb );
    }

     

    if ( c is Button ) {
    Button lb = (Button)c;
    if ( !string.IsNullOrEmpty( lb.Attributes["AjaxPostback"] ) )
    ScriptManager1.RegisterAsyncPostBackControl( lb );
    }

     

    if ( c is ImageButton ) {
    ImageButton lb = (ImageButton)c;
    if ( !string.IsNullOrEmpty( lb.Attributes["AjaxPostback"] ) )
    ScriptManager1.RegisterAsyncPostBackControl( lb );
    } RegisterControlsRecursive( c );
    }
    }

    This way you just need to run this call for each "main" user control you wish to register.

    Let me know if this helps!

    Nathan Zaugg

Page 1 of 1 (8 items)