这是我的情况:
页面:
<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 的选择发生变化时,我想更新页面中的 UpdatePanel(异步)。我尝试了 AutoPostBack="true",但这总是会导致页面的完整回发(请参阅 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,因此无法相应地设置触发器。
我该怎么做?
最佳答案
也许有一个技巧可以将此代码放在下拉列表中。
dropDown1.Attributes["onchange"] =
Page.ClientScript.GetPostBackEventReference(ParentUpdatePanel, "") + "; return false;";
当您更改下拉列表时,您可以使用直接 JavaScript 调用向 UpdatePanel 发送更新事件。
关于c# - 从 ChildControl 中的 Dropdownlist 触发 UpdatePanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9210899/