2010-06-16 87 views

回答

1

的快速和肮脏的方法(本例中增加了10个标签和文字的占位符ASP.NET页面上:

Dim c As Integer = 0 
While c < 10 
    Dim lab As New Label() 
    Dim ltr As New Literal() 
    lab.Text = c.ToString() 
    ltr.Text = "<br/>" 
    PlaceHolder1.Controls.Add(lab) 
    PlaceHolder1.Controls.Add(ltr) 
    C+=1 
End While 
+0

非常感谢。参见:http://stackoverflow.com/questions/3057533/adding-link-to-a-label-asp-net-vb – 2010-06-16 22:11:14

0

有许多事情需要做,以使这项工作,但简单地动态创建控件并将它们添加页面,您将需要一个Placeholder您的ASPX页面上:

<asp:TextBox ID="txtLabelCount" runat="server" /> 
<asp:Button ID="btnCreate" runat="server" Text="Create" /><br /> 
<asp:Placeholder ID="PlaceHolder1" runat="server" /> 

然后,在btnCreate的单击事件处理程序:

' Number of labels to create. txtLabelCount should be validated to ensure only integers are passed into it 
Dim labelCount As Integer = txtLabelCount.Text 

For i As Integer = 0 To labelCount - 1 
    ' Create the label control and set its text attribute 
    Dim Label1 As New Label 
    Label1.Text = "XYZ" 

    Dim Literal1 As New Literal 
    Literal1.Text = "<br />" 

    ' Add the control to the placeholder 
    PlaceHolder1.Controls.Add(Label1) 
    PlaceHolder1.Controls.Add(Literal1) 
Next 
+0

如何在它们之间添加
??? !! – 2010-06-16 21:48:25

+0

@AZIRAR - 对不起,忘了。只需在标签后添加一个新文字,其文字设置为'
'。我已经修改了我的答案来证明这一点。顺便说一句,执行'
'的XHTML方式是'
'。 – 2010-06-16 22:58:59