2009-12-01 29 views
0

当单击asp:LinkBut​​ton“addToCartButton”(请参阅​​下面的代码)时,我想触发PostBack。InvalidOperationException:PostBack触发器无法在DataList的ItemTemplate中找到控件

我已经用asp:UpdatePanel“updPnlProductsList”(见下面的代码)声明了PostBack触发器。

<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"> 
           &nbsp; 
          </th> 
          <th align="left"> 
           Product Description 
          </th> 
          <th align="center"> 
           In Stock 
          </th> 
          <th align="center"> 
           Price 
          </th> 
          <th align="left"> 
           &nbsp; 
          </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;"> 
          &nbsp; 
         </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. --> 

不幸的是,当我运行这段代码我得到的错误:

出现InvalidOperationException:ID为“addToCartButton”控制找不到在UpdatePanel的“updPnlProductsList”触发。

有人请帮我参考DataList的ItemTemplate中的'addToCartButton'。

或者我可以在后面的asp:LinkBut​​ton代码中导致PostBack?我在C#中编写代码。

亲切的问候

沃尔特

回答

1

EDIT2:建议不能工作首选解决方案。试试这个(从你已经尝试过得到的)的建议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到触发器列表。

编辑1:这是我的想法。我没有测试它...

 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); 

      } 
     } 
+0

感谢joerage为您的快速反应。请你说明我该如何做到这一点? – 2009-12-01 14:58:13

+0

嗨joerage。我试图将其添加到我的代码后面: protected void productsList_ItemDataBound(object sender,DataListItemEventArgs e) //处理productsList的onItemDataBound事件。如果(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);脚本管理器(RegManager.RegisterPostBackControl)(e.Item.FindControl(“addToCartButton”)); } 它没有工作。同样的错误。 – 2009-12-01 15:11:18

+0

嗨joerage。我试过这个: protected void productsList_ItemDataBound(object sender,DataListItemEventArgs e) {//处理productsList的onItemDataBound事件。 如果(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { 控制控制= NULL; control = e.Item.FindControl(“addToCartButton”); PostBackTrigger trigger = new PostBackTrigger(); trigger.ControlID = control.ID; updPnlProductsList.Triggers.Add(trigger); } } 但它失败并出现相同的错误。 – 2009-12-01 15:49:21

相关问题