System.InvalidOperationException 执行 OnRowCommand 后未给出名称 (ASP.NET)
System.InvalidOperationException with no name given after executing an OnRowCommand (ASP.NET)
因此,在我使用 GridView 上的 onRowCommand 从数据库中删除项目后,我得到了 System.InvalidOperationException,下面的错误。事情是它完美地工作,它删除了项目但是在 onRowCommand 中指定的方法完成后它给了我错误,这是没有意义的。已经看过 this and this
错误:未找到名称为“”的 public 方法,或者类型 'ASP.admin_overview_aspx'
中有多个同名方法
堆栈跟踪:
[InvalidOperationException: A public method with the name '' was either not found or there were multiple methods with the same name on the type 'ASP.admin_overview_aspx'.]
System.Web.UI.WebControls.ModelDataSourceView.FindMethod(String methodName) +2439378
System.Web.UI.WebControls.ModelDataSourceView.RequireAsyncModelBinding(String methodName, ModelDataSourceMethod& method) +67
System.Web.UI.WebControls.ModelDataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +86
System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +930
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1183
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.Button.OnCommand(CommandEventArgs e) +114
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +260
System.Web.UI.WebControls.Button.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) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
这是网格视图:
<h4>General Policies </h4>
<asp:GridView runat="server" ID="GeneralGrid"
ItemType="Source.GeneralPolicy" DataKeyNames="id"
SelectMethod="GPolicyGrid_Get"
AutoGenerateColumns="false" OnRowCommand="GeneralGrid_RowCommand">
<Columns>
<asp:DynamicField DataField="id" />
<asp:DynamicField DataField="name" />
<asp:DynamicField DataField="appliedGroup" />
<asp:DynamicField DataField="author" />
<asp:DynamicField DataField="createdOn" />
<asp:DynamicField DataField="modifiedOn" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="ButtonEdit" runat="server" CommandName="EDIT" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Edit" />
<asp:Button ID="ButtonDelete" runat="server" CommandName="DELETE" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是隐藏代码:
protected void passwordGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView gv = (GridView)sender;
int index = Convert.ToInt32(e.CommandArgument.ToString());
int id = Convert.ToInt32(gv.DataKeys[index].Value);
if (e.CommandName == "EDIT")
{
Response.Redirect("~/Admin,false);
}
else if (e.CommandName == "DELETE")
{
using (NubeSSPRContext db = new NubeSSPRContext())
{
var ppolicy = db.PasswordPolicies.Find(id);
db.Entry(ppolicy).State = EntityState.Deleted;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
ModelState.AddModelError("",
String.Format("Item with id {0} no longer exists in the database.", id));
}
}
}
知道来源吗?我有四个表,我可以使用不同的 onRowCommand 方法从中删除,并且所有这些表 return 删除后出现相同的错误。
提前致谢
好的,所以我找到了问题的根源。我会留下答案以防万一有人遇到同样的问题。这似乎是 OnRowCommand="GeneralGrid_RowCommand" 和 DELETE 命令的问题。所以我找到了另一种更简单的方法来执行编辑和删除命令。基本上像这样执行 updateMethod 和 deleteMethod 更容易:
public void AnswerGrid_DeleteItem(int id)
{
using (NubeSSPRContext db = new NubeSSPRContext())
{
var policy = db.AnswerPolicies.Find(id);
db.Entry(policy).State = EntityState.Deleted;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
ModelState.AddModelError("",
String.Format("Item with id {0} no longer exists in the database.", id));
}
}
}
// The id parameter name should match the DataKeyNames value set on the control
public void GeneralGrid_UpdateItem(int id)
{
Response.Redirect("~/Admin/GeneralSettings?GeneralPolicyID=" + id.ToString(), false);
}
然后使用 CommandName="Update"、"Delete" 更新网格的按钮。我没有找到错误的原因,但至少可以解决。
因此,在我使用 GridView 上的 onRowCommand 从数据库中删除项目后,我得到了 System.InvalidOperationException,下面的错误。事情是它完美地工作,它删除了项目但是在 onRowCommand 中指定的方法完成后它给了我错误,这是没有意义的。已经看过 this and this
错误:未找到名称为“”的 public 方法,或者类型 'ASP.admin_overview_aspx'
中有多个同名方法堆栈跟踪:
[InvalidOperationException: A public method with the name '' was either not found or there were multiple methods with the same name on the type 'ASP.admin_overview_aspx'.]
System.Web.UI.WebControls.ModelDataSourceView.FindMethod(String methodName) +2439378
System.Web.UI.WebControls.ModelDataSourceView.RequireAsyncModelBinding(String methodName, ModelDataSourceMethod& method) +67
System.Web.UI.WebControls.ModelDataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +86
System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +930
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1183
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.Button.OnCommand(CommandEventArgs e) +114
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +260
System.Web.UI.WebControls.Button.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) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
这是网格视图:
<h4>General Policies </h4>
<asp:GridView runat="server" ID="GeneralGrid"
ItemType="Source.GeneralPolicy" DataKeyNames="id"
SelectMethod="GPolicyGrid_Get"
AutoGenerateColumns="false" OnRowCommand="GeneralGrid_RowCommand">
<Columns>
<asp:DynamicField DataField="id" />
<asp:DynamicField DataField="name" />
<asp:DynamicField DataField="appliedGroup" />
<asp:DynamicField DataField="author" />
<asp:DynamicField DataField="createdOn" />
<asp:DynamicField DataField="modifiedOn" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="ButtonEdit" runat="server" CommandName="EDIT" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Edit" />
<asp:Button ID="ButtonDelete" runat="server" CommandName="DELETE" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是隐藏代码:
protected void passwordGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView gv = (GridView)sender;
int index = Convert.ToInt32(e.CommandArgument.ToString());
int id = Convert.ToInt32(gv.DataKeys[index].Value);
if (e.CommandName == "EDIT")
{
Response.Redirect("~/Admin,false);
}
else if (e.CommandName == "DELETE")
{
using (NubeSSPRContext db = new NubeSSPRContext())
{
var ppolicy = db.PasswordPolicies.Find(id);
db.Entry(ppolicy).State = EntityState.Deleted;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
ModelState.AddModelError("",
String.Format("Item with id {0} no longer exists in the database.", id));
}
}
}
知道来源吗?我有四个表,我可以使用不同的 onRowCommand 方法从中删除,并且所有这些表 return 删除后出现相同的错误。
提前致谢
好的,所以我找到了问题的根源。我会留下答案以防万一有人遇到同样的问题。这似乎是 OnRowCommand="GeneralGrid_RowCommand" 和 DELETE 命令的问题。所以我找到了另一种更简单的方法来执行编辑和删除命令。基本上像这样执行 updateMethod 和 deleteMethod 更容易:
public void AnswerGrid_DeleteItem(int id)
{
using (NubeSSPRContext db = new NubeSSPRContext())
{
var policy = db.AnswerPolicies.Find(id);
db.Entry(policy).State = EntityState.Deleted;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
ModelState.AddModelError("",
String.Format("Item with id {0} no longer exists in the database.", id));
}
}
}
// The id parameter name should match the DataKeyNames value set on the control
public void GeneralGrid_UpdateItem(int id)
{
Response.Redirect("~/Admin/GeneralSettings?GeneralPolicyID=" + id.ToString(), false);
}
然后使用 CommandName="Update"、"Delete" 更新网格的按钮。我没有找到错误的原因,但至少可以解决。