close
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) ViewState["Count"] = 1; else { if (ViewState["ADD"] != null && (bool)this.ViewState["ADD"] == true) { AddDropDownList(); } } } //動態增加控制項 protected void btnDemo_Click(object sender, EventArgs e) { ViewState["Count"] = ((int)ViewState["Count"]) + 1; AddDropDownList(); } //取動態增加控制項的值 protected void btntest_Click(object sender, EventArgs e) { //讀出PlaceHolder所有是TextBox子控項的text屬性 string strjs = ""; foreach (Control obj in PlaceHolder1.Controls) { if (obj is System.Web.UI.WebControls.TextBox) { strjs += ((TextBox)obj).Text; } } string js = "alert('" + strjs + "');"; System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, typeof(string), "js", js, true); } //方法 private void AddDropDownList() { PlaceHolder1.Controls.Clear(); //先清除所有子控制項 for (int i = 0; i < (int)ViewState["Count"]; i++) { Label Label = new Label(); Label.ID = "Label" + i.ToString(); //使用流水號取ID Label.Text = "批號:"; PlaceHolder1.Controls.Add(Label); DropDownList DropDownList = new DropDownList(); DropDownList.ID = "DropDownList" + i.ToString(); //使用流水號取ID DropDownList.Items.Insert(0, new ListItem("請選擇", "")); DropDownList.Items.Insert(1, new ListItem("A", "A")); DropDownList.Items.Insert(2, new ListItem("B", "B")); PlaceHolder1.Controls.Add(DropDownList); Label LabelQ = new Label(); LabelQ.ID = "LabelQ" + i.ToString(); //使用流水號取ID LabelQ.Text = "數量:"; PlaceHolder1.Controls.Add(LabelQ); TextBox TextBox = new TextBox(); TextBox.ID = "TextBox" + i.ToString(); //使用流水號取ID TextBox.Text = ""; TextBox.Width = 140; PlaceHolder1.Controls.Add(TextBox); TextBox txtDate = new TextBox(); txtDate.ID = "txtDate" + i.ToString(); //使用流水號取ID txtDate.Text = ""; txtDate.Width = 80; txtDate.CssClass = "Wdate"; txtDate.Attributes.Add("OnFocus", "WdatePicker({dateFmt:'yyy/MM/dd', maxDate:'%y-%M-%d'})"); PlaceHolder1.Controls.Add(txtDate); PlaceHolder1.Controls.Add(newLiteralControl("<br>")); } this.ViewState["ADD"] = true; }
文章標籤
全站熱搜
留言列表