UpdatePanel 或 PlaceHolder 组件内的 GridView 分页

发布于 2024-08-15 19:23:09 字数 2327 浏览 3 评论 0原文

我是 ASP.NET 新手。

我正在开发 ASP.NET C# Web 表单,该表单动态创建 GridView 组件并使用从我的 Web 服务接收到的数据填充它们。

我正在服务器端(cs 文件)以编程方式创建这些 GridView 组件 - 它必须灵活 - 1 个 GridView,有时 10 个 GridView 组件。

当我尝试添加分页时会出现问题 - 每当用户单击“下一页”时,整个页面都会因为回发而刷新,并且我会丢失所有数据并且页面返回空白/空。

我使用 PlaceHolder 来保存 GridView 组件,在寻找解决方案时,我发现 UpdatePanel 作为更好的替代方案 - 据我了解页面可以部分刷新 - 这意味着只需刷新 UpdatePanel...但它不起作用。

以下代码示例是我的测试,UpdatePanel 是在客户端(.aspx 页面)中启动的唯一组件,其余组件在 .cs 中以编程方式启动。

我该如何解决上述问题?

为什么整个页面都在刷新,而我却丢失了数据? 你能推荐另一种方式吗?可以为我提供任何代码示例吗?

如果我不重建GridView,它就不起作用...

这是我的Default.aspx.cs

public partial class TestAjaxForm : System.Web.UI.Page
{
    DataTable table;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bindGridView();
    }

    public void bindGridView()
    {

        GridView gridView1 = new GridView();
        gridView1.AutoGenerateColumns = true;

        gridView1.PageSize = 2;
        gridView1.AllowPaging = true;
        gridView1.PagerSettings.Mode = PagerButtons.Numeric;
        gridView1.PagerSettings.Position = PagerPosition.Bottom;
        gridView1.PagerSettings.PageButtonCount = 10;
        gridView1.PageIndexChanging += new GridViewPageEventHandler(this.GridView1_PageIndexChanging);

        table = new DataTable();
        table.Columns.Add("FirstName");
        table.Columns.Add("LastName");

        DataRow row = table.NewRow();
        row["FirstName"] = "John";
        row["LastName"] = "Johnoson";
        table.Rows.Add(row);

        row = table.NewRow();
        row["FirstName"] = "Johnny";
        row["LastName"] = "Marley";
        table.Rows.Add(row);

        row = table.NewRow();
        row["FirstName"] = "Kate";
        row["LastName"] = "Li";
        table.Rows.Add(row);

        panel.ContentTemplateContainer.Controls.Add(gridView1);

        gridView1.DataSource = table;
        gridView1.DataBind();

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView gridView1 = (GridView)sender;
        gridView1.PageIndex = e.NewPageIndex;
        gridView1.DataSource = table;
        gridView1.DataBind();
    }
}

谢谢。

I'm new to ASP.NET.

Im developing ASP.NET C# web form which creating GridView components dynamically and filling them using the data received from my webservice.

I'm creating these GridView components programmatically in server-side (cs file)- it has to be flexible - 1 GridView and sometimes 10 GridView components.

The problem occurs when I'm trying to add pagination - whenever the user clicks the "next" page, the whole page is getting refreshed because of a postBack and I'm loosing all my data and the page returns Blank/Empty.

I used PlaceHolder to hold the GridView components, while searching for a solution, I found UpdatePanel as a better alternative - as far as I understand the page can be partially refreshed - which means that only the UpdatePanel has to be refreshed...but it doesn't work.

The following code sample is my TEST, the UpdatePanel is the only component initiated in the client side (.aspx page), the rest initiated programmatically in .cs .

how can I solve the issue described above?

Why does the whole page is getting refreshed and I'm loosing my data?
can you recommend another way? can provide me with any code sample?

If I'm not rebuilding the GridView, it doesn't work...

Here is my Default.aspx.cs

public partial class TestAjaxForm : System.Web.UI.Page
{
    DataTable table;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bindGridView();
    }

    public void bindGridView()
    {

        GridView gridView1 = new GridView();
        gridView1.AutoGenerateColumns = true;

        gridView1.PageSize = 2;
        gridView1.AllowPaging = true;
        gridView1.PagerSettings.Mode = PagerButtons.Numeric;
        gridView1.PagerSettings.Position = PagerPosition.Bottom;
        gridView1.PagerSettings.PageButtonCount = 10;
        gridView1.PageIndexChanging += new GridViewPageEventHandler(this.GridView1_PageIndexChanging);

        table = new DataTable();
        table.Columns.Add("FirstName");
        table.Columns.Add("LastName");

        DataRow row = table.NewRow();
        row["FirstName"] = "John";
        row["LastName"] = "Johnoson";
        table.Rows.Add(row);

        row = table.NewRow();
        row["FirstName"] = "Johnny";
        row["LastName"] = "Marley";
        table.Rows.Add(row);

        row = table.NewRow();
        row["FirstName"] = "Kate";
        row["LastName"] = "Li";
        table.Rows.Add(row);

        panel.ContentTemplateContainer.Controls.Add(gridView1);

        gridView1.DataSource = table;
        gridView1.DataBind();

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView gridView1 = (GridView)sender;
        gridView1.PageIndex = e.NewPageIndex;
        gridView1.DataSource = table;
        gridView1.DataBind();
    }
}

