[TIP]Triggers and user controls

Last post 09-28-2006 4:20 PM by JDub. 5 replies.

Sort Posts:

  • [TIP]Triggers and user controls

    07-19-2006, 6:32 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs

    hello guys.

    well, since you cannot build your own triggers (http://forums.asp.net/thread/1345373.aspx), i though i'd share a quick tip on how to have partial postbacks when you have a dependency between an event on a user control and an updatepanel (ie, you have a trigger).

    so, what you have to do is call the registerasyncpostbackcontrol on the user control and pass in the control that will start the postback. here's a small sample:

    user control

    <%@ Control Language="C#" ClassName="MyUserControl" %>

    <script runat="server">
        private object aux = new object();
        public event EventHandler BtClicked
        {
            add
            {
                this.Events.AddHandler(aux, value);
            }
            remove
            {
                this.Events.RemoveHandler(aux, value);
            }
        }

        void h(object s, EventArgs e)
        {
            OnBtClicked(e);
        }

        protected void OnBtClicked(EventArgs a)
        {
            if (this.Events[aux] != null)
            {
                ((EventHandler)(this.Events[aux]))(this, a);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Microsoft.Web.UI.ScriptManager manager = Microsoft.Web.UI.ScriptManager.GetCurrent(this.Page);
            if (manager != null)
            {
                manager.RegisterAsyncPostBackControl(bt);
            }
        }
    </script>

    <asp:Button runat="server" ID="bt" Text="Submit" OnClick="h" UseSubmitBehavior="false" />

     

    aspx page

    <%@ Page Language="C#"  %>

    <%@ Register Src="MyUserControl.ascx" TagName="MyUserControl" 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">
        void h(object o, EventArgs e)
        {
            lbl.Text = DateTime.Now.ToString();
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <atlas:ScriptManager runat="server" ID="manager" EnablePartialRendering="true">
            </atlas:ScriptManager>
           
            <script type="text/javascript" src="Atlas.js">
            </script>
           
            <atlas:UpdatePanel runat="server" ID="pane">
                <ContentTemplate>
                    <%= DateTime.Now.ToString() %>
                </ContentTemplate>
                <Triggers>
                    <atlas:ControlEventTrigger ControlID="MyUserControl1" EventName="BtClicked" />
                </Triggers>
            </atlas:UpdatePanel>
            <%= DateTime.Now.ToString() %>
           
           
            <uc1:MyUserControl id="MyUserControl1" runat="server" onbtclicked="h">
            </uc1:MyUserControl></div>
            <asp:Label runat="server" ID="lbl" />
        </form>
    </body>
    </html>

    hope this helps.

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: [TIP]Triggers and user controls

    09-26-2006, 1:50 PM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • ASPInsiders
      TrustedFriends-MVPs

    Hi,

     uh, cool. Thanks!
     

    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: [TIP]Triggers and user controls

    09-26-2006, 2:08 PM
    • Member
      55 point Member
    • alanle
    • Member since 06-22-2006, 9:43 PM
    • Posts 11

    Excellent Post.  I was trying to do something similar and didn't figure it out.

    I see now.  The trick is to expose the UserControl's control event and register the asynchronous postback on load.
     

  • Re: [TIP]Triggers and user controls

    09-27-2006, 1:54 PM
    • Contributor
      5,535 point Contributor
    • Eilon
    • Member since 06-26-2002, 2:14 PM
    • Redmond, WA
    • Posts 951
    • AspNetTeam

    Luis, that's a great sample. I'd like to share a few comments:

    1. I think your example is the way I'd recommend doing this anyway. If the UserControl encapsulates some piece of logic, it should expose only the relevant parts of its behavior through public methods/properties/events.
    2. The constructors on trigger will not be internal when we ship. We have a general goal of saying that any piece of functionality that we can build into Atlas a customer should be able to implement themselves. I'm certainly not promising anything specific, nor am I saying that this is true for everything, but it's a general goal.
    3. We've slightly reworked a few things about how ControlEventTrigger works. The EventName is now optional if you want to capture all postbacks that were caused by a control inside the target control. However if your event might be raised even though it wasn't the one that caused the postback (e.g. a TextBox's TextChanged event) then you still need to specify EventName.

    Thanks,

    Eilon

    Blog: http://weblogs.asp.net/LeftSlipper/
  • Re: [TIP]Triggers and user controls

    09-27-2006, 2:10 PM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs

    hello.

    thanks for the info!

    btw, both Manuel and I are curious about multiple inheritance introduced by the OO extensions:

    http://forums.asp.net/thread/1410935.aspx

    well, as Manuel points out, there's some basic infraestructure which seems to point that multiple inheritance should be supported (or at least was a goal at the very begining?). however, the current version doesn't support it in most scenarios, as we all know. any official comments on this? :)

     

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: [TIP]Triggers and user controls

    09-28-2006, 4:20 PM
    • Member
      15 point Member
    • JDub
    • Member since 06-19-2002, 11:35 AM
    • Posts 3

    That is just what the doctor ordered!

    Thank you very much.

    + JDub
Page 1 of 1 (6 items)