dynamicform.aspx

来自「asp.net技术内幕的书配源码」· ASPX 代码 · 共 81 行

ASPX
81
字号
<Script Runat="Server">

Sub Page_Load
  Dim intCounter As Integer

  If Not IsPostBack Then
    ViewState( "ProductFieldCount" ) = 1
  ElseIf ViewState( "ProductFieldCount" ) > 1 Then
    For intCounter = 2 To ViewState( "ProductFieldCount" )
      AddProductField( intCounter )
    Next
  End If
End Sub

Sub AddProductFieldCount( s As Object, e As EventArgs )
  ViewState( "ProductFieldCount" ) += 1
  AddProductField( ViewState( "ProductFieldCount" ) )
End Sub

Sub AddProductField( strFieldNum As String )
  Dim litLabel As LiteralControl
  Dim txtTextBox As TextBox

  ' Add Literal Control
  litLabel = New LiteralControl
  litLabel.Text = "<p><b>Product " & strFieldNum & ":</b> "
  plhProductFields.Controls.Add( litLabel )

  ' Add TextBox Control
  txtTextBox = New TextBox
  txtTextBox.ID = "txtProduct" & strFieldNum
  plhProductFields.Controls.Add( txtTextBox )
End Sub

Sub btnSubmit_Click( s As Object, e As EventArgs )
  Response.Redirect( "ThankYou.aspx" )
End Sub

</Script>

<html>
<head><title>DynamicForm.aspx</title></html>
<body>

<form Runat="Server">

<b>Customer Name:</b>
<asp:TextBox
  ID="txtCustomer"
  Runat="Server" />

<p>
<div style="background-color: yellow;padding:10px">

<b>Product 1:</b>
<asp:TextBox
  ID="txtProduct1"
  Runat="Server" />

<asp:PlaceHolder
  id="plhProductFields"
  Runat="Server" />

</div>

<p>
<asp:Button
  Text="Add Product Field"
  OnClick="AddProductFieldCount"
  Runat="Server" />

<asp:Button
  id="btnSubmit"
  Text="Submit Complete Order"
  OnClick="btnSubmit_Click"
  Runat="Server" />

</form>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?