我有一個帶有兩列的GridView。第一列顯示日期,第二列顯示日期的第一列。到目前爲止,一切正常,但當我更改頁面時,Web失敗並顯示以下消息:「未將對象引用設置爲對象的實例。」在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() –
哪個*行*和什麼是異常發生異常? – naveen