Can I specify a UserControl custom event as a Trigger for update panel on Parent page?

Last post 12-05-2007 2:00 PM by ldadams. 6 replies.

Sort Posts:

  • Can I specify a UserControl custom event as a Trigger for update panel on Parent page?

    06-19-2007, 11:00 AM
    • Loading...
    • kylen
    • Joined on 12-08-2006, 3:56 PM
    • Posts 48

    I have a parent page which hosts several user controls contained within an accordion on the parent page which are created dynamically. 

    My Parent page is wrapped in an UpdatePanel and UserControls are also wrapped in UpdatePanels due to only needing partial postback on most of the user interaction with the control.

    Lets say I have an event on each User Control that bubbles back to the parent page called "OnEnableLink". When this event is raised from the UserControl, I'd like to enable a LinkButton on the Control's Parent Page.  When the event is called the code on the parent page catches the event and enables the link [lnkExample.Enabled = true;] but the page isn't reflecting the change as the link button is still disabled.  My assumption is the reason is it's not updating due to the UpdatePanel.  I've tried calling ParentPageUpdatePanel.Update() within the event after i enable the link button and it results in an error that Update() can't be called outside of PreRender.  I'm assuming I need a trigger to update the Parent Page when it intercepts the event from the UserControl but since they are created dynamically I'm lost :-( 

    Thanks in advance for any suggestions!

  • Re: Can I specify a UserControl custom event as a Trigger for update panel on Parent page?

    06-20-2007, 2:50 AM
    • Loading...
    • Garbin
    • Joined on 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,494
    • ASPInsiders
      TrustedFriends-MVPs

    Hi,

    if a control that does a postback is inside an UpdatePanel, by default it triggers a partial update regardless of the particular event raised.

    Probably the cause of the issue is in the way dynamic controls are handled. At what stage of the server page lifecycle are you creating them?

    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: Can I specify a UserControl custom event as a Trigger for update panel on Parent page?

    06-20-2007, 3:07 AM
    • Loading...
    • kylen
    • Joined on 12-08-2006, 3:56 PM
    • Posts 48

    They are created in Page_Init. If my userControl has an UpdatePanel wrapped around it and the Parent page of the control is wrapped with another UpdatePanel doesn't the partial render on the control only update contents on the control and not my parent page? 

  • Re: Can I specify a UserControl custom event as a Trigger for update panel on Parent page?

    06-25-2007, 5:56 AM
    Answer

    Hi.kylen

     

    I worked it out,the following codes works cool,No problem at all!

     

    I specified the UserControl custom event "TextID_Clicked" as a Trigger for update panel on Parent page.

     BTW,Another sulotion is mentioned at Trigger PostBack to an UpdatePanel from a separate UserControl?  

    uctextlist.ascx :

     

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

     

    <script runat="server">

        public delegate void ClickEventHandler(object sender, CommandEventArgs e);

        public event ClickEventHandler TextID_Clicked;

        private ArrayList ar = new ArrayList();

        public ArrayList VSar

        {

            set

            {

                ar = value;

            }

            get

            {

                return ar;

            }

        }

     

        public void LoadLink()

        {

            Repeater1.DataSource = ar;

            Repeater1.DataBind();

        }

        protected void lb_Command(object sender, CommandEventArgs e)

        {

            if (TextID_Clicked != null) TextID_Clicked(this, e);

        }

    </script>

     

    <div style="overflow:auto; width:100px; height:100px">

    <asp:Repeater ID="Repeater1" runat="server">

        <HeaderTemplate>

            <table>

        </HeaderTemplate>

        <ItemTemplate>

            <tr>

                <td style="background-color:Gray">

                    <asp:LinkButton ID='b1' runat="server" CommandArgument='<%# Container.DataItem %>' OnCommand="lb_Command"><%# Container.DataItem %></asp:LinkButton>

                </td>

            </tr>

        </ItemTemplate>

        <AlternatingItemTemplate>

            <tr>

                <td>

                    <asp:LinkButton ID='b2' runat="server" CommandArgument='<%# Container.DataItem %>' OnCommand="lb_Command"><%# Container.DataItem %></asp:LinkButton>

                </td>

            </tr>

        </AlternatingItemTemplate>

        <SeparatorTemplate>

            <tr>

                <td>

                    -----------------------------------------------------------------</td>

            </tr>

        </SeparatorTemplate>

        <FooterTemplate>

            </table>

        </FooterTemplate>

    </asp:Repeater>

    </div>

     

    uctextdetail.ascx:

     

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

     

    <script runat="server">

        public String TextBox1Text

        {

            get

            {

                return TextBox1.Text;

            }

            set

            {

                TextBox1.Text = value;

            }

        }

    </script>

     

    <asp:Label ID="Label1" runat="server" Text="Detail:"></asp:Label><br />

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

     

    Default29.aspx:

     

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

     

    <%@ Register Src="uctextlist.ascx" TagName="uctextlist" TagPrefix="uc1" %>

    <%@ Register Src="uctextdetail.ascx" TagName="uctextdetail" TagPrefix="uc2" %>

    <!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 cboSystem_SelectedIndexChanged(object sender, EventArgs e)

        {

            cboTextType.Items.Clear();

            cboTextType.Items.Add(new ListItem("", "0"));

            for (int i = 1; i < 4; i++)

            {

                ListItem item = new ListItem(cboSystem.SelectedItem.Text + i.ToString(), i.ToString());

                cboTextType.Items.Add(item);

            }

            cboTextType.SelectedIndex = 0;

        }

     

        protected void cboTextType_SelectedIndexChanged(object sender, EventArgs e)

        {

            cboAttribute.Items.Clear();

            cboAttribute.Items.Add(new ListItem("", "0"));

            for (int i = 1; i < 4; i++)

            {

                ListItem item = new ListItem(cboTextType.SelectedItem.Text + i.ToString(), i.ToString());

                cboAttribute.Items.Add(item);

            }

            cboAttribute.SelectedIndex = 0;

        }

     

        protected void btnSearch_Click(object sender, EventArgs e)

        {

            //myTextList.Controls.Clear();

            //myTextList.VSht = null;

            myTextList.VSar.Clear();

            for (int i = 1; i < 4; i++)

            {

                myTextList.VSar.Add(i.ToString() + " " + cboSystem.SelectedItem.Text + "_" + cboTextType.SelectedItem.Text + "_" + cboAttribute.SelectedItem.Text + " ");

                //myTextList.AddLinkButton(i.ToString() + " " + cboSystem.SelectedItem.Text + "_" + cboTextType.SelectedItem.Text + "_" + cboAttribute.SelectedItem.Text + " ");

            }

            myTextList.LoadLink();

            myTextList.Visible = true;

        }

     

        protected void myTextList_TextID_Clicked(object sender, CommandEventArgs e)

        {

            myTextDetail.TextBox1Text = e.CommandArgument.ToString();

            myTextDetail.Visible = true;

        }

    </script>

     

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

        <title>Untitled Page</title>

    </head>

    <body>

        <form id="form1" runat="server">

            <asp:ScriptManager ID="ScriptManager1" runat="server">

            </asp:ScriptManager>

            <div>

                <asp:UpdatePanel ID="updateSearch" ChildrenAsTriggers="false" UpdateMode="Conditional"

                    runat="server">

                    <ContentTemplate>

                        <asp:DropDownList ID="cboSystem" runat="server" AutoPostBack="True" OnSelectedIndexChanged="cboSystem_SelectedIndexChanged">

                            <asp:ListItem Value="1" Selected="True">test1</asp:ListItem>

                            <asp:ListItem Value="2">test2</asp:ListItem>

                            <asp:ListItem Value="3">test3</asp:ListItem>

                        </asp:DropDownList>

                        <asp:DropDownList ID="cboTextType" runat="server" AutoPostBack="True" OnSelectedIndexChanged="cboTextType_SelectedIndexChanged">

                            <asp:ListItem Value="0" Selected="True"></asp:ListItem>

                        </asp:DropDownList>

                        <asp:DropDownList ID="cboAttribute" runat="server">

                            <asp:ListItem Value="0" Selected="True"></asp:ListItem>

                        </asp:DropDownList>

                        <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />

                    </ContentTemplate>

                    <Triggers>

                        <asp:AsyncPostBackTrigger ControlID="cboSystem" EventName="SelectedIndexChanged" />

                        <asp:AsyncPostBackTrigger ControlID="cboTextType" EventName="SelectedIndexChanged" />

                    </Triggers>

                </asp:UpdatePanel>

                <asp:UpdatePanel ID="updateList" ChildrenAsTriggers="false" UpdateMode="Conditional"

                    runat="server">

                    <ContentTemplate>

                        <uc1:uctextlist ID="myTextList" Visible="false" runat="server" OnTextID_Clicked="myTextList_TextID_Clicked" />

                    </ContentTemplate>

                    <Triggers>

                        <asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click" />

                    </Triggers>

                </asp:UpdatePanel>

                <asp:UpdatePanel ID="updateDetail" UpdateMode="Conditional" runat="server">

                    <ContentTemplate>

                        <uc2:uctextdetail ID="myTextDetail" Visible="false" runat="server" />

                    </ContentTemplate>

                    <Triggers>

                        <asp:AsyncPostBackTrigger ControlID="myTextList" EventName="TextID_Clicked" />

                    </Triggers>

                </asp:UpdatePanel>

            </div>

        </form>

    </body>

    </html>

     

    Let me know if you need more info.

    Sincerely,
    Jin-Yu Yin

    Sincerely,
    Jin-Yu Yin
    Microsoft Online Community Support
  • Re: Can I specify a UserControl custom event as a Trigger for update panel on Parent page?

    06-28-2007, 12:19 AM

    Thanks Jin-Yu Yin Smile

    It will help us...

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: Can I specify a UserControl custom event as a Trigger for update panel on Parent page?

    07-03-2007, 10:59 AM
    • Loading...
    • kylen
    • Joined on 12-08-2006, 3:56 PM
    • Posts 48
    Thanks Jin-Yu Yin! Works perfectly...
  • Re: Can I specify a UserControl custom event as a Trigger for update panel on Parent page?

    12-05-2007, 2:00 PM
    • Loading...
    • ldadams
    • Joined on 09-08-2004, 11:34 AM
    • Posts 37

    Hi,

     Would it be possible to trigger an updatepanel from a usercontrol that resides outside of the updatepanel?

Page 1 of 1 (7 items)