移动分页时 Formview 中的字段不会更改

发布于 2024-12-05 14:11:29 字数 975 浏览 3 评论 0原文

我有一个 formview,在页面加载时调用 sql 服务器并检索 5 条记录,但我希望 formview 对其进行分页。

我已成功连接到数据库,填充数据集,并在网页渲染时返回数据。问题是,当我移动到新页面时,数据绑定字段中的数据没有更改。

这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        conn = new SqlConnection(connstr);
        ds = new DataSet();
        da = new SqlDataAdapter("call to stored proc", conn);

        try

        {
            conn.Open();
            da.Fill(ds, "m");
            FormView1.DataSource = ds;
            FormView1.DataKeyNames = new string[] { "PropKey" };
            FormView1.DataBind();
        }
        catch (Exception ex)
        {
            Result = ex.Message;
        }
        finally
        {
            conn.Close();
        }
    }

接下来,当单击分页按钮时,我有以下内容:

protected void FormView1_PageIndexChanging1(object sender, FormViewPageEventArgs e)
{
    FormView1.PageIndex = e.NewPageIndex;
}

请帮忙, 谢谢

I have a formview that on page load makes a call to a sql server and retrieves 5 records which I want the formview to paginate though.

I have sucessfully connected to the db, filled a dataset, and returned data when the web page renders. The problem is that when I move to a new page, the data does not change in the databound field.

Here is my code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        conn = new SqlConnection(connstr);
        ds = new DataSet();
        da = new SqlDataAdapter("call to stored proc", conn);

        try

        {
            conn.Open();
            da.Fill(ds, "m");
            FormView1.DataSource = ds;
            FormView1.DataKeyNames = new string[] { "PropKey" };
            FormView1.DataBind();
        }
        catch (Exception ex)
        {
            Result = ex.Message;
        }
        finally
        {
            conn.Close();
        }
    }

Next when the paginate buttons are clicked I have this:

protected void FormView1_PageIndexChanging1(object sender, FormViewPageEventArgs e)
{
    FormView1.PageIndex = e.NewPageIndex;
}

Please help,
Thanks

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

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

发布评论

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

评论(1

昇り龍 2024-12-12 14:11:29

在设置新页面索引后,您需要将数据绑定到 FormView,如下所示。

protected void FormView1_PageIndexChanging1(object sender, FormViewPageEventArgs e)
{
    FormView1.PageIndex = e.NewPageIndex;
    BindFormViewData();
}

这是必需的,因为 FormView 只显示活动记录的数据,不存储数据源中的任何其他记录,因此当页面索引发生更改时,需要重新绑定数据源。请参阅:FormView Web 服务器控件中的分页

从上面的链接:

如果 FormView 控件绑定到数据源控件或任何
实现 ICollection 接口的数据结构(包括
datasets),控件从数据源获取所有记录,
显示当前页面的记录,并丢弃其余的。什么时候
用户移动到另一个页面,FormView 控件重复
处理,显示不同的记录。

希望这有帮助。

You will need to bind the data to the FormView just after setting the new page index like below.

protected void FormView1_PageIndexChanging1(object sender, FormViewPageEventArgs e)
{
    FormView1.PageIndex = e.NewPageIndex;
    BindFormViewData();
}

This is required because the FormView only displays the data for the active record and does not store any other records from the datasource, so upon change of the page index, the datasource is required to be bound again. See: Paging in a FormView Web Server Control

From the above link:

If the FormView control is bound to a data source control, or to any
data structure that implements the ICollection interface (including
datasets), the control gets all the records from the data source,
displays the record for the current page, and discards the rest. When
the user moves to another page, the FormView control repeats the
process, displaying a different record.

Hope this helps.

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