ASP.Net 添加控件 "不能获取 [控件] 的内部内容,因为其内容不是字面量。"

6

我目前正在尝试在我的网页表单中动态添加HTML控件,但每次在另一个控件中插入控件时,错误“无法获取[control]的内部内容,因为内容不是文字”(尝试查看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

哪一行代码出错了? - NotMe
1个回答

2

谢谢,使用RenderControl很有效。我之前使用了ICallbackEventHandler接口,所以渲染已经发生,无法使用InnerHTML。 - Narayana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接