在VB.NET中创建动态TextBox [英] Create dynamic TextBoxes in VB.NET
问题描述
您好,
我尝试在VB.NET中创建动态TextBox。
I try to create dynamic TextBoxes in VB.NET.
.aspx文件中的代码是:
The code in the .aspx file ist:
< asp:Panel ID =" pnlTextBoxes" runat =" server">
< / asp:Panel>
< hr />
< asp:Button ID =" btnAdd" RUNAT = QUOT;服务器" Text =" Add New"的OnClick = QUOT; AddTextBox" />
$
< asp:Button ID =" btnGet" RUNAT = QUOT;服务器" Text ="获取值"的OnClick = QUOT; GetTextBoxValues" />
<asp:Panel ID="pnlTextBoxes" runat="server">
</asp:Panel>
<hr />
<asp:Button ID="btnAdd" runat="server" Text="Add New" OnClick="AddTextBox" />
<asp:Button ID="btnGet" runat="server" Text="Get Values" OnClick="GetTextBoxValues" />
背后的代码:
受保护的子Page_PreInit(发件人为对象,e为EventArgs)处理Me.PreInit
Dim keys As List(Of String)= Request.Form.AllKeys.Where(Function(key)key.Contains(" txtDynamic"))。ToList()
  ;&NBSP;&NBSP;&NBSP; Dim i As Integer = 1
For Each key As String In keys
Me.CreateTextBox(键)
i + = 1
下一页
结束点¥
受保护的子AddTextBox(发件人为对象,e为EventArgs)
Dim index As Integer = pnlTextBoxes.Controls.OfType(Of TextBox)()。ToList()。Count + 1
Me.CreateTextBox(" txtDynamic"& index)
结束点¥
Private Sub CreateTextBox(id As String)
Dim txt As New TextBox()
txt.ID = id
Me.pnlTextBoxes.Controls.Add(txt)
Dim lt As New Literal()
lt.Text ="< br />"
pnlTextBoxes.Controls.Add(lt)
结束点¥
受保护的子GetTextBoxValues(发件人为对象,e为EventArgs)
Dim message As String =""
For Each textBox As TextBox in pnlTextBoxes.Controls.OfType(Of TextBox)()
message + = textBox.ID +":" + textBox.Text +" \ n"
下一个
ClientScript.RegisterClientScriptBlock(Me.GetType()," alert"," alert('"& message&"');",True)
  ; End Sub
Protected Sub Page_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
Dim keys As List(Of String) = Request.Form.AllKeys.Where(Function(key) key.Contains("txtDynamic")).ToList()
Dim i As Integer = 1
For Each key As String In keys
Me.CreateTextBox(key)
i += 1
Next
End Sub
Protected Sub AddTextBox(sender As Object, e As EventArgs)
Dim index As Integer = pnlTextBoxes.Controls.OfType(Of TextBox)().ToList().Count + 1
Me.CreateTextBox("txtDynamic" & index)
End Sub
Private Sub CreateTextBox(id As String)
Dim txt As New TextBox()
txt.ID = id
Me.pnlTextBoxes.Controls.Add(txt)
Dim lt As New Literal()
lt.Text = "<br />"
pnlTextBoxes.Controls.Add(lt)
End Sub
Protected Sub GetTextBoxValues(sender As Object, e As EventArgs)
Dim message As String = ""
For Each textBox As TextBox In pnlTextBoxes.Controls.OfType(Of TextBox)()
message += textBox.ID + ": " + textBox.Text + "\n"
Next
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
在没有使用Site.Master的情况下,在运行时创建动态TextBox是没有问题的。我可以创建一个,两个......文本框一个接一个没有问题。
There is no problem to crate dynamic TextBoxes at runtime when no Site.Master is in use. I can create one, two ... Textboxes one after another without problem.
当我使用Site.Master 您可以
创建 在运行时只有一个文本框。
创建第二个 框 将产生类型'的异常' System.NullReferenceException"。我的错在哪里?
When I use an Site.Master you can create only one Textbox at runtime. The creating of a second boxThe will produce an An exception of type 'System.NullReferenceException'. Where is my fault?
在使用Site.Master时,无法动态创建文本框?
It is not possible creating dynamicly Textboxes when a Site.Master is in use??
问候语Katharina
greetings Katharina
推荐答案
当我使用Site.Master 时,您可以
在运行时只创建一个文本框 。
创建第二个 框 将产生类型'的异常' System.NullReferenceException"。我的错在哪里?
When I use an Site.Master you can create only one Textbox at runtime. The creating of a second boxThe will produce an An exception of type 'System.NullReferenceException'. Where is my fault?
您可以在专注于ASP.Net的论坛中更好地回答您的问题。
You will get a better response to your question in a forum that is focused on ASP.Net.
ASP .Net论坛 (ASP 和
VB for MVC )
ASP.Net forums (ASP and VB for MVC)
这篇关于在VB.NET中创建动态TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!