【问题标题】:A public method with the name '' was either not found找不到名为“”的公共方法
【发布时间】:2017-03-07 07:55:54
【问题描述】:

我有一个带有操作按钮的数据绑定网格视图,用于更新和删除用户购物车中的项目。网格视图在后面的代码中使用“SelectMethod”来生成数据。我遇到的问题是,每次我在网格视图中编辑一些数据时,数据都会成功持久化,但是在页面完成加载之前就会抛出错误。

以下是异常详情:

System.InvalidOperationException

找不到名为“”的公共方法,或者“ASP.posworx_cart_aspx”类型上有多个同名方法。

更令人困惑的是 (' ') 是一个空字符串,我检查了我的代码,我什至没有任何具有空字符串值的属性,除了 对于项目模板的标题,我认为这并不重要。

这是堆栈跟踪:

[InvalidOperationException:找不到名为“”的公共方法,或者在“ASP.posworx_cart_aspx”类型上有多个同名的方法。] System.Web.UI.WebControls.ModelDataSourceView.FindMethod(String methodName) +2464454 System.Web.UI.WebControls.ModelDataSourceView.RequireAsyncModelBinding(String methodName, ModelDataSourceMethod& 方法) +67 System.Web.UI.WebControls.ModelDataSourceView.Update(IDictionary 键、IDictionary 值、IDictionary oldValues、DataSourceViewOperationCallback 回调)+97 System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean CauseValidation) +1210 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean CauseValidation, String validationGroup) +877 System.Web.UI.WebControls.GridView.OnBubbleEvent(对象源,EventArgs e)+89 System.Web.UI.Control.RaiseBubbleEvent(对象源,EventArgs args)+37 System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(对象源,EventArgs e)+90 System.Web.UI.Control.RaiseBubbleEvent(对象源,EventArgs args)+37 System.Web.UI.WebControls.LinkBut​​ton.OnCommand(CommandEventArgs e) +121 System.Web.UI.WebControls.LinkBut​​ton.RaisePostBackEvent(字符串 eventArgument)+161 System.Web.UI.WebControls.LinkBut​​ton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 eventArgument)+12 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9754214 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3562

我的网格视图的标记代码:

<div class="table-responsive unbordered">
  <asp:GridView ID="shoppingCartGridView" runat="server" DataKeyNames="ItemDetailId" SelectMethod="GetShoppingCartItems" AutoGenerateColumns="false" BorderColor="Transparent" BackColor="White" ForeColor="Black" CssClass="table table-hover table-condensed"
    OnRowUpdating="shoppingCartGridView_RowUpdating" OnRowDeleting="shoppingCartGridView_RowDeleting" ItemType="OnlineShoppingApplication.BusinessEntities.ShoppingCart.ShoppingCartItem">
    <SelectedRowStyle BackColor="#CC3333" ForeColor="White"></SelectedRowStyle>
    <SortedAscendingCellStyle BackColor="#FFF"></SortedAscendingCellStyle>
    <SortedAscendingHeaderStyle BackColor="#333333"></SortedAscendingHeaderStyle>
    <SortedDescendingCellStyle BackColor="#FFF"></SortedDescendingCellStyle>
    <SortedDescendingHeaderStyle BackColor="#333333"></SortedDescendingHeaderStyle>

    <Columns>
      <asp:TemplateField HeaderText="Product" HeaderStyle-Width="50%">
        <ItemTemplate>
          <div class="row">
            <div class="col-sm-2 hidden-xs"><img class="img-responsive" src="../Images/product-image holder.gif" alt="<%#:Item.ItemDescription%>"></div>
            <div class="col-sm-10">
              <h4 class="bold-text nomargin">
                <%#:Item.ItemDescription %>
              </h4>
              <p data-th="Product">
                <%#:Item.ItemDescription %>
              </p>
            </div>
          </div>
        </ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="ItemDetailId" HeaderText="Id" SortExpression="ItemDetailId" Visible="false" />
      <%--<asp:TemplateField HeaderText="Name">
                <ItemTemplate>
               <a class="btn-link bold-text" href="<%#: GetRouteUrl("ProductByIdRoute", new {productId = Item.ItemId,productDetailId = Item.ItemDetailId})%>">
        <%#:Item.ItemDescription%>
          </a>
          </ItemTemplate>
          </asp:TemplateField>--%>
          <asp:BoundField DataField="ItemPrice" ItemStyle-CssClass="btn-link item-vertical-align" HeaderText="Price" HeaderStyle-Width="10%" DataFormatString="{0:c}" />
          <asp:TemplateField HeaderText="Quantity" HeaderStyle-Width="8%">
            <ItemTemplate>
              <asp:TextBox ID="quantityTextBox" runat="server" CssClass="form-control text-center" Text="<%#:Item.Quantity %>" TextMode="Number"></asp:TextBox>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Subtotal" HeaderStyle-Width="22%" HeaderStyle-CssClass="text-center" ItemStyle-CssClass="btn-link text-center">
            <ItemTemplate>
              <%#:$"{(Convert.ToDouble(Item.Quantity)) * (Convert.ToDouble(Item.ItemPrice)):c}"%>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="" HeaderStyle-Width="10%">
            <ItemTemplate>
              <asp:LinkButton ID="updateCartItemButton" runat="server" CssClass="btn btn-info btn-sm" Text='<i class="fa fa-refresh"></i>' alt="Update item" CommandName="Update">
              </asp:LinkButton>

              <asp:LinkButton ID="deleteCartItemButton" runat="server" CssClass="btn btn-danger btn-sm" Text='<i class="fa fa-trash-o"></i>' alt="Delete item" CommandName="Delete">
              </asp:LinkButton>
            </ItemTemplate>
          </asp:TemplateField>
    </Columns>

    <EmptyDataTemplate>
      <div class="row alert alert-warning alert-dismissable fade in" style="margin-bottom:0px">
        There are no items in your shopping cart
      </div>
    </EmptyDataTemplate>

    <EmptyDataTemplate>
      <div class="row alert alert-warning alert-dismissable fade in" style="margin-bottom:0px">
        There are no items in your shopping cart
      </div>
    </EmptyDataTemplate>
  </asp:GridView>
