2009-12-01 6 views
0

asp : LinkButton "addToCartButton"을 클릭하면 포스트 백을 트리거하고 싶습니다 (아래 코드 참조).InvalidOperationException : PostBack 트리거가 DataList의 ItemTemplate에서 컨트롤을 찾을 수 없음

나는 asp : UpdatePanel "updPnlProductsList"(아래 코드 참조)로 포스트 백 트리거를 선언했습니다.

경우 InvalidOperationException : ID 'addToCartButton'와 컨트롤이 UpdatePanel 'updPnlProductsList'에서 트리거를 찾을 수 없습니다 나는이 코드를 실행하면

<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. --> 

불행하게도, 나는 오류가 발생합니다.

누군가가 DataList의 ItemTemplate 내에서 'addToCartButton'을 참조하도록 도와 주시겠습니까?

또는 어쩌면 내가 뒤에있는 ASP : LinkButton 코드에서 포스트 백을 일으킬 수 있습니까? 나는 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. 내가 어떻게 할 수 있는지 설명해 주시겠습니까? –

+0

안녕하세요. 이 코드를 뒤에 코드에 추가하려고 시도했습니다. protected void productsList_ItemDataBound (Object sender, DataListItemEventArgs e) { // productsList의 onItemDataBound 이벤트를 처리합니다. if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { ScriptManager scriptManager = ScriptManager.GetCurrent (this.Page); scriptManager.RegisterPostBackControl (e.Item.FindControl ("addToCartButton")); } 작동하지 않았습니다. 같은 오류. –

+0

안녕하세요. 나는 이것을 시도했다 : 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 = 새 PostBackTrigger(); trigger.ControlID = control.ID; updPnlProductsList.Triggers.Add (trigger); } } 하지만 같은 오류로 인해 실패했습니다. –