[英]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.