我有一个带有操作按钮的数据绑定网格视图,用于更新和删除用户购物车中的项目。网格视图在后面的代码中使用“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& method)+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(Object source,EventArgs e)+89 System.Web.UI.Control.RaiseBubbleEvent(Object source,EventArgs args)+37 System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source,EventArgs e)+90 System.Web.UI.Control.RaiseBubbleEvent(Object source,EventArgs args)+37 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)+121 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)+161 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String 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);
}
}
先谢谢你们,我非常感谢你的帮助 对此,因为我真的不知道该怎么做。
答案 0 :(得分:0)
在对ModelDataSourceView
类进行一些研究后,我发现该类的属性名为UpdateMethod
,DeleteMethod
,InsertMethod
(基本属性为指定所有CRUD操作的名称)。
然后我认为在更新行时不会触发OnRowUpdating
和OnRowDeleting
事件,为什么不在GridView中添加UpdateMethod
属性(该属性的值是负责执行更新操作的方法的名称)?毕竟,已经存在SelectMethod
属性,因此让UpdateMethod
和DeleteMethod
属性与OnRowUpdating
和{OnRowDeleting
相比更有意义。 1}}被解雇的事件。
Aaaaand Walaaa !!!!!它工作得很好。
所以我的网格视图现在看起来像这样(我完成的所有工作都删除了OnRowUpdating
属性并添加了UpdateMethod
属性,当然,我将OnRowDeleting
替换为DeleteMethod
}):
<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);
}
}