Afaik,将异步触发器添加到
UpdatePanel
以编程方式控制正在工作。
解决方法是将它们添加到
Page_Init
事件和设置触发器
ControlID
控件唯一ID值的属性:
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
// unique id instead of client id
trigger.ControlID = yourDropDownControl.UniqueID;
trigger.EventName = "SelectedIndexChanged";
QuoteUpdatePanel.Triggers.Add(trigger);
似乎成功了。我在上面创建了类似的页面/控件结构。所以有用户控制
QuotePropertyControl
和
Default
保存此控件的页。
我补充说
dropDownList.AutoPostBack = true
属性,并且能够从下拉列表中捕获异步回发。所以,猜测问题出在这个属性中。
还有一件事:如何注册异步触发器并不重要;这两种变体
ScriptManager.RegisterAsyncPostBackControl
通过
AsyncPostBackTrigger
像个魔咒一样工作(直到页面的初始事件)。
我是这样做的:
报价属性控制.ascx.cs
private string[] data = { "a", "b", "c", "d", "e" };
public void PopluateUpdatePanel(IEnumerable<string> standardOptions)
{
foreach (string standardOp in standardOptions)
{
DropDownList dropDownList = new DropDownList();
dropDownList.SelectedIndexChanged +=
QuotePropertyDropDown_SelectedIndexChanged;
dropDownList.ID = standardOp + "DropDownList";
dropDownList.CssClass = "quote-property-dropdownlist";
dropDownList.AutoPostBack = true;
dropDownList.DataSource = data;
dropDownList.DataBind();
Label propertyLabel = new Label() { Text = standardOp };
StandardOptionsPlaceHolder.Controls.Add(propertyLabel);
StandardOptionsPlaceHolder.Controls.Add(dropDownList);
ScriptManager.GetCurrent(Page)
.RegisterAsyncPostBackControl(dropDownList);
}
}
protected void QuotePropertyDropDown_SelectedIndexChanged(
object sender,
EventArgs e
)
{
StandardOptionsUpdatePanel.Update();
}
报价属性控制.ascx
<asp:UpdatePanel ID="QuoteUpdatePanel" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
Cost:
<asp:Label ID="QuoteCostLabel" runat="server" />
<fieldset id="standard-options">
<legend>Standard Options</legend>
<asp:UpdatePanel ID="StandardOptionsUpdatePanel"
runat="server"
ChildrenAsTriggers="true"
UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder ID="StandardOptionsPlaceHolder"
runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
Abx.CS
string[] names = { "ab", "bc", "ef" };
protected void Page_Init(object sender, EventArgs e)
{
ctlQuoteProperty.PopluateUpdatePanel(names);
}
阿斯克斯
<%@ Register Src="~/QuotePropertyControl.ascx"
TagPrefix="uc"
TagName="QuoteProperty" %>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager" runat="server" />
<uc:QuoteProperty runat="server"
ID="ctlQuoteProperty">
</uc:QuoteProperty>
</div>
</form>