簡體   English   中英

將數據動態添加到網格視圖內的ASP文字控件中

[英]Adding data dynamically to a asp literal control inside a grid view

我試圖將數據添加到放置在gridview內的文字。 當前代碼如下所示

protected void GvListingRowDataBound(object sender, GridViewRowEventArgs e)
{
       var query = DisplayAllData();
       Literal info = (Literal)e.Row.FindControl("ltritemInfo");

       if(query != null)
       {
           foreach (var listing in query)
           {
               var list = DisplayListById(listing.id);
               info.Text = "<h3>" + list.title + "</h3>";
               info.Text += "<h4>" + list.description + "</h4>";
           }
       }
}

這將產生一個錯誤

你調用的對象是空的。

如果有人對此有想法,那將是很大的幫助

謝謝

確保僅對數據行進行操作,而不對頁眉,頁腳,分隔符,分頁器等進行操作。其枚舉是DataControlRowtype 這就是為什么您的info對象/引用為null的原因,因為它首先對標頭進行操作。

  • 檢查e.Row.RowType是否為DataRow類型。
  • 為了安全起見,還請檢查您的info是否不為null。
protected void GvListingRowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {        
       var query = DisplayAllData();
       Literal info = (Literal)e.Row.FindControl("ltritemInfo");

       if(query != null && info !=null) 
       {
           foreach (var listing in query)
           {
                var list = DisplayListById(listing.id);
                info.Text = string.Format("<h3>{0}</h3><h4>{1}</h4>",
                               list.title, list.description);    
           }
       }
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM