ObjectDataSource SelectCountMethod 未执行

发布于 2024-12-13 21:14:32 字数 2769 浏览 2 评论 0原文

这是关于 myIdea-project-during-worktime 项目。我正在使用 ObjectDataSource 来填充 GridView。由于某种原因,“页面”部分(应该位于网格的底部)没有出现。我在这里查看了其他类似问题的答案,但无法弄清楚我的代码出了什么问题。

GridView 看起来像这样:

<asp:GridView ID="GridView1" 
            runat="server" 
            AllowPaging="True"
            PageSize="10" 
            AutoGenerateColumns="False" 
            DataKeyNames="rowid"
            DataSourceID="PopulateGridView">
 ........
 </asp:GridView>

GridView 使用 ObjectDataSource“PopulateGridView”。 “PopulateGridView”看起来像这样:

<asp:ObjectDataSource 
ID="PopulateGridView" 
runat="server"
TypeName="SurveyItemInfo"
SelectMethod="GetPagedData"
EnablePaging="True"
SelectCountMethod="GetRowCount"
StartRowIndexParameterName="startRow"
MaximumRowsParameterName="pageSize"
SortParameterName="sortColumns"
OnObjectCreated="SurveyItemInfo_ObjectCreated">
</asp:ObjectDataSource>

使用“GetPagedData”和“GetRowCount”等方法创建类“SurveyItemInfo”。 GetPagedData 看起来像这样:

 public DataTable GetPagedData(string sortColumns, int startRow, int pageSize)
    {
        // datatable
        DataTable dt = new DataTable();
        string sql;

        // Create connection
        SqlConnection con = new SqlConnection(_connectionString);

        // Create command
        sql = "";
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "dbo.epm_ListSurveyItemForReportPaged";
        cmd.Parameters.AddWithValue("@reportid", reportid);
        cmd.Parameters.AddWithValue("@startRow", startRow);
        //cmd.Parameters.AddWithValue("@pageSize", pageSize);
        cmd.Parameters.AddWithValue("@pageSize", 50);

        // Execute command
        using (con)
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            dt.Load(reader);
            //reader.Close();
        }
        return dt;
    }

我正在使用此方法中的存储过程将分页数据获取到网格。请注意,我已经注释掉了存储过程获取页面大小(行数)的第三个参数的行。我在这里使用硬编码值来测试目的。

调试时,我发现方法 GetPagedData 的参数 startRow 和 pageSize 的值为零。我也不确定,但 ObjectdataSource 的 SelectCountMethod (GetRowCount) 方法不应该执行一次吗?它不是。

此方法具有以下代码:

public int GetRowCount()
{
    using (SqlConnection conn = new SqlConnection(_connectionString))
    {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("SELECT COUNT(1) FROM dbo.data_SurveyItem where reportid=" + reportid.ToString(), conn))
        {
            return (int)cmd.ExecuteScalar();
        }
    }
}

因此问题是用于浏览不同页面的数字页面链接不会出现在 GridView 的底部。对于 GridView 和 ObjectDataSource,都启用了分页和排序(尽管我没有使用排序)。我不太确定我还缺少什么。 感谢您的帮助。

This is about myIdea-project-during-worktime project. I am using an ObjectDataSource to populate a GridView. For some reason the "page" part (that is supposed to come at the bottom of the of the grid) is not coming up. I have looked at the answers for other similar question here but haven't been able to figure out what is going wrong with my code.

The GridView looks like this:

<asp:GridView ID="GridView1" 
            runat="server" 
            AllowPaging="True"
            PageSize="10" 
            AutoGenerateColumns="False" 
            DataKeyNames="rowid"
            DataSourceID="PopulateGridView">
 ........
 </asp:GridView>

The GridView is using ObjectDataSource "PopulateGridView". "PopulateGridView" looks like this:

<asp:ObjectDataSource 
ID="PopulateGridView" 
runat="server"
TypeName="SurveyItemInfo"
SelectMethod="GetPagedData"
EnablePaging="True"
SelectCountMethod="GetRowCount"
StartRowIndexParameterName="startRow"
MaximumRowsParameterName="pageSize"
SortParameterName="sortColumns"
OnObjectCreated="SurveyItemInfo_ObjectCreated">
</asp:ObjectDataSource>

A class "SurveyItemInfo" is created with methods like "GetPagedData" and "GetRowCount".
GetPagedData looks like this:

 public DataTable GetPagedData(string sortColumns, int startRow, int pageSize)
    {
        // datatable
        DataTable dt = new DataTable();
        string sql;

        // Create connection
        SqlConnection con = new SqlConnection(_connectionString);

        // Create command
        sql = "";
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "dbo.epm_ListSurveyItemForReportPaged";
        cmd.Parameters.AddWithValue("@reportid", reportid);
        cmd.Parameters.AddWithValue("@startRow", startRow);
        //cmd.Parameters.AddWithValue("@pageSize", pageSize);
        cmd.Parameters.AddWithValue("@pageSize", 50);

        // Execute command
        using (con)
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            dt.Load(reader);
            //reader.Close();
        }
        return dt;
    }

I am using a sproc from this method to get the paged data to grid. Note that I have commented out the line where the sproc is getting its 3rd parameter for page size (number of rows). I am using a hard-coded value here to test purpose.

When debugging through I have found that the values for parameter startRow and pageSize for method GetPagedData are zero. Also I am not sure about it but isn't the method for ObjectdataSource's SelectCountMethod (GetRowCount) supposed to get executed once? It is not.

This method has the following code:

public int GetRowCount()
{
    using (SqlConnection conn = new SqlConnection(_connectionString))
    {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("SELECT COUNT(1) FROM dbo.data_SurveyItem where reportid=" + reportid.ToString(), conn))
        {
            return (int)cmd.ExecuteScalar();
        }
    }
}

So the issue is numeric page links to browse to different pages is not coming up at the bottom of the GridView. For both GridView and ObjectDataSource paging and sorting are enabled (although I am not using sorting). I am not exactly sure what else I am missing.
Thanks for your help.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文