对象数据源选择方法在代码调用中不返回任何内容

发布于 2024-09-24 23:52:27 字数 1652 浏览 1 评论 0原文

我有一个 ObjectDataSource,配置了正确的 SelectMethodSelectParameters。数据源绑定到网格视图,该视图在页面加载时成功显示数据。

我需要的是能够重新运行由 ObjectDataSource 定义的 Select 方法来存储在变量中并操作其中的项目。我一直遇到的问题是,调用 .Select() 方法总是返回 0 行,尽管它正确填充了网格视图。

我无法在对象数据源上手动重新运行 Select() 方法是否有原因?

更新 2:

以下是我设置 ObjectDataSource 的方法:

myObjectDataSource.TypeName = typeof(MyDataAccessObject).ToString();
myObjectDataSource.SelectMethod = "GetBy" + stringVariable;
myObjectDataSource.SelectCountMethod = "GetCountBy" + stringVariable;
myObjectDataSource.EnablePaging = true;

更新 1:

我在链接按钮的 OnClick 事件上运行 Select()

protected void LinkButton1_Click(object sender, EventArgs e)
{
    SetupDataSource(); // populates the objSource's SelectMethod, SelectParameters and TypeName etc.
    var items = objSource.Select();
    int count = items.Count(); // returns 0;
}

ObjectDataSource在 Page_Load 事件中进行设置(设置 SelectMethod 和 SelectParameters)。

ObjectDataSource 定义:

<asp:ObjectDataSource ID="objSource" runat="server" EnablePaging="True" SortParameterName="sortExpression" ></asp:ObjectDataSource>

GridView 定义:

        <asp:GridView 
        ID="myGridView" 
        runat="server" 
        DataSourceID="objSource"
        AllowPaging="true"
        ShowHeader="true" 
        AutoGenerateColumns="false"
        AllowSorting="true" 
        Width="100%" >

I have an ObjectDataSource with the proper SelectMethod and SelectParameters configured. The data source is bound to a grid view which successfully displays the data on page load.

What I need is the ability to rerun the Select method defined by the ObjectDataSource to be stored in a variable and manipulate the items in it. The issue I keep encountering is that calling the .Select() method always returns 0 rows despite it populating the grid view properly.

Is there a reason I can't manually rerun the Select() method on the object data source?

Update 2:

Here is how I setup the ObjectDataSource:

myObjectDataSource.TypeName = typeof(MyDataAccessObject).ToString();
myObjectDataSource.SelectMethod = "GetBy" + stringVariable;
myObjectDataSource.SelectCountMethod = "GetCountBy" + stringVariable;
myObjectDataSource.EnablePaging = true;

Update 1:

I run the Select() on a link button's OnClick event:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    SetupDataSource(); // populates the objSource's SelectMethod, SelectParameters and TypeName etc.
    var items = objSource.Select();
    int count = items.Count(); // returns 0;
}

The ObjectDataSource is setup (SelectMethod and SelectParameters are set) in the Page_Load event.

ObjectDataSource definition:

<asp:ObjectDataSource ID="objSource" runat="server" EnablePaging="True" SortParameterName="sortExpression" ></asp:ObjectDataSource>

GridView definition:

        <asp:GridView 
        ID="myGridView" 
        runat="server" 
        DataSourceID="objSource"
        AllowPaging="true"
        ShowHeader="true" 
        AutoGenerateColumns="false"
        AllowSorting="true" 
        Width="100%" >

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

行雁书 2024-10-01 23:52:27

事实证明,底层数据访问对象的方法考虑了分页,并返回始终为 0 的 .Take(maximumRows)

作为解决方法,我通过 myObjectDataSource.EnablePaging = false; 以编程方式禁用了分页。 然后创建了一个不考虑分页的新函数(需要一个新函数,因为 ObjectDataSource 正在寻找具有特定参数的函数)。

Turns out that underlying data access object's method took pagination into account and was returning .Take(maximumRows) which was always 0.

As a workaround, I programmatically disabled pagination via myObjectDataSource.EnablePaging = false; then created a new function which does not take into account pagination (a new function was required because the ObjectDataSource was looking for a function with specific paremeters).

絕版丫頭 2024-10-01 23:52:27

您没有提及何时(在什么情况下您尝试将 重新绑定到数据源)
简短的代码片段可能会有所帮助。

如果您处于回发状态,则 Gridview 不会在回发时重新绑定,它们的行将从视图状态中拉回。在页面加载(或初始化?)时将 gridview 的 DatasourceID 重置为对象数据源 ID 将导致 gridview 反弹。

You don't mention when (in what event you are trying to rebind the to the DataSource)
Short code snippet may help.

If you are in Postback then Gridviews are not re-bound on postback, their rows are pulled back from viewstate. Resetting the gridview's DatasourceID to the object data source ID on page load (or init?) will cause the gridview to be rebound.

烈酒灼喉 2024-10-01 23:52:27

我以前也遇到过类似的问题。我认为 .Select() 是使用 DataReader 实现的,一旦读取器为空就调用 Select,因此任何后续调用 .Select 或 .Count() 将返回空结果。

因此,您可以做的是使用 .ToList() 将结果存储在列表中,然后继续重用该列表。

I had similar problem before. I think .Select() is implemented using DataReader and once Select has been called once the reader is empty so any subsequent call to .Select or .Count() will return empty result.

Therefore, what you can do is to use .ToList() to store the result in a list and then just keep reusing the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文