【问题标题】:Trigger UpdatePanel from Dropdownlist in ChildControl从 ChildControl 中的下拉列表触发 UpdatePanel
【发布时间】:2012-02-09 12:32:44
【问题描述】:

这是我的情况:

页面:

<asp:UpdatePanel ID="updatePanel1" runat="server" ChildrenAsTriggers="true">
  <ContentTemplate>
    ...
    <uc:ChildControl ID="ucChild" runat="server" />
    ...
  </ContentTemplate>
</asp:UpdatePanel>

儿童控制:

...
<asp:DropDownList id="dropDown1" runat="server" />
...

当 ChildControl 中 DropDownList 的选择发生变化时,我想(异步)更新 Page 中的 UpdatePanel。我尝试了 AutoPostBack="true",但这总是会导致页面的完整 PostBack(请参阅 this question)。

我尝试使用

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="???" EventName="SelectedIndexChanged" />
</Triggers>

但“dropDown1”和“ucChild.dropDown1”都不能用作 ControlID 的值。

我还尝试将 UpdatePanel 的引用传递给 ChildControl 并通过以下方式添加触发器:

protected override void OnPreRender(EventArgs e)
{
    if (ParentUpdatePanel != null)
    {
        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
        trigger.ControlID = dropDown1.ID;
        trigger.EventName = "SelectedIndexChanged";
        ParentUpdatePanel.Triggers.Add(trigger);
    }
    base.OnPreRender(e);
}

(也尝试使用 dropDown1.ChildID)

但是当下拉列表中的值更改时,我仍然无法触发 UpdatePanel。问题似乎是 UpdatePanel 看不到 ChildControl 中的 Control,因此无法相应地设置 Trigger。

我该怎么做?

【问题讨论】:

    标签: c# asp.net ajax drop-down-menu updatepanel


    【解决方案1】:

    也许有一个技巧可以将此代码放在下拉列表中。

    dropDown1.Attributes["onchange"] =   
    Page.ClientScript.GetPostBackEventReference(ParentUpdatePanel, "") + "; return false;";
    

    当您更改下拉列表时,您会使用直接 javascript 调用向 UpdatePanel 发送更新事件。

    【讨论】:

    • 它不适用于您的代码,因为 没有评估,但我通过后面的代码运行它:dropDown1.Attributes["onchange"] = Page.ClientScript.GetPostBackEventReference(ParentUpdatePanel, "") + "; return false;"; 谢谢!
    • 如果您可以将其编辑为有效的代码,我将很乐意接受它作为答案。 :)
    • @atticae 正确,这必须作为属性添加到后面的代码中。
    • @atticae 已更新,对于“返回 false”,我不是 100% 必要的。
    • 嗯,我认为它有效并且它实际上触发了更新,但响应是:“该页面的视图状态无效”。知道如何解决这个问题吗?
    【解决方案2】:

    如果下拉列表控件位于更新面板中,则在下拉列表控件上设置 AutoPostBack=True 不应刷新整个页面。

    我创建了一个简单的例子:

    default.aspx:

    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="up" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
            <ContentTemplate>            
                <uc:UserControl ID="ucChild" runat="Server"></uc:UserControl>
                <asp:Label ID="lbl" runat="server"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
    

    default.aspx.cs(代码隐藏):

    public partial class _default : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            lbl.Text = ucChild.value;
        }   
    }
    

    UserControl.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl.ascx.cs" Inherits="somenamespace.UserControl" %>
    <asp:DropDownList runat="server" ID="ddl" AutoPostBack="true">
        <asp:ListItem Text="1" Value="1"></asp:ListItem>
        <asp:ListItem Text="2" Value="2"></asp:ListItem>
    </asp:DropDownList>
    

    UserControl.ascx.cs(代码隐藏):

    public partial class UserControl : System.Web.UI.UserControl {
        public string value {
            get { return ddl.SelectedValue.ToString(); }
        }
    }
    

    当我更改下拉列表时,标签会更新而没有完整的回帖。

    【讨论】:

    • 我知道它“不应该刷新整个页面”,但出于某种原因,它对我来说确实如此,而且从我的研究来看,我不是唯一一个:stackoverflow.com/questions/2138565/…
    • 包含更多代码,以便我们查看是否存在其他明显问题。更新面板中的所有控件是否会导致完整的页面刷新或只是有问题的下拉列表? how is the ddl getting populated, and what is it doing when the selection is changed?
    • 不需要,Aristo 的解决方法对我有用。这个问题已经占用了我太多时间。我可能会在周末尝试使用您的代码示例重现它并提供更多信息。
    猜你喜欢
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多