Thank you.

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

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

发布评论

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

评论(3

谷夏 2024-08-22 19:23:09

如果您希望自定义 GridView 方法起作用,则必须在每次页面加载时重新创建并重新绑定网格...这就是动态网格的问题...动态控件不会保留其视图状态,但如果您添加了网格并动态生成列,这对你来说会更容易(因为我认为它可能会动态记住列,或者你可以将 AutoGenerateColumns 设置为 true,它会引入你的数据行列名称)。

华泰

If you want a custom GridView approach to work, you have to recreate and rebind the grid on every page load... that's the problem with dynamic Grids... Dynamic controls don't retain their viewstate, but if you added the grid and dynamically generated the columns, that would work easier for you (because I think it may dynamically remember the columns, or you can set AutoGenerateColumns to true, and it brings in your data row column names).

HTH

献世佛 2024-08-22 19:23:09

您应该创建 GridView 并将其添加到 PreInit 或 Init 事件处理程序中的控件树中,以便它们有机会正确绑定到 ViewState,并且无论是否存在回发都应该执行此操作:如果您不添加它们在回发中,他们显然不会在那里显示任何内容。

数据绑定可以在页面生命周期的后期发生。

分页似乎仅在您还使用 ObjectDataSource 时才起作用;我从来没能让它单独与 GridView 一起工作。

You should create your GridView and add it to the control tree in the PreInit or Init event handler so that they have a chance to properly bind to ViewState, and it should be done whether or not there's a postback: if you don't add them in a postback, they obviously won't be there to display anything.

DataBinding can happen later in the page life cycle.

Paging only seems to work if you also use an ObjectDataSource; I've never been able to get it to work with a GridView by itself.

倾其所爱 2024-08-22 19:23:09

我认为最简单的方法是在占位符内的 aspx 页面中声明 gridview,而占位符也包含在更新面板内。

据我所见,约翰没有做任何无法在标记本身中声明的事情,因此这不应该成为问题。如果由于某种原因 gridview 不应显示在屏幕上,只需将占位符的 Visible 属性设置为 false 即可。

就分页而言,正如 RickNz 正确指出的那样,如果将 gridview 绑定到 LinqDatasource、SqlDatasource 或 ObjectDatasource,它只会保留状态,但如果将其绑定到自定义业务对象,您需要做的就是保存Session 中的数据并在 PageIndexChanged 上再次重新绑定它。

在伪代码中,会是这样的。

Page_load
{
     If (!IsPostBack)
     Binddata();
 }

 private void Binddata()
 {
     var data = Getdata();
     Gridview1.DataSource= data;
     Gridview.DataBind();
     Session["data"]=data; // cache the data 
  }

 protected void Gridview1_indexchanged(object sender, GridviewPageEventArgs e)
 {
      var data= Session["data"];
      Gridview1. DataSource=data;
      Gridview1.DataBind();
      GridView1.PageIndex=e.NewPageIndex;
  }

I think the easiest thing to do is have the gridview declared in the aspx page inside the place holder, while the place holder is also contained inside the update panel.

For what I can see, John is not doing anything that can't be declared in the markup itself so that shouldn't be a problem. If for some reason the gridview should not be displayed on the screen, it suffices to set the Visible property of the placeholder to false.

As far as paging is concerned, as RickNz correctly points out, it will only retain the state if you bind the gridview to either a LinqDatasource, SqlDatasource or ObjectDatasource but if you bind it to custom business objects, what you need to do is save the data in Session and rebind it again on PageIndexChanged.

In pseudo code, would be sometging like this.

Page_load
{
     If (!IsPostBack)
     Binddata();
 }

 private void Binddata()
 {
     var data = Getdata();
     Gridview1.DataSource= data;
     Gridview.DataBind();
     Session["data"]=data; // cache the data 
  }

 protected void Gridview1_indexchanged(object sender, GridviewPageEventArgs e)
 {
      var data= Session["data"];
      Gridview1. DataSource=data;
      Gridview1.DataBind();
      GridView1.PageIndex=e.NewPageIndex;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文