我想在我的页面dynamicly
中创建标签,例如用户将在文本框中choose
标签的数量,并且我将使用.text =“XYZ显示此标签的编号”
感谢。
答案 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