Tutorial :ASP.Net Add Control “Cannot get inner content of [control] because the contents are not literal.”



Question:

I am currently trying to dynamically add HTML controls in my web form, but each time I insert a control in another control the error Cannot get inner content of [control] because the contents are not literal pops up(when trying to look at innerHtml or innerText) and cannot seem to find the reason why.

Here is the code that I am using:

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  


Solution:1

You should not use innerHtml after rendering the controls, use RenderControl instead, as stated here:

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »