我可以从用户控件的下拉列表中触发更新面板吗

发布于 2024-08-25 22:18:59 字数 1983 浏览 1 评论 0原文

我在主页中有一个带有两个下拉列表的用户控件。当用户从任一 ddl 中选择一个项目时,我想在内容页面上的更新面板内加载特定的用户控件。我不知道如何让用户控件触发更新面板。非常感谢任何建议。

主控

    <%@ 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>

用户控制

    <%@ 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>

内容页面

<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>

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>
         
<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毅然前行 2024-09-01 22:18:59

如果您想通过您创建的用户控件更新面板,您可以尝试设置 UpdatePanel 的 UpdateMode = Conditional。然后,在用户控件的单击事件(或任何事件)中,添加如下内容:

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

UPDATE

由于您无法以声明方式访问触发器,因此您可以从代码隐藏中添加它们。在您的内容页面上,添加如下内容:

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

                        UpdatePanel1.Triggers.Add(triggerUserControl);

对另一个 DropDownList 执行相同的操作。我没有尝试过,但这似乎是合理的。

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.

内心旳酸楚 2024-09-01 22:18:59

尝试向 UpdatePanel 添加回发触发器:

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

Try adding a postback trigger to your UpdatePanel:

<Triggers>
    <asp:PostBackTrigger ControlID="ddl..." />
</Triggers>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文