</div>
</div>

选择网格视图的方法:

public IQueryable<BusinessEntities.ShoppingCart.ShoppingCartItem> GetShoppingCartItems()
{
    IQueryable<BusinessEntities.ShoppingCart.ShoppingCartItem> shoppingCartItems = _cartService.GetCartItems(SiteMaster.Customer.CustomerGuid).AsQueryable();
    if (shoppingCartItems != null)
    {
        CreateCartSummary();    
    }
    return shoppingCartItems;
}

终于来了

shoppingCartGridView_RowUpdating事件方法:

protected void shoppingCartGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    try
    {
        int prouductDetailId = (int)shoppingCartGridView.DataKeys[e.RowIndex].Value;
        TextBox quantityTextBox = (TextBox)shoppingCartGridView.Rows[e.RowIndex].FindControl("quantityTextBox");
        int newQuantity = int.Parse(quantityTextBox.Text);
        UpdateCartItem(prouductDetailId, newQuantity);
        DisplayMessage("Your cart has been updated successfully", Bootstrap.MessageType.Success);
        shoppingCartGridView.DataBind();
    }
    catch (Exception ex)
    {
        ExceptionUtility.LogException(ex, $"{this}");
    }
}

还有

shoppingCartGridView_RowDeleting事件方法:

protected void shoppingCartGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
    {
        int productDetailId = (int)shoppingCartGridView.DataKeys[e.RowIndex].Value;
        RemoveCartItem(productDetailId);
        DisplayMessage("The item has been deleted from your cart", Bootstrap.MessageType.Success);
        shoppingCartGridView.DataBind();
    }
    catch(Exception ex)
    {
        ExceptionUtility.LogException(ex, $"{this}");
        DisplayMessage(@"An error occured while deleting the item from the cart please try again later", Bootstrap.MessageType.Danger);
    }
}

在此先感谢各位,我将非常感谢您的帮助 关于这个,因为我真的不知道该怎么办。

【问题讨论】:

标签: c# asp.net .net gridview


【解决方案1】:

在对ModelDataSourceView类做了一些研究之后,我发现这个类的属性叫做UpdateMethod,DeleteMethod,InsertMethod基本上是属性来指定所有CRUD操作的名称)。

这让我想到,与其在更新行时触发 OnRowUpdatingOnRowDeleting 事件,不如直接在 GridView 中添加一个 UpdateMethod 属性(具有该属性的值是负责执行更新操作的方法的名称)?毕竟已经有一个SelectMethod 属性,所以让UpdateMethodDeleteMethod 属性与其一起使用而不是触发OnRowUpdatingOnRowDeleting 事件更有意义。

啊啊啊啊啊啊啊!!!!!!它运行良好。

所以我的网格视图现在看起来像这样(我所做的只是删除了OnRowUpdating 属性并添加了UpdateMethod 属性,当然,我用DeleteMethod 替换了OnRowDeleting):

