2013-10-13 91 views
1

在asp.net web應用程序中,從數據庫填充datalist時出現此錯誤。
在設計的網頁我有一個項目模板標籤內的一些標籤,當我試圖通過FindControl訪問這些標籤,它給人的錯誤:Asp.net對象引用錯誤

Object reference not set to an instance of object

這裏是我的代碼:

Products.aspx的.cs:

public partial class Products : System.Web.UI.Page 
    {      
     Product product;    

     protected void Page_Load(object sender, EventArgs e) 
     {  
      if (!IsPostBack) 
       DataList1.DataBind(); 
      product = this.getProducts(); 

      Label TitleLabel = (Label)DataList1.FindControl("TitleLabel"); 
      TitleLabel.Text = product.Name;  

      Label DescLabel = (Label)DataList1.FindControl("DescLabel"); 
      DescLabel.Text = product.LongDescription;  

      Label PriceLabel = (Label)DataList1.FindControl("PriceLabel"); 
      PriceLabel.Text = product.UnitPrice.ToString();  

      ImageButton PImage = (ImageButton)DataList1.FindControl("ImageButton1"); 
      PImage.ImageUrl = "images/"+product.ImageFile;      
     }  

     private Product getProducts() 
     { 
      Product p = new Product(); 

      DataView productsTable = (DataView) 
      SqlDataSource1.Select(DataSourceSelectArguments.Empty); 

      foreach (DataRowView row in productsTable) 
      {  
       p.ProductID = row["P_Id"].ToString(); 
       p.Name = row["Title"].ToString(); 
       p.ShortDescription = row["Desc"].ToString(); 
       p.LongDescription = row["Desc_full"].ToString(); 
       p.UnitPrice = Convert.ToDecimal(row["Price"]); 
       p.ImageFile = row["imageurl"].ToString();         
      }      
      return p;  
     } 
    } 

Products.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Products.aspx.cs" Inherits="ECProject.Products" %> 

<!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:DataList ID="DataList1" runat="server" DataKeyField="P_Id" 
      DataSourceID="SqlDataSource1" RepeatColumns="4" 
      RepeatDirection="Horizontal" CellPadding="4" ForeColor="#333333" > 
      <AlternatingItemStyle BackColor="White" ForeColor="#284775" /> 
      <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
      <ItemTemplate > 

       <asp:ImageButton ID="ImageButton1" runat="server" Height = "200px"/> 
       <br /> 

       Title: 
       <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' /> 
       <br />     

       Brand: 
       <asp:Label ID="DescLabel" runat="server" Text='<%# Eval("Desc") %>' /> 
       <br />          

       Available: 
       <asp:Label ID="Is_ActiveLabel" runat="server" Text='<%# Eval("Is_Active") %>' /> 
       <br />  

       Price: 
       <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' /> 
        <br /> 
      </ItemTemplate> 
      <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
     </asp:DataList> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ECDB.mdf;Integrated Security=True;User Instance=True" 
      ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [Product]"> 
     </asp:SqlDataSource>   
    </div> 
    </form> 
</body> 
</html> 

錯誤:

Line 25:    
Line 26:    Label TitleLabel = (Label)DataList1.FindControl("TitleLabel"); 
Line 27:    TitleLabel.Text = product.Name; 
Line 28: 
Line 29: 

請幫幫忙,如何擺脫這種錯誤的?

+0

幾乎所有的'NullReferenceException'都是一樣的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 –

回答

0

要自定義您的單個DataList項目,您需要在ItemDataBound事件中執行此操作。查看this教程瞭解更多詳情。

但是,看起來您正在以不正確的方式接近您的任務。底線是你需要將你的DataSource綁定到一個項目集合,並且你試圖一個接一個地提供它的項目。讓我知道,如果我誤解了你,你確實需要綁定一個單獨的產品到你的DataList,在綁定時定製它的外觀。

2

該列表通常包含多個項目,因此您的邏輯有缺陷。你可以做什麼它在加入這樣的行處理ItemDataBound事件列表中的您Page_Load

DataList1.ItemDataBound += new DataListItemEventHandler(DataList1_ItemDataBound); 

而且有這樣的方法:

void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     Label TitleLabel = (Label)e.Item.FindControl("TitleLabel"); 
     TitleLabel.Text = "changed by code"; 
    } 
} 
0

我覺得把你拉布勒在其他物體一樣的面板。你應該在html代碼中找到你的標籤的真實名稱,所以最好的方法是在你的腳本代碼中輸入一個錯誤的錯誤,當你的解決方案運行時,它會停下來,你可以找到你的標籤控件的真實名稱。然後使用以下代碼:

標籤標題標籤=(標籤)DataList1.FindControl(「標籤控制的真實名稱」);

我希望它能幫助你。