Reputation: 2960
I am adding labels to the page programmaticaly(codebehind file c#)
Label label1 = new Label();
label1.Text = "abc";
this.Page.Form.FindControl("ContentPlaceHolder1").Controls.Add(label1);
Label label2 = new Label();
label2.Text = "def";
this.Page.Form.FindControl("ContentPlaceHolder1").Controls.Add(label2);
I want to add hr and br between these labels.How to do that?
this.Page.Form.FindControl("ContentPlaceHolder1").Controls.Add("<hr/>");
doesn't work.
Upvotes: 4
Views: 32413
Reputation: 6123
Label label1 = new Label();
label1.Text = "Test 1";
form1.Controls.Add(label1);
form1.Controls.Add(new Literal() { ID="row", Text="<hr/>" } );
Label label2 = new Label();
label2.Text = "Test 2";
form1.Controls.Add(label2);
Output:
Test 1
---------------------------------------------------------------------------------
Test 2
Upvotes: 13
Reputation: 68440
You could use a HtmlGenericControl
var hrControl = new HtmlGenericControl("hr")
this.Page.Form.FindControl("ContentPlaceHolder1").Controls.Add(hrControl);
Upvotes: 7
Reputation: 152634
Add a LiteralControl
:
this.Page.Form.FindControl("ContentPlaceHolder1")
.Controls.Add(new LiteralControl("<hr/>"));
Upvotes: 7
Reputation: 728
you can use a literal control
Literal c = new Literal();
c.Text = "<hr />;
Upvotes: 4