dynamicform.aspx

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

ASPX
80
字号
<%@ Page Language="C#"%>

<script runat=server>

void Page_Load(Object sender , EventArgs e) 
{
  if (! IsPostBack )
    ViewState[ "ProductFieldCount" ] = 1;
  else
    for ( int intCounter = 2 ; intCounter <= (int)ViewState ["ProductFieldCount"]; intCounter++)
      AddProductField( intCounter.ToString() );
}

void AddProductFieldCount( object s, EventArgs e ) {
  ViewState[ "ProductFieldCount" ] = (int)ViewState[ "ProductFieldCount" ] + 1;
  AddProductField( ViewState[ "ProductFieldCount" ].ToString() );
}

void AddProductField( string  strFieldNum ) {
   LiteralControl litLabel;
   TextBox txtTextBox;

  // 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 );
}

void btnSubmit_Click( object s, EventArgs e ) {
  Response.Redirect( "ThankYou.aspx" );
}

</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 + -
显示快捷键?