Reputation: 159
I've got a dynamically created List in asp.net with the following code:
HtmlGenericControl li = new HtmlGenericControl("li");
li.ID = "liQuestions" + recordcount.ToString();
li.Attributes.Add("role", "Presentation");
ULRouting.Controls.Add(li);
HtmlGenericControl anchor = new HtmlGenericControl("a");
li.Attributes.Add("myCustomIDAtribute", recordcount.ToString());
anchor.InnerText = "Test " + new HtmlGenericControl("br") + "12345";
li.Controls.Add(anchor);
I tried to put in a HtmlGenericControl but that doesn't work. How do I add a br inbetween Test and 12345 please?
Upvotes: 1
Views: 1326
Reputation: 681
Although I'm not able to test it myself right now, I believe the first method in hazzik's answer would work, but I don't think his second method would. The HtmlGenericControl is known to not support self-closing elements, which the BR element is.
The following SO questions have more detail and another possible solution:
HtmlGenericControl("br") rendering twice
Adding <br/> dynamically between controls asp.net
Upvotes: 1
Reputation: 13344
You need to use InnerHtml
property
HtmlGenericControl li = new HtmlGenericControl("li");
li.ID = "liQuestions" + recordcount.ToString();
li.Attributes.Add("role", "Presentation");
ULRouting.Controls.Add(li);
HtmlGenericControl anchor = new HtmlGenericControl("a");
li.Attributes.Add("myCustomIDAtribute", recordcount.ToString());
anchor.InnerHtml = "Test <br/> 12345";
li.Controls.Add(anchor);
Or, like this:
anchor.Controls.Add(new LiteralControl("Test")); //or new Literal("Test");
anchor.Controls.Add(new HtmlGenericControl("br"));
anchor.Controls.Add(new LiteralControl("12345")); //or new Literal("12345");
Upvotes: 4