简体   繁体   中英

Can I trigger an Update Panel from a Drop Down list in a User Control

I have a user control in a master page with two drop down lists. When the user selects an item out of either ddl, I want to load a specific user control inside an update panel on the content page. I can't figure out how to get the user control to trigger the update panel. Any suggestions are very much appreciated.

Master

    <%@ Register src="toolbar.ascx" tagname="toolbar" tagprefix="uc1" %>
<head id="Head1" runat="server">
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
    </div>
    <uc1:toolbar ID="toolbar1" runat="server" />
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </form>
</body>
</html>

User Control

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="toolbar.ascx.cs" Inherits="Blah.toolbar" %>
<asp:DropDownList ID="ddlDesiredPage" runat="server" AutoPostBack="True" 
            EnableViewState="True" 
            onselectedindexchanged="goToSelectedPage">
            <asp:ListItem Value="-">DDL 1</asp:ListItem>
        </asp:DropDownList>
        &nbsp;
<asp:DropDownList ID="ddlDesiredPageSP" runat="server" AutoPostBack="True" 
        EnableViewState="True"
        onselectedindexchanged="goToSelectedPage">
            <asp:ListItem Value="-">DDL 2</asp:ListItem>
</asp:DropDownList>

Content Page

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" onload="UpdatePanel1_Load">
        <ContentTemplate>
            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        </ContentTemplate>
        <Triggers>
        ?????????????????????????????????
        </Triggers>
    </asp:UpdatePanel>
</asp:Content>

If you want to update the panel via the User Control you've created, you could try setting the UpdatePanel's UpdateMode = Conditional . Then, in your User Control's click event (or whichever event), have something like this:

 UpdatePanel mUpdatePanel = this.Page.Master.FindControl("upContent") as UpdatePanel;  
    if (mUpdatePanel != null)  
    {
        mUpdatePanel.Update();  
    }
    else
    {
         //update panel not found
    }

UPDATE

Since you can't access your triggers declaratively, you could add them from the code-behind. On your content page, add something like this:

 AsyncPostBackTrigger triggerUserControl = new AsyncPostBackTrigger();
                        DropDownList ucDDL = this.Page.Master.FindControl("ddlDesiredPage") as DropDownList;
                        triggerUserControl.ControlID = ucDDL.ID;
                        triggerUserControl.EventName = "Click";

                        UpdatePanel1.Triggers.Add(triggerUserControl);

Do the same for the other DropDownList. I haven't tried this, but it seems reasonable.

Try adding a postback trigger to your UpdatePanel:

<Triggers>
    <asp:PostBackTrigger ControlID="ddl..." />
</Triggers>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM