【问题标题】:Error when paging in GridView在 GridView 中分页时出错
【发布时间】:2015-04-23 07:23:49
【问题描述】:

我有一个包含两列的 GridView。第一个显示日期,第二个显示第一列中日期的年份。到目前为止一切正常,但是当我更改页面时,网络失败并显示以下消息:“对象引用未设置为对象的实例。”

这是我的代码:

Test.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="qq_site_Test" %>

<!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>TEST</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" 
            AllowSorting="True" AutoGenerateColumns="False" 
            EnableModelValidation="True" onrowcreated="GridView1_RowCreated" 
            onpageindexchanged="GridView1_PageIndexChanged" 
            onpageindexchanging="GridView1_PageIndexChanging">
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="creationDate" HeaderText="creationDate" SortExpression="creationDate" />
                <asp:TemplateField HeaderText="Year">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>                
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Test.aspx.cs

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

public partial class qq_site_Test : System.Web.UI.Page
{
    static DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            String connectionString = "DSN=kitchenmaster.es.qq-site;";
            String sqlQuery = "SELECT * FROM tblSystems WHERE ID < 100";

            ds = new DataSet();

            OdbcConnection connection = new OdbcConnection(connectionString);
            OdbcCommand command = new OdbcCommand(sqlQuery, connection);
            OdbcDataAdapter adapter = new OdbcDataAdapter(command);

            try
            {
                adapter.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            catch (System.Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
            }
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            try
            {
                DateTime dt = (DateTime)DataBinder.Eval(e.Row.DataItem, "creationDate");
                e.Row.Cells[GetColumnByID("Year")].Text = dt.Year.ToString();
            }
            catch (System.Exception ex)
            {
                Response.Write(ex.Message);             
            }
        }
    }

    protected int GetColumnByID(String columnName)
    {
        foreach (DataControlField column in GridView1.Columns)
        {
            if (column.HeaderText == columnName)
                return GridView1.Columns.IndexOf(column);
        }

        return -1;
    }

    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        GridView1.SelectedIndex = -1;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            if (e.NewPageIndex != -1)
            {
                GridView1.PageIndex = e.NewPageIndex;
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
        catch (System.Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
}

你能帮帮我吗?

【问题讨论】:

  • 您收到上述异常的确切代码行是什么?
  • 异常发生在事件GridView1_RowCreated()
  • 哪条,有什么例外?

标签: c# asp.net gridview


【解决方案1】:

这行代码

GridView1.DataSource = ds;

可能是您遇到错误的地方。

当调用GridView1_PageIndexChanging 事件时,您尚未设置ds 的值。您的代码中唯一这样做的部分是在 Page_Load 事件中,并且仅当它不是回发时。

【讨论】:

    【解决方案2】:

    如果我在事件 GridView1_RowCreated() 中检查 e.Row.DataItem 的值,则不会发生错误,但我不知道这样做是否正确。新版本的活动是:

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                try
                {
                    if (e.Row.DataItem != null)
                    {
                        DateTime dt = (DateTime)DataBinder.Eval(e.Row.DataItem, "creationDate");
                        e.Row.Cells[GetColumnByID("Year")].Text = dt.Year.ToString();
                    }
                }
                catch (System.Exception ex)
                {
                    Response.Write("** " + ex.Message);
                }
            }
        }
    

    【讨论】:

    • 当然,你避免了错误,但你的分页真的有效吗?
    【解决方案3】:

    请在 PageIndexChanging 事件中检查您的“ds”对象。 先填充数据集并绑定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多