AsyncPostBackTrigger from another user control

Last post 07-27-2007 2:58 AM by Jin-Yu Yin - MSFT. 2 replies.

Sort Posts:

  • AsyncPostBackTrigger from another user control

    07-23-2007, 11:32 AM
    • Member
      94 point Member
    • junkyP
    • Member since 09-15-2004, 10:57 AM
    • Posts 25

    Hi,

    I have 2 web user controls in same master page.

    And in each user control there is datalist.

    In first user control's datalist there is link button.

    What I'm trying to do that is

    when I click linkbutton in first user control, update the second datalist in second user control with Update Panel.

    What is the easiest way to add postback trigger to another control in another user control.

    Thanks in advance.

  • Re: AsyncPostBackTrigger from another user control

    07-27-2007, 2:37 AM
    Answer

    Hi,

    In my opinion, it's not a good practice to have one UserControl rely on another UserControl, unless the second one is placed inside the first one.

    Any way, you may try the following way:

    1. Register the second UC as the trigger programmatically in PageLoad of the first UC:

                Page_Load()
                {         

                         Control c = this.Page.FindControl("id of the second UC");

                         ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(c);

                 }

     

    2. Call Update method explicitly on the UpdatePanel in the second UC's linkButton click event handler:

              click()

             {

                       UpdatePanel up = this.Page.FindControl("id of the first UC").FindControl("id of the updatePanel") as UpdatePanel;

                       up.Update();
     

              }
     

    Hope this helps although personally I'm against this approach. 

  • Re: AsyncPostBackTrigger from another user control

    07-27-2007, 2:58 AM
    Answer

    Hi junkyP,

    In order to do that, we can do the following stepes :

    1.     Define a custom property to store the client ID of a hidden trigger control of the Update Panel(Button1clientID property of Gallery User Control):

    <%@ Control Language="C#" ClassName="Gallery" %>
    <script runat="server">
        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToString();
        }
        public string Button1clientID
        {
            get
            {
                return Button1.ClientID;
            }
        }
    </script>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <div style="visibility: hidden">
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" UseSubmitBehavior="false" Text="Button" /></div>
        </ContentTemplate>
    </asp:UpdatePanel>

    2.    Define a custom property in the other User Control(Button1clientID property of Upload User Control):

    <%@ Control Language="C#" ClassName="Upload" %>
    <script runat="server">
        private string buttonclientID = "";
        public string Button1clientID
        {
            set
            {
                buttonclientID = value;
            }
        }

    </script>

    3.    Set Upload1.Button1clientID = Gallery1.Button1clientID in the page_Load:

    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            Upload1.Button1clientID = Gallery1.Button1clientID;
        }
    </script>

    4.    Fire the click event of the hidden trigger control manually:

    <script type="text/javascript">
         function submitForm()
         {
             document.getElementById("<%=buttonclientID %>").click();
         }
    </script>

    <button onclick="javascript:submitForm();">Upload</button>

    Here is the full sample code: 

    http://forums.asp.net/p/1117746/1739422.aspx#1739422 
    Related threads: 
    http://forums.asp.net/t/1117746.aspx (Trigger PostBack to an UpdatePanel from a separate UserControl?)

    Let me know if you need more info. 

    Best Regards

    Sincerely,
    Jin-Yu Yin
    Microsoft Online Community Support
Page 1 of 1 (3 items)
Microsoft Communities