如何以编程方式将触发器添加到 ASP.NET UpdatePanel?
我正在尝试编写一个报价生成器。对于每种产品,都有一组选项。我想为每个选项动态添加一个下拉列表,然后将它们的 SelectedIndexChanged 事件全部连接起来以更新报价成本。
我在将 DropDownList 控件添加到 UpdatePanel 时没有遇到任何问题,但我似乎无法连接事件。
页面加载后,下拉列表及其数据就在那里,但更改它们不会调用 SelectedIndexChanged 事件处理程序,QuoteUpdatePanel 也不会更新。 我有这样的东西:
编辑:自不支持以编程方式添加 AsyncPostBackTrigger 控件,我已将代码更改为此,但我仍然没有收到事件:
编辑 2: 尝试添加占位符将下拉列表添加到(而不是直接添加到 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();
}
I am trying to write a quote generator. For each product, there are a set of options. I want to dynamically add a drop down list for each option, and then have their SelectedIndexChanged events all wired up to update the quote cost.
I am not having any trouble adding the DropDownList controls to my UpdatePanel, but I can't seem to wire up the events.
After the page loads, the drop downs are there, with their data, but changing them does not call the SelectedIndexChanged event handler, nor does the QuoteUpdatePanel update.
I have something like this:
Edit: Since programmatically adding AsyncPostBackTrigger controls is not supported, I've change my code to this, but I still don't get the event:
Edit 2: Tried adding a PlaceHolder to add the drop down lists to (instead directly into the ContentTemplateContainer, still no events firing.
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>
The code to add the dropdowns and the event they are to be wire up for:
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,以编程方式向UpdatePanel
控件添加异步触发器是可行的。解决方法是将它们添加到
Page_Init
事件中并设置触发器的ControlID
属性到控件的唯一 id 值:似乎成功了。我在上面创建了类似的页面/控件结构。因此,有用户控件
QuotePropertyControl
和保存此控件的Default
页面。我添加了 dropDownList.AutoPostBack = true 属性,并且能够从下拉列表中捕获异步回发。所以,猜测问题出在这个属性上。
还有一件事:如何注册异步触发器并不重要;两种变体 ScriptManager.RegisterAsyncPostBackControl 和通过 AsyncPostBackTrigger 都像魅力一样工作(直到页面的 init 事件)。
这是我的做法:
QuotePropertyControl.ascx.cs
QuotePropertyControl.ascx
Default.aspx.cs
Default.aspx >
AFAIK, adding async triggers to theUpdatePanel
control programmatically is working.The workaround is to add them in
Page_Init
event and set trigger'sControlID
property to the control unique id value:Seems made this work. I created similar structure of page/control above. So there are user control
QuotePropertyControl
andDefault
page which holds this control.I added
dropDownList.AutoPostBack = true
property and was able to catch an async postback from the dropdown. So, guessing the problem was in this property.One more thing: it's really doesn't matter how to register async trigger; both variants
ScriptManager.RegisterAsyncPostBackControl
and throughAsyncPostBackTrigger
worked like a charm (until the init event of the page).Here is how I did it:
QuotePropertyControl.ascx.cs
QuotePropertyControl.ascx
Default.aspx.cs
Default.aspx