是蠻直接的,不需要再透過JQUERY之類的,像PHP那樣需要再透過JQUERY產生
說明開始
F001.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="F001.aspx.cs" Inherits="F001" %>
F001.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class F001 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { if (this.ViewState["TextBoxAdded"] != null && (bool)this.ViewState["TextBoxAdded"] == true) { AddTextBox(); } } } public int i=0; //動態新增控制項 private void AddTextBox() { //PlaceHolder1.Controls.Clear(); //先清除所有子控制項 TextBox textbox = new TextBox(); i++; textbox.ID = "DynBox" + i ;//重點是要給他一個ID textbox.Text = textbox.ID; PlaceHolder1.Controls.Add(textbox); this.ViewState["TextBoxAdded"] = true; } //事件觸發動態新增控制項 protected void Button1_Click(object sender, EventArgs e) { AddTextBox(); } //取值出來 protected void Button2_Click(object sender, EventArgs e) { TextBox tmpbox = PlaceHolder1.FindControl("DynBox1") as TextBox; Label1.Text = tmpbox.Text; } }
範例下載
如果我要一個按鈕是增加,一個按鈕是減少呢?
回覆刪除這個方法好像沒法達成?