在ASP.NET(VB)上动态创建标签

时间:2010-06-16 21:24:45

标签: asp.net vb.net

我想在我的页面dynamicly中创建标签,例如用户将在文本框中choose标签的数量,并且我将使用.text =“XYZ显示此标签的编号”

感谢。

3 个答案:

答案 0 :(得分:1)

快速而脏的方法(此示例在ASP.NET页面上向PlaceHolder添加10个标签和文字:

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

答案 1 :(得分:0)

答案 2 :(得分:0)

要完成这项工作需要做很多事情,但只需动态创建控件并将其添加到页面中,您需要在ASPX页面上Placeholder

<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