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