移动分页时,Formview中的字段未更改 [英] Fields not changing in Formview when moving Pagination

查看:84
本文介绍了移动分页时,Formview中的字段未更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的代码:

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;
}

请帮助, 谢谢

推荐答案

在设置新的页面索引后,您将需要将数据绑定到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();
}

这是必需的,因为FormView仅显示活动记录的数据,并且不存储数据源中的任何其他记录,因此,在更改页面索引时,需要再次绑定数据源.请参阅:在FormView Web服务器控件中分页

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

从上面的链接:

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

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.

希望这会有所帮助.

这篇关于移动分页时,Formview中的字段未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