分页时,网格视图值不绑定

时间:2014-03-21 11:11:06

标签: asp.net gridview webforms

在下面的代码中,我有一个网格,当我尝试分页时抛出页面索引更改错误,所以我试图修复该问题。但现在,当我分页时,它没有绑定值。所以任何人都帮我解决问题

<asp:GridView 
 OnPageIndexChanging="grdsrchlst_PageIndexChanging" ID="grdsrchlst" runat="server"
 PagerStyle-Mode="NumericPages" PageSize="10" PagerStyle-Visible="true"
 AutoGenerateColumns="False" AllowPaging="True">
</asp:GridView> 

代码:

protected void grdsrchlst_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grdsrchlst.PageIndex = e.NewPageIndex;
    SearchInformation(DataTable dtSearch); //Throws error ...i cant bind the value when paginate
}

public void SearchInformation(DataTable dtSearch)
{ 
    grdsrchlst.DataSource = dtSearch;
    grdsrchlst.DataBind();
}

4 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

dtSearch事件中,null值为grdsrchlst_PageIndexChanging

首先加载dtSearch,然后尝试绑定。

答案 2 :(得分:0)

我认为问题在于您在grdsrchlst_PageIndexChanging中调用SearchInformation()函数的方式。您在调用本身中声明了一个新参数dtSearch,因此它会在未初始化时发出错误。

尝试初始化dataTable,如下所示

protected void grdsrchlst_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdsrchlst.PageIndex = e.NewPageIndex;

            //Initialize dtSearch
            DataTable dtSearch = new DataTable();
            SearchInformation(dtSearch);
        }

在给定的代码中虽然你已经初始化了dtSearch,但它仍然没有任何值因此你的网格将不会被显示。其次,我发现你有&#39; AutoGenerateColumns&#39;网格的参数设置为false。因此,除非您在代码中的某个位置向gridview添加了列,否则如果与没有值或列的数据表绑定,则不会显示网格。

以下是我编写的示例代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CSharpWebFormTrials._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView 
            OnPageIndexChanging="grdsrchlst_PageIndexChanging"
            ID="grdsrchlst" runat="server" PagerStyle-Mode="NumericPages"
            PageSize="10" PagerStyle-Visible="true" AutoGenerateColumns="True" AllowPaging="True" Visible="true">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

背后的代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace CSharpWebFormTrials
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dtSearch = fillData();
            SearchInformation(dtSearch);

            //Store in viewstate
            ViewState["dtSearch"] = dtSearch;

            //Store in Session
            //Session["dtSearch"] = dtSearch;
        }

        protected void grdsrchlst_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdsrchlst.PageIndex = e.NewPageIndex;

            //Initialize the dtSearch variable
            //DataTable dtSearch = new DataTable();

            //Alternately you can fill the data
            //DataTable dtSearch = fillData();

            //Access vuewstate
            DataTable dtSearch;
            if (ViewState["dtSearch"] != null)
            {
                dtSearch = (DataTable)ViewState["dtSearch"];
            }
            else
            {
                dtSearch = fillData();
            }

            //Access Sessiop
            //if (Session["dtSearch"] != null)
            //{
            //    dtSearch = (DataTable)Session["dtSearch"];
            //}
            //else
            //{
            //    dtSearch = fillData();
            //}

            SearchInformation(dtSearch);
        }

        public void SearchInformation(DataTable dtSearch)
        {
            grdsrchlst.DataSource = dtSearch;
            grdsrchlst.DataBind();
        }

        /// <summary>
        /// Filling the dataTable with Dummy data
        /// </summary>
        /// <returns> 
        /// Filled dataTable
        /// </returns>

        public DataTable fillData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Col1");
            dt.Columns.Add("Col2");
            dt.Columns.Add("Col3");
            dt.Columns.Add("Col4");

            for (int i = 0; i < 20; i++)
            {
                dt.Rows.Add(i, i, i, i);
            }
            return dt;
        }
    }
}

我自己是初学者,如果我传递了任何错误的信息,请专家评论。

答案 3 :(得分:0)

;首次填充时将DataTable粘贴在ViewState中,并从ViewState中检索它,并在每次更改分页时重新绑定。

ViewState["dtSearch"] = dtSearch;

然后,当您在PageIndex中移动时发生回发 - 您可以通过写入来获取DataTable:

dtSearch = (DataTable)ViewState["dtSearch"];