分页对象数据源

发布于 2024-08-30 15:12:20 字数 2847 浏览 6 评论 0原文

我的 ASP 页面代码:

  <asp:ObjectDataSource runat="server" ID="odsResults" OnSelecting="odsResults_Selecting" />
    <tr><td>
    <wssawc:SPGridViewPager ID="sgvpPagerTop" runat="server" GridViewId="sgvConversionResults" />
    </td></tr>
    <tr>
        <td colspan="2" class="ms-vb">
            <wssawc:SPGridView 
                runat="server" 
                ID="sgvConversionResults" 
                AutoGenerateColumns="false"
                RowStyle-CssClass=""
                AlternatingRowStyle-CssClass="ms-alternating" 
                />
        </td>
    </tr>

类代码:

public partial class Convert : System.Web.UI.Page

{
   ...
   private  DataTable resultDataSource = new DataTable();
   ...

  protected void Page_Init(object sender, EventArgs e)
    {
        ...

        resultDataSource.Columns.Add("Column1");
        resultDataSource.Columns.Add("Column2");
        resultDataSource.Columns.Add("Column3");
        resultDataSource.Columns.Add("Column4");
        ...
        odsResults.TypeName = GetType().AssemblyQualifiedName;
        odsResults.SelectMethod = "SelectData";
        odsResults.SelectCountMethod = "GetRecordCount";
        odsResults.EnablePaging = true;
        sgvConversionResults.DataSourceID = odsResults.ID;
        ConversionResultsCreateColumns();
        sgvConversionResults.AllowPaging = true;
        ...
    }

    protected void btnBTN_Click(object sender, EventArgs e)
    {
        // add rows into resultDataSource
    }

    public DataTable SelectData(DataTable ds,int startRowIndex,int maximumRows)
    {
        DataTable dt = new DataTable();

        dt.Columns.Add("Column1");
        dt.Columns.Add("Column2");
        dt.Columns.Add("Column3");
        dt.Columns.Add("Column4");

        for (int i =startRowIndex; i<startRowIndex+10 ;i++)
        {   
            if (i<ds.Rows.Count)
            {
                dt.Rows.Add(ds.Rows[i][0].ToString(), ds.Rows[i][1].ToString(),
                ds.Rows[i][2].ToString(), ds.Rows[i][3].ToString());
            }
        }
        return dt;
    }

    public int GetRecordCount(DataTable ds)
    {
        return ds.Rows.Count;
    }

    protected void odsResults_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.InputParameters["ds"] = resultDataSource;

    }

}

单击按钮 resultDataSource 会收到一些行。页面重新加载,我们可以在 sgvConversionResults 中看到结果。前 10 行。但是,在单击寻呼机中的下一页后,我们会收到消息“此视图中没有可显示的项目”。当我尝试调试时,我发现在 postBack 页面(单击下一页)之后输入参数“ds”为空,ds.Rows.Count = 0 等等...就好像 resultDataSource 变成空了(( 我做错了什么?


onPostBack 所有变量都获取默认值,sgvConversionResults 保存她的结构但具有清晰的行。如何保存 PostBack 事件中的 sgvConversionResults 数据???

My ASP page code:

  <asp:ObjectDataSource runat="server" ID="odsResults" OnSelecting="odsResults_Selecting" />
    <tr><td>
    <wssawc:SPGridViewPager ID="sgvpPagerTop" runat="server" GridViewId="sgvConversionResults" />
    </td></tr>
    <tr>
        <td colspan="2" class="ms-vb">
            <wssawc:SPGridView 
                runat="server" 
                ID="sgvConversionResults" 
                AutoGenerateColumns="false"
                RowStyle-CssClass=""
                AlternatingRowStyle-CssClass="ms-alternating" 
                />
        </td>
    </tr>

Class code:

public partial class Convert : System.Web.UI.Page

{
   ...
   private  DataTable resultDataSource = new DataTable();
   ...

  protected void Page_Init(object sender, EventArgs e)
    {
        ...

        resultDataSource.Columns.Add("Column1");
        resultDataSource.Columns.Add("Column2");
        resultDataSource.Columns.Add("Column3");
        resultDataSource.Columns.Add("Column4");
        ...
        odsResults.TypeName = GetType().AssemblyQualifiedName;
        odsResults.SelectMethod = "SelectData";
        odsResults.SelectCountMethod = "GetRecordCount";
        odsResults.EnablePaging = true;
        sgvConversionResults.DataSourceID = odsResults.ID;
        ConversionResultsCreateColumns();
        sgvConversionResults.AllowPaging = true;
        ...
    }

    protected void btnBTN_Click(object sender, EventArgs e)
    {
        // add rows into resultDataSource
    }

    public DataTable SelectData(DataTable ds,int startRowIndex,int maximumRows)
    {
        DataTable dt = new DataTable();

        dt.Columns.Add("Column1");
        dt.Columns.Add("Column2");
        dt.Columns.Add("Column3");
        dt.Columns.Add("Column4");

        for (int i =startRowIndex; i<startRowIndex+10 ;i++)
        {   
            if (i<ds.Rows.Count)
            {
                dt.Rows.Add(ds.Rows[i][0].ToString(), ds.Rows[i][1].ToString(),
                ds.Rows[i][2].ToString(), ds.Rows[i][3].ToString());
            }
        }
        return dt;
    }

    public int GetRecordCount(DataTable ds)
    {
        return ds.Rows.Count;
    }

    protected void odsResults_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.InputParameters["ds"] = resultDataSource;

    }

}

On clicking the button resultDataSource receives some rows. Page reload and we can see result in sgvConversionResults. First 10 rows. But after click next page in pager we have message "There are no items to show in this view". When I try debug I find that after postBack page (on click next page) input params "ds" is blank, ds.Rows.Count = 0 and etc... As though resultDataSource became empty((
What did I do incorrectly?


onPostBack all variables get default values, sgvConversionResults save her structure but has clear rows. How I can save sgvConversionResults data onPostBack event???

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

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

发布评论

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

评论(2

冷了相思 2024-09-06 15:12:20

也许可以尝试:

protected void Page_Load(object sender, EventArgs e)
{
    sgvConversionResults.DataBind();
}

您的代码看起来有点复杂,您使用 ObjectDataSource 有什么特殊原因吗?因为您可以在代码隐藏中将数据表直接绑定到gridview

Maybe try:

protected void Page_Load(object sender, EventArgs e)
{
    sgvConversionResults.DataBind();
}

Your code seems a bit convoluted though, any particular reason you are using an ObjectDataSource? Cause you can bind the datatable directly to the gridview in the codebehind

情深已缘浅 2024-09-06 15:12:20

我用 ViewState 传输 resultDataSource 并且这项工作成功了!

I transmit resultDataSource with ViewState and this work success!

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