jag hittade den här koden på www.asp.net.
Men jag lyckas inte få det att fungera ihop med min kod
<%@ Page Language="VB" %>
<script runat="server">
Public Const iNumOfButtons = 3 'This is the original number of hardcoded buttons
Sub Page_Load(s As Object, e As EventArgs)
If IsPostBack Then
'Controls are not preserverd for a postback so you have to rebuild them
If ViewState("iNum") <> Nothing Then
Dim i as integer
For i = 1 to ViewState("iNum")
AddTextBox(i)
Next
End If
End If
End Sub
Sub btnAdd_Click(s As Object, e As EventArgs)
ViewState("iNum") +=1
AddTextBox(ViewState("iNum"))
End Sub
Sub AddTextBox(i As Integer)
Dim txtNew As New Textbox()
Dim ltlStart As New LiteralControl("Textbox " & (i+iNumOfButtons) & " ")
Dim ltlEnd As New LiteralControl("<p>")
txtNew.id="txt" & (i+iNumOfButtons)
plhMore.Controls.Add(ltlStart)
plhMore.Controls.Add(txtNew)
plhMore.Controls.Add(ltlEnd)
End Sub
Sub btnFinish_Click (s As Object, e As EventArgs)
'Hardcoded Textboxes
lblRes.Text = "txt1=" & txt1.Text & "<br>"
lblRes.Text &= "txt2=" & txt2.Text & "<br>"
lblRes.Text &= "txt3=" & txt3.Text & "<br>"
'Dynamic textboxes
If ViewState("iNum") <> Nothing Then
Dim i as integer
Dim iExtraBtns as Integer = ViewState("iNum") + iNumOfButtons
For i = (iNumOfButtons+ 1) to iExtraBtns
lblRes.Text &= "txt" & i.Tostring() & "=" & Request.Params("txt" & i.ToString()) & "<br>"
Next
End If
End Sub
</script>
<html>
<head>
<title> New ASP.NET Document </title>
</head>
<body>
<form runat="server" method="">
TextBox 1 <asp:TextBox id="txt1" runat="server" /> <p>
TextBox 2 <asp:TextBox id="txt2" runat="server" /> <p>
TextBox 3 <asp:TextBox id="txt3" runat="server" /> <p>
<asp:PlaceHolder id="plhMore" runat="server" />
<asp:Button id="btnAdd" Text="AddTextBox" onclick="btnAdd_Click" runat="server" /> <asp:Button id="btnFinish" Text="Finish" onclick="btnFinish_Click" runat="server" />
--------------------------------------------------------------------------------
<asp:Label id="lblRes" runat="server"/>
</form>
</body>
</html>
Vet någon hur jag ska göra för att få detta att fungera?