2015-04-23 66 views
0

我有一個帶有兩列的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); 
     } 
    } 
} 

你能幫助我嗎?

+3

接收上述異常的確切代碼行是什麼? –

+0

事件GridView1_RowCreated() –

+0

哪個*行*和什麼是異常發生異常? – naveen

回答

1

這行代碼

GridView1.DataSource = ds; 

可能是你在哪裏得到的錯誤。

當調用GridView1_PageIndexChanging事件時,您尚未設置ds的值。代碼中的唯一部分是在Page_Load事件中,並且只有當它不是回發時。

0

如果我在事件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); 
      } 
     } 
    } 
+0

當然,你可以避免這個錯誤,但是你的分頁確實有效嗎? – rikitikitik

0

請檢查您的「DS」的對象裏面PageIndexChanging事件。 先填充數據集並綁定。