我正在尝试编写报价生成器。对于每种产品,都有一组选项。我想为每个选项动态添加一个下拉列表,然后将他们的SelectedIndexChanged事件全部连接起来以更新报价成本。
将DropDownList控件添加到UpdatePanel时没有任何问题,但我似乎无法连接事件。
页面加载后,下拉列表及其数据,但更改它们不会调用SelectedIndexChanged事件处理程序,QuoteUpdatePanel也不会更新。 我有这样的事情:
修改:自programmatically adding AsyncPostBackTrigger controls is not supported以来,我已将我的代码更改为此内容,但我仍然没有收到此事件:
编辑2:尝试添加PlaceHolder以将下拉列表添加到(而不是直接添加到ContentTemplateContainer中,仍然没有触发事件。
QuotePanel.ASCX
<asp:ScriptManager ID="ScriptManager" runat="server" />
<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>
添加下拉列表及其要接线的事件的代码:
protected void PopluateUpdatePanel(IEnumerable<IQuoteProperty> standardOptions)
{
foreach (IQuoteProperty standardOp in standardOptions)
{
QuotePropertyDropDownList<IQuoteProperty> dropDownList = new QuotePropertyDropDownList<IQuoteProperty>(standardOp);
dropDownList.SelectedIndexChanged += QuotePropertyDropDown_SelectedIndexChanged;
dropDownList.ID = standardOp.GetType().Name + "DropDownList";
dropDownList.CssClass = "quote-property-dropdownlist";
Label propertyLabel = new Label() {Text = standardOp.Title, CssClass = "quote-property-label"};
StandardOptionsPlaceHolder.Controls.Add(propertyLabel);
StandardOptionsPlaceHolder.Controls.Add(dropDownList);
_standardOptionsDropDownLists.Add(dropDownList);
ScriptManager.RegisterAsyncPostBackControl(dropDownList);
}
}
void QuotePropertyDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
QuoteCostLabel.Text = QuoteCost.ToString();
StandardOptionsUpdatePanel.Update();
}
答案 0 :(得分:4)
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
都像魅力一样(直到页面的init事件)。
我是这样做的:
<强> QuotePropertyControl.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();
}
<强> QuotePropertyControl.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>
<强> Default.aspx.cs 强>
string[] names = { "ab", "bc", "ef" };
protected void Page_Init(object sender, EventArgs e)
{
ctlQuoteProperty.PopluateUpdatePanel(names);
}
<强> Default.aspx的强>
<%@ 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>