Nurlan
Nurlan

Reputation: 2960

add <br/> and <hr/> to page in codebehind asp.net

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

Answers (4)

Waqar Janjua
Waqar Janjua

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

Claudio Redi
Claudio Redi

Reputation: 68440

You could use a HtmlGenericControl

 var hrControl = new HtmlGenericControl("hr")
 this.Page.Form.FindControl("ContentPlaceHolder1").Controls.Add(hrControl);

Upvotes: 7

D Stanley
D Stanley

Reputation: 152634

Add a LiteralControl:

this.Page.Form.FindControl("ContentPlaceHolder1")
    .Controls.Add(new LiteralControl("<hr/>"));

Upvotes: 7

jon
jon

Reputation: 728

you can use a literal control

Literal c = new Literal();
c.Text = "<hr />;

Upvotes: 4

Related Questions