asp.net動態增加伺服器端控制項並提交表單

來源:互聯網
上載者:User

標籤:head   src   new   ace   目的   tar   alt   問題   new t   

為什麼要用原生的呢?

1、目的 原生出現瀏覽器安全色性問題 極少,不用測試多瀏覽相容性 。需要考慮到市面上的其他垃圾瀏覽器。 

2、效能不好 如果不考慮第一條 你可以換一種方式

 

直接上代碼

.aspx頁面

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title>DEMO</title></head><body>    <form id="form1" runat="server">                <br />        <br />        <asp:Label ID="Label2" runat="server" Text="動態控制項"></asp:Label>        <asp:PlaceHolder ID="phBody" runat="server"></asp:PlaceHolder>        <br />        <br />        <br />        <br />        <br />        <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="加三個動態控制項" />        <asp:Button ID="btnDelLast" runat="server" OnClick="btnDelLast_Click" Text="刪除後面三個" />        <br />        <br />        <br />        <br />        <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="提交儲存" />        <br />        <br />        <asp:Label runat="server" ID="lbMsg" ForeColor="Red"></asp:Label>    </form></body></html>

  

後台.cs代碼

  

  protected void Page_Load(object sender, EventArgs e)        {            #region 初始化控制項             if (ViewState["AddedControl"] == null)            {                ViewState["AddedControl"] = 1;                CreateControl();            }            else            {                int cout = Convert.ToInt32(ViewState["AddedControl"]);                for (int i = 0; i < cout; i++)                {                    CreateControl();                }            }            #endregion            if (!IsPostBack)            {            }        }        /// <summary>        /// 給容器phBody建立動態控制項        /// </summary>        /// <param name="index"></param>        public void CreateControl()        {            Panel plBody = new Panel();            TextBox txt = new TextBox();            txt.Text = "";            plBody.Controls.Add(txt);            TextBox txt2 = new TextBox();            txt2.Text = "";            plBody.Controls.Add(txt2);            TextBox txt3 = new TextBox();            txt3.Text = "";            plBody.Controls.Add(txt3);                          phBody.Controls.Add(plBody);        }        //擷取動態添加的控制項值        protected void btnSave_Click(object sender, EventArgs e)        {            System.Text.StringBuilder sb = new System.Text.StringBuilder();            for (int i = 0; i < phBody.Controls.Count; i++)            {                Control ctrl = phBody.Controls[i];                for (int j = 0; j < ctrl.Controls.Count; j++)                {                    if (j <= 3)                    {                        TextBox txt = ctrl.Controls[j] as TextBox;                        if (txt != null)                        {                            sb.Append(txt.Text);                        }                    }                }            }            lbMsg.Text = sb.ToString();        }        //加三行        protected void btnAdd_Click(object sender, EventArgs e)        {            if (ViewState["AddedControl"] == null)            {                ViewState["AddedControl"] = 1;            }            else            {                int cout = Convert.ToInt32(ViewState["AddedControl"]);                ViewState["AddedControl"] = cout + 1;            }            CreateControl();        }        //刪除最後一行動態添加的控制項        protected void btnDelLast_Click(object sender, EventArgs e)        {            int index = phBody.Controls.Count - 1;            phBody.Controls.RemoveAt(index);            if (ViewState["AddedControl"] == null)            {                ViewState["AddedControl"] = 0;            }            else            {                int cout = Convert.ToInt32(ViewState["AddedControl"]);                ViewState["AddedControl"] = cout - 1;            }        }

  

 

預覽效果

 

asp.net動態增加伺服器端控制項並提交表單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.