我正在向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);
我想在这些标签之间添加hr和br。怎么做?
this.Page.Form.FindControl("ContentPlaceHolder1").Controls.Add("<hr/>");
不起作用。
答案 0 :(得分:13)
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
答案 1 :(得分:6)
this.Page.Form.FindControl("ContentPlaceHolder1")
.Controls.Add(new LiteralControl("<hr/>"));
答案 2 :(得分:6)
您可以使用HtmlGenericControl
var hrControl = new HtmlGenericControl("hr")
this.Page.Form.FindControl("ContentPlaceHolder1").Controls.Add(hrControl);
答案 3 :(得分:4)
你可以使用文字控件
Literal c = new Literal();
c.Text = "<hr />;