<asp:GridView
            ID="ShoppingCartGridView"
            runat="server"
            DataKeyNames="ItemDetailId"
            SelectMethod="GetShoppingCartItems"
            UpdateMethod="ShoppingCartGridView_UpdateItem"
            DeleteMethod="ShoppingCartGridView_DeleteItem"
            AutoGenerateColumns="false"
            BorderColor="Transparent"
            BackColor="White"
            ForeColor="Black"
            CssClass="table table-hover table-condensed"
            OnDataBound="ShoppingCartGridView_DataBound"
            ItemType="OnlineShoppingApplication.BusinessEntities.ShoppingCart.ShoppingCartItem">
            <SelectedRowStyle BackColor="#CC3333" ForeColor="White"></SelectedRowStyle>
            <SortedAscendingCellStyle BackColor="#FFF"></SortedAscendingCellStyle>
            <SortedAscendingHeaderStyle BackColor="#333333"></SortedAscendingHeaderStyle>
            <SortedDescendingCellStyle BackColor="#FFF"></SortedDescendingCellStyle>
            <SortedDescendingHeaderStyle BackColor="#333333"></SortedDescendingHeaderStyle>

            <Columns>
                <asp:TemplateField HeaderText="Product" HeaderStyle-Width="50%">
                    <ItemTemplate>
                        <div class="row">
                            <div class="col-sm-2 hidden-sm hidden-xs">
                                <img class="img-responsive" src="../Images/product-image holder.gif" alt="<%#:Item.ItemDescription%>"></div>
                            <div class="col-sm-10">
                                <h4 class="bold-text nomargin"><%#:Item.ItemDescription %></h4>
                                <p data-th="Product">
                                    <a
                                        href="<%#:GetRouteUrl("ProductByIdRoute", new {productId = Item.ItemId,productDetailId = Item.ItemDetailId})%>"
                                        class="btn-link"><%#:$"{Item.SizeCurve} {Item.Colour} {Item.ItemDescription}"%>
                                    </a>
                                </p>
                            </div>
                        </div>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="ItemDetailId" HeaderText="Id" SortExpression="ItemDetailId" Visible="false" />
               <asp:BoundField DataField="ItemPrice"  ItemStyle-CssClass="currency item-vertical-align" HeaderText="Price" HeaderStyle-Width="10%" DataFormatString="{0:F2}"/>
                <asp:TemplateField HeaderText="Quantity" HeaderStyle-Width="8%">
                    <ItemTemplate>
                        <asp:TextBox ID="quantityTextBox" runat="server" CssClass="form-control text-center" Text="<%#:Convert.ToInt32(Item.Quantity) %>" TextMode="Number"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Subtotal" HeaderStyle-Width="22%" HeaderStyle-CssClass="text-center" ItemStyle-CssClass="text-center">
                    <ItemTemplate>
                        <%#:$"{Convert.ToInt32((Convert.ToDouble(Item.Quantity)) * (Convert.ToDouble(Item.ItemPrice)))}"%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText=" " HeaderStyle-Width="10%" ItemStyle-CssClass="action">
                    <ItemTemplate>
                        <asp:LinkButton
                            ID="updateCartItemButton"
                            runat="server"
                            CssClass="btn btn-info btn-sm"
                            Text='<i class="fa fa-refresh"></i>'
                            alt="Update item"
                            CommandName="Update">
               </asp:LinkButton>

                        <asp:LinkButton
                            ID="deleteCartItemButton"
                            runat="server"
                            CssClass="btn btn-danger btn-sm"
                            Text='<i class="fa fa-trash-o"></i>'
                            alt="Delete item"
                            CommandName="Delete">
                        </asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

            <EmptyDataTemplate>
                <div class="row alert alert-warning alert-dismissable fade in" style="margin-bottom: 0px">
                    There are no items in your shopping cart
                </div>
            </EmptyDataTemplate>

            <EmptyDataTemplate>
                <div class="row alert alert-warning alert-dismissable fade in" style="margin-bottom: 0px">
                    There are no items in your shopping cart
                </div>
            </EmptyDataTemplate>
        </asp:GridView>

这是我在网格视图中引用的新的但没有太大不同的 UpdateMethod:

public void shoppingCartGridView_UpdateItem(int ItemDetailId)
   {
        try
        {
            int rowIndex = GetRowIndexByItemDetailId(ItemDetailId);
            TextBox quantityTextBox = (TextBox)ShoppingCartGridView.Rows[rowIndex].FindControl("quantityTextBox");
            int newQuantity = int.Parse(quantityTextBox.Text);
            UpdateCartItem(ItemDetailId, newQuantity);
            DisplayMessage("Your cart has been updated successfully", Bootstrap.MessageType.Success);
            ShoppingCartGridView.DataBind();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, $"{this}");
            DisplayMessage(@"An error occured while updating your cart item, 
                             please try again in a moment", Bootstrap.MessageType.Danger);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 2013-05-15
    相关资源
    最近更新 更多