InvalidOperationException异常:回发触发器不能一个DataList的ItemTemplate模板内查找控制 [英] InvalidOperationException: PostBack Trigger Cannot Find a Control within the ItemTemplate of a DataList
问题描述
我想触发回发时的asp:LinkButton的addToCartButton点击(见code以下)
I would like to trigger a PostBack when the asp:LinkButton "addToCartButton" is clicked (see code below).
我已经声明了一个触发回发用asp:UpdatePanel的updPnlProductsList(见code以下)
I have declared a PostBack Trigger with the asp:UpdatePanel "updPnlProductsList" (see code below).
<asp:Panel ID="pnlProductsList" runat="server" Visible="false">
<asp:UpdatePanel ID="updPnlProductsList" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="featured">
<h3>Product listing</h3>
<div class="product clearfix">
<asp:DataList ID="productsList" runat="server" DataKeyField="prodid" OnItemCommand="productsList_ItemCommand"
OnItemDataBound="productsList_ItemDataBound">
<HeaderTemplate>
<table>
<col width="85" />
<col width="315" />
<col width="85" />
<col width="315" />
<col width="85" />
<tr>
<th align="left">
</th>
<th align="left">
Product Description
</th>
<th align="center">
In Stock
</th>
<th align="center">
Price
</th>
<th align="left">
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td height="85" valign="top">
<asp:HyperLink ID="standardImage" Style="float: left; margin-right: 5px; border: 2px;
vertical-align: top;" Width="75" Height="75" runat="server"></asp:HyperLink>
</td>
<td height="85" valign="top">
<asp:LinkButton ID="lbProductDescription" CommandName="show_product" runat="server" />
<p>
<asp:Label ID="MfPartNo" runat="server" /></p>
</td>
<td height="85" align="center" valign="top">
<p>
<asp:Label ID="inventoryTextLabel" runat="server" /></p>
</td>
<td style="padding-bottom: 10px;" height="85" align="center" valign="top">
<div class="product-actions clearfix">
<p class="price">
<strong>
<asp:Label ID="sellPriceLabel" runat="server" Style="font-size: 0.9em;" /></strong>
</p>
</div>
</td>
<td style="padding-bottom: 10px;" height="85" valign="top">
<div class="product-actions clearfix">
<p class="view">
<asp:LinkButton ID="addToCartButton" runat="server" Text="<span><strong>Buy</strong></span>"
CommandName="add_to_cart" class="newactionbutton" />
</p>
</div>
</td>
</tr>
</div>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan="5" style="border-bottom: dotted 1px gray; line-height: 0.1em;">
</td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="addToCartButton" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
<!-- /The all new Products List. -->
不幸的是,当我运行这个code我得到的错误:
Unfortunately, when I run this code I get the error:
InvalidOperationException异常:ID为addToCartButton的控制不能在UpdatePanel的'updPnlProductsList'触发器中找到
InvalidOperationException: A control with ID 'addToCartButton' could not be found for the trigger in UpdatePanel 'updPnlProductsList'.
会有人帮我在DataList的ItemTemplate中中引用了addToCartButton。
Would someone please help me reference the 'addToCartButton' within the ItemTemplate of the DataList.
或者,也许我可以导致ASP回发:身后的LinkButton code?我编码在C#。
Or maybe I can cause a PostBack in the asp:LinkButton code behind? I'm coding in C#.
亲切的问候
沃尔特
推荐答案
EDIT2:建议不能工作首选解决方案。试试这个(从你已经尝试过得到的)的建议这里:
First solution suggested cannot work. Try this (derived from what you already tried) as suggested here:
productsList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(e.Item.FindControl("addToCartButton"));
// Add this update call.
updPnlProductsList.Update();
}
}
在服务器端事件添加您的控制OnRowDataBound()。在这一点上,你会加入合适的ID到触发器列表中。
Add your control on the server side event OnRowDataBound(). At this point you will be adding the right id to the Triggers list.
EDIT1:这是我的想法。我没有测试它...
This is what I had in mind. I did not test it...
protected void gv_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType = DataControlRowType.DataRow)
{
// TODO: Find control within the row.
Control control = null;
var trigger = new AsyncPostBackTrigger();
trigger.ControlID = control.ID;
updPnlProductsList.Triggers.Add(trigger);
}
}
这篇关于InvalidOperationException异常:回发触发器不能一个DataList的ItemTemplate模板内查找控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!