ASP.Net在分页gridView时抛出此错误。它可以很好地加载第一块元素,但是在点击任何页码时都会失败。我有一个类似的页面实现ViewAllEvents没有参数,运行正常。所以我想这与添加部分代码的参数有关。
ObjectDataSource 'pbpDataSource' could not find a non-generic method 'FindEvents' that has parameters: keywords, keywords1, count, start."} System.Exception {System.InvalidOperationException}
网页代码:
<%@ Page Language="C#" MasterPageFile="~/Sportacus.Master" AutoEventWireup="true"
Codebehind="ViewEvents.aspx.cs" Inherits="Es.Udc.DotNet.Sportacus.Web.Pages.Event.ViewEvents"
meta:resourcekey="Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_MenuExplanation"
runat="server">
-
<asp:Localize ID="lclMenuExplanation" runat="server" meta:resourcekey="lclMenuExplanation" />
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder_BodyContent"
runat="server">
<form id="form1" runat="server">
<asp:GridView ID="gvEvents" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="False" onpageindexchanging="gvEvents_PageIndexChanging" ShowHeaderWhenEmpty="true">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="Id" HeaderText="<%$ Resources:, hdrEventName %>" DataNavigateUrlFormatString="SingleEvent.aspx?evnId={0}" DataTextField="Name" />
<asp:BoundField DataField="Category.catName" HeaderText ="<%$ Resources:, hdrCategoryName %>"/>
<asp:BoundField DataField="Date" HeaderText ="<%$ Resources:, hdrEventDate %>"/>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="pbpDataSource" runat="server"></asp:ObjectDataSource>
</form>
</asp:Content>
代码背后:
using System;
using System.Web.Security;
using Es.Udc.DotNet.Sportacus.Web.HTTP.Session;
using Es.Udc.DotNet.ModelUtil.Exceptions;
using Es.Udc.DotNet.Sportacus.Model.UserService.Exceptions;
using System.Collections.Generic;
using Es.Udc.DotNet.Sportacus.Model.EventService;
using Microsoft.Practices.Unity;
using System.Web;
using System.Web.UI.WebControls;
using Es.Udc.DotNet.Sportacus.Web.Properties;
using System.Reflection;
using System.Data;
namespace Es.Udc.DotNet.Sportacus.Web.Pages.Event
{
public partial class ViewEvents : SpecificCulturePage
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
pbpDataSource.ObjectCreating += this.PbpDataSource_ObjectCreating;
pbpDataSource.TypeName =
Settings.Default.ObjectDS_Event_Service;
pbpDataSource.EnablePaging = true;
pbpDataSource.SelectMethod =
Settings.Default.ObjectDS_Event_SelectMethod;
pbpDataSource.SelectCountMethod =
Settings.Default.ObjectDS_Event_SelectCountMethod;
pbpDataSource.StartRowIndexParameterName = Settings.Default.Sportacus_startIndex;
pbpDataSource.MaximumRowsParameterName = Settings.Default.Sportacus_count;
long catId = Convert.ToInt32(Request.Params.Get("catId"));
String keywords = Request.Params.Get("keywords");
pbpDataSource.SelectParameters.Add("keywords", DbType.String, keywords);
if (catId != -1)
{
pbpDataSource.SelectParameters.Add("categoryId", DbType.Int64, catId.ToString());
}
gvEvents.AllowPaging = true;
gvEvents.PageSize = Settings.Default.Sportacus_defaultCount;
gvEvents.DataSource = pbpDataSource;
gvEvents.DataBind();
}
catch (TargetInvocationException)
{
//TODO Etiqueta de error
}
}
protected void gvEvents_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
gvEvents.PageIndex= e.NewPageIndex;
gvEvents.DataBind();
}
protected void PbpDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
/* Get the Service */
IUnityContainer container =
(IUnityContainer)HttpContext.Current.
Application["unityContainer"];
IEventService eventService = new EventService();
eventService = (IEventService)container.BuildUp(eventService.GetType(), eventService, "IEventService");
e.ObjectInstance = (IEventService)eventService;
}
}
}
我没有发布配置文件,但是它们经过三重检查,不应该是问题。
为什么页面要求关键字1?我该如何解决这个问题?
PS。不要担心方法不要求catId参数,方法会被重载,所以它可以使用或不使用该参数。
编辑: 堆栈跟踪
User code did not control System.InvalidOperationException
HResult=-2146233079
Message=ObjectDataSource 'pbpDataSource' could not find a non-generic method 'FindEvents' that has parameters: keywords, keywords1, count, start.
Source=System.Web
StackTrace:
in System.Web.UI.WebControls.ObjectDataSourceView.GetResolvedMethodData(Type type, String methodName, IDictionary allParameters, DataSourceOperation operation)
en System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)
en System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
en System.Web.UI.WebControls.DataBoundControl.PerformSelect()
en System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
en System.Web.UI.WebControls.GridView.DataBind()
en Es.Udc.DotNet.Sportacus.Web.Pages.Event.ViewEvents.Page_Load(Object sender, EventArgs e) en c:\Users\Trigork\Documents\Visual Studio 2013\Projects\Sportacus\Web\Pages\Event\ViewEvents.aspx.cs:line 61
en System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
en System.Web.UI.Control.OnLoad(EventArgs e)
en System.Web.UI.Control.LoadRecursive()
en System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
如果您想知道,第61行是gvEvents.DataBind();
抛出异常之前的Request.Params字符串
"keywords=a&catId=-1&__EVENTTARGET=ctl00%24ContentPlaceHolder_BodyContent%24gvEvents&__EVENTARGUMENT=Page%242&__VIEWSTATE=%2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v1kH35r4CKJ9pINf58%2bwkrHsyc7srY52vIBt2HtEPSwzWAi%2fkIjMHD2HPNVjjw&AspxAutoDetectCookieSupport=1&ASP.NET_SessionId=b5opxek5wofqswxdekptvvcw&.ASPXAUTH=5695CBAE7ED635548F37EC4DABD548CA6ECE2446D099EBCF528026246DF38E1F2F98E54CCA20C5EC6D354D71A78B084A9107C2BFF664D22D1CF0681B67FF825F659B64A325652376284FC680AC8518615F958A39528C61E944B9756B890B8B8E6D266DCDD72781CD31EF555C1EBEB42A530BC74A75DAE14DCDCAF702CDAFD224A16D1BB3CB0B224618B6CC920E6E7502&ALL_HTTP=HTTP_CACHE_CONTROL%3amax-age%3d0%0d%0aHTTP_CONNECTION%3akeep-alive%0d%0aHTTP_CONTENT_LENGTH%3a1224%0d%0aHTTP_CONTENT_TYPE%3aapplication%2fx-www-form-urlencoded%0d%0aHTTP_ACCEPT%3atext%2fhtml%2capplication%2fxhtml%2bxml%2capplication%2fxml%3bq%3d0.9%2cimage%2fwebp%2c*%2f*%3bq%3d0.8%0d%0aHTTP_ACCEPT_ENCODING%3agzip%2c+deflate%0d%0aHTTP_ACCEPT_LANGUAGE%3aes-ES%2ces%3bq%3d0.8%2cen%3bq%3d0.6%2cja%3bq%3d0.4%0d%0aHTTP_COOKIE%3aAspxAutoDetectCookieSupport%3d1%3b+ASP.NET_SessionId%3db5opxek5wofqswxdekptvvcw%3b+.ASPXAUTH%3d5695CBAE7ED635548F37EC4DABD548CA6ECE2446D099EBCF528026246DF38E1F2F98E54CCA20C5EC6D354D71A78B084A9107C2BFF664D22D1CF0681B67FF825F659B64A325652376284FC680AC8518615F958A39528C61E944B9756B890B8B8E6D266DCDD72781CD31EF555C1EBEB42A530BC74A75DAE14DCDCAF702CDAFD224A16D1BB3CB0B224618B6CC920E6E7502%0d%0aHTTP_HOST%3alocalhost%3a3404%0d%0aHTTP_REFERER%3ahttp%3a%2f%2flocalhost%3a3404%2fPages%2fEvent%2fViewEvents.aspx%3fkeywords%3da%26catId%3d-1%0d%0aHTTP_USER_AGENT%3aMozilla%2f5.0+(Windows+NT+6.2%3b+WOW64)+AppleWebKit%2f537.36+(KHTML%2c+like+Gecko)+Chrome%2f42.0.2311.152+Safari%2f537.36%0d%0aHTTP_ORIGIN%3ahttp%3a%2f%2flocalhost%3a3404%0d%0a&ALL_RAW=Cache-Control%3a+max-age%3d0%0d%0aConnection%3a+keep-alive%0d%0aContent-Length%3a+1224%0d%0aContent-Type%3a+application%2fx-www-form-urlencoded%0d%0aAccept%3a+text%2fhtml%2capplication%2fxhtml%2bxml%2capplication%2fxml%3bq%3d0.9%2cimage%2fwebp%2c*%2f*%3bq%3d0.8%0d%0aAccept-Encoding%3a+gzip%2c+deflate%0d%0aAccept-Language%3a+es-ES%2ces%3bq%3d0.8%2cen%3bq%3d0.6%2cja%3bq%3d0.4%0d%0aCookie%3a+AspxAutoDetectCookieSupport%3d1%3b+ASP.NET_SessionId%3db5opxek5wofqswxdekptvvcw%3b+.ASPXAUTH%3d5695CBAE7ED635548F37EC4DABD548CA6ECE2446D099EBCF528026246DF38E1F2F98E54CCA20C5EC6D354D71A78B084A9107C2BFF664D22D1CF0681B67FF825F659B64A325652376284FC680AC8518615F958A39528C61E944B9756B890B8B8E6D266DCDD72781CD31EF555C1EBEB42A530BC74A75DAE14DCDCAF702CDAFD224A16D1BB3CB0B224618B6CC920E6E7502%0d%0aHost%3a+localhost%3a3404%0d%0aReferer%3a+http%3a%2f%2flocalhost%3a3404%2fPages%2fEvent%2fViewEvents.aspx%3fkeywords%3da%26catId%3d-1%0d%0aUser-Agent%3a+Mozilla%2f5.0+(Windows+NT+6.2%3b+WOW64)+AppleWebKit%2f537.36+(KHTML%2c+like+Gecko)+Chrome%2f42.0.2311.152+Safari%2f537.36%0d%0aOrigin%3a+http%3a%2f%2flocalhost%3a3404%0d%0a&APPL_MD_PATH=%2fLM%2fW3SVC%2f8%2fROOT&APPL_PHYSICAL_PATH=C%3a%5cUsers%5cTrigork%5cDocuments%5cVisual+Studio+2013%5cProjects%5cSportacus%5cWeb%5c&AUTH_TYPE=Forms&AUTH_USER=Trigork&AUTH_PASSWORD=&LOGON_USER=Trigork&REMOTE_USER=Trigork&CERT_COOKIE=&CERT_FLAGS=&CERT_ISSUER=&CERT_KEYSIZE=&CERT_SECRETKEYSIZE=&CERT_SERIALNUMBER=&CERT_SERVER_ISSUER=&CERT_SERVER_SUBJECT=&CERT_SUBJECT=&CONTENT_LENGTH=1224&CONTENT_TYPE=application%2fx-www-form-urlencoded&GATEWAY_INTERFACE=CGI%2f1.1&HTTPS=off&HTTPS_KEYSIZE=&HTTPS_SECRETKEYSIZE=&HTTPS_SERVER_ISSUER=&HTTPS_SERVER_SUBJECT=&INSTANCE_ID=8&INSTANCE_META_PATH=%2fLM%2fW3SVC%2f8&LOCAL_ADDR=%3a%3a1&PATH_INFO=%2fPages%2fEvent%2fViewEvents.aspx&PATH_TRANSLATED=C%3a%5cUsers%5cTrigork%5cDocuments%5cVisual+Studio+2013%5cProjects%5cSportacus%5cWeb%5cPages%5cEvent%5cViewEvents.aspx&QUERY_STRING=keywords%3da%26catId%3d-1&REMOTE_ADDR=%3a%3a1&REMOTE_HOST=%3a%3a1&REMOTE_PORT=17999&REQUEST_METHOD=POST&SCRIPT_NAME=%2fPages%2fEvent%2fViewEvents.aspx&SERVER_NAME=localhost&SERVER_PORT=3404&SERVER_PORT_SECURE=0&SERVER_PROTOCOL=HTTP%2f1.1&SERVER_SOFTWARE=Microsoft-IIS%2f8.0&URL=%2fPages%2fEvent%2fViewEvents.aspx&HTTP_CACHE_CONTROL=max-age%3d0&HTTP_CONNECTION=keep-alive&HTTP_CONTENT_LENGTH=1224&HTTP_CONTENT_TYPE=application%2fx-www-form-urlencoded&HTTP_ACCEPT=text%2fhtml%2capplication%2fxhtml%2bxml%2capplication%2fxml%3bq%3d0.9%2cimage%2fwebp%2c*%2f*%3bq%3d0.8&HTTP_ACCEPT_ENCODING=gzip%2c+deflate&HTTP_ACCEPT_LANGUAGE=es-ES%2ces%3bq%3d0.8%2cen%3bq%3d0.6%2cja%3bq%3d0.4&HTTP_COOKIE=AspxAutoDetectCookieSupport%3d1%3b+ASP.NET_SessionId%3db5opxek5wofqswxdekptvvcw%3b+.ASPXAUTH%3d5695CBAE7ED635548F37EC4DABD548CA6ECE2446D099EBCF528026246DF38E1F2F98E54CCA20C5EC6D354D71A78B084A9107C2BFF664D22D1CF0681B67FF825F659B64A325652376284FC680AC8518615F958A39528C61E944B9756B890B8B8E6D266DCDD72781CD31EF555C1EBEB42A530BC74A75DAE14DCDCAF702CDAFD224A16D1BB3CB0B224618B6CC920E6E7502&HTTP_HOST=localhost%3a3404&HTTP_REFERER=http%3a%2f%2flocalhost%3a3404%2fPages%2fEvent%2fViewEvents.aspx%3fkeywords%3da%26catId%3d-1&HTTP_USER_AGENT=Mozilla%2f5.0+(Windows+NT+6.2%3b+WOW64)+AppleWebKit%2f537.36+(KHTML%2c+like+Gecko)+Chrome%2f42.0.2311.152+Safari%2f537.36&HTTP_ORIGIN=http%3a%2f%2flocalhost%3a3404"
根据用户@rism的建议,我试图吞下InvalidOperationException
,只是为了发现gridView
gvEvents
即使有第一个大块也不会出现结果
答案 0 :(得分:2)
您应该编辑Page_Load,以便在回发时加载页面时不重新添加param关键字(比如更改页面时)。
编辑:您在Page_Load上所做的一切都停留在Page(在ViewState中),因此对代码的某些部分使用if(!IsPostBack)是一个好习惯。