ASP.Net添加控件“无法获取[控件]的内部内容,因为内容不是文字的。”

时间:2009-09-16 14:50:59

标签: asp.net

我目前正在尝试在我的网络表单中动态添加HTML控件,但每次我在另一个控件中插入一个控件时弹出错误Cannot get inner content of [control] because the contents are not literal(当试图查看innerHtml或innerText时)并且似乎无法找到原因。

以下是我正在使用的代码:

Dim newsList As New HtmlControls.HtmlGenericControl
    newsList.TagName = "ul"
    Dim i As Integer = 0
    For Each newsDR As DataRow In newDS.Tables(0).Rows
        i += 1
        Dim stri As String = i.ToString()
        Dim newsListItem As New HtmlControls.HtmlGenericControl
        newsListItem.TagName = "li"
        newsListItem.ID = "newsListItem" + stri

        Dim newsTitle As New HtmlControls.HtmlGenericControl
        newsTitle.TagName = "h1"
        newsTitle.ID = "newsTitle" + stri
        Dim newsAnchor As New HtmlControls.HtmlAnchor
        newsAnchor.ID = "newsAnchor" + stri
        newsAnchor.InnerHtml = newsDR("NewsTitle")
        newsAnchor.HRef = "#"

        newsTitle.Controls.Add(newsAnchor)

        Dim newsSummary As New HtmlControls.HtmlGenericControl
        newsSummary.TagName = "div"
        newsSummary.ID = "newsSummary" + stri
        newsSummary.InnerHtml = newsDR("NewsSummary")

        newsListItem.Controls.Add(newsTitle)
        newsListItem.Controls.Add(newsSummary)
        newsList.Controls.Add(newsListItem)
    Next

1 个答案:

答案 0 :(得分:2)

渲染控件后不应使用innerHtml,而是使用RenderControl,如下所示:

http://forums.asp.net/t/1168614.aspx