I have a foreach loop which loops through every row to add a li
and an a
element. The current row value of the column 'stars' is added to the a
element as you can see from the code below, the gymTypes has 6 rows which are:
1 STAR 2 STAR 3 STAR 4 STAR 5 STAR 6 STAR
So as you can imagine I am trying to create html elements for each one of them, but when debugging it only loops through the foreach once and gives an exception of "System.StackOverflowException" and then it reloads the page.
foreach(DataRow row in gymTypes.Rows)
{
HtmlGenericControl li = new HtmlGenericControl("li");
HtmlGenericControl a = new HtmlGenericControl("a");
a.InnerText = row["stars"].ToString();
li.Controls.Add(li);
gym_tab_stars.Controls.Add(li);
}
Does anyone know why I am getting this exception?
Thanks
The issue is that the li
has itself as a child. This is causing the stack overflow exception when the HTML is being rendered.
Change li.Controls.Add(li);
to li.Controls.Add(a)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.