1. 程式人生 > >ASP.NET動態生成按鈕和事件

ASP.NET動態生成按鈕和事件

ASP.NET動態生成TextBox並獲取其值

動態生成的文字框,生成的是HTML控制元件,所以獲取文字框的值  控時,要注意獲取的方法,不同時間獲取的方法不同.(頁面一架載完時的值 和 控制元件的原有值改變時  兩種情況)

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //建立一個文字框
        TextBox tbxName = new TextBox();
        tbxName.ID = "tbxName";
        tbxName.Text = "OK";
        this.form1.Controls.Add(tbxName);

        //建立一個按鈕
        Control c = ParseControl("<asp:Button Text = '按我' ID = 'myButton' runat = 'server' />");
        this.form1.Controls.Add(c);

        //建立一個按鈕事件    這部份只能放在Page_Load之前。。給按鈕新增事件。
        Button myButton = (Button)Page.FindControl("myButton");
        myButton.Command += new CommandEventHandler(this.myButton_Click);

    }

    protected void myButton_Click(Object sender, EventArgs e)
    {
        //獲取文字框的值
          由於動態生成的文字框,生成的是HTML控制元件,所以獲取文字框的值只能用

            string strTxtValue=Request.form["txtName"].toString()

來獲取,而不能用

            TextBox tb=(TextBox)this.page.form.FindControl("txtName")

            string strTxtValue=tb.Text

來獲取(親身體驗獲取不到更改後的值),因為生成的是HTML控制元件,而不是Web控制元件,tb獲取的值是最初的值
    }
}

另:

 protected void btnSave_Click(object sender, EventArgs e)

{

        ArrayList al = new ArrayList();
        al.Add(txtAname1);        //txtAname1 為TextBox 的 id ,代表 TextBox 型別,不是字串
        al.Add(txtAname2);
        al.Add(txtAname3);
        al.Add(txtAname4);

        string[] txtid = { "txtAname1", "txtAname2", "txtAname3", "txtAname4" };

       #region 修改


            //問題
            mTQI = bTQI .GetModel(int.Parse(Request.QueryString["ID"]));
            mTQI.QName = Request.Form["txtQname"].ToString().Trim();
            mTQI.Qcreatetime = DateTime.Parse(Request.Form["txtQcreatetime"].Trim());
            mTQI.Qa = Request.Form["txtQa"].Trim();
            mTQI.Qislist = Request.Form["DropDownList1"].Trim();
            //mTQI.Qcreatetime = DateTime.Parse(txtQcreatetime.Text.Trim());    //這三行獲取不到改變的值
            //mTQI.Qa = txtQa.Text;
            //mTQI.Qislist = DropDownList1.SelectedValue;

           //答案

            List<JC.Model.t_Answerinfo> lTai2 = new List<JC.Model.t_Answerinfo>();
            DataSet ds = bTAI.GetList("aQid="+Request.QueryString["ID"]+"");
            lTai2 = bTAI.DataTableToList(ds.Tables[0]);
            for (int i = 0; i < lTai2.Count;i++ )
            {
                //TextBox txt = (TextBox)FindControl("txtAname1");           
                string st = Request.Form[txtid[i].ToString()].ToString();
                lTai2[i].Aname = st;
            }
           
            //修改
            numTQ = bTQI.Update(mTQI);
            for (int i = 0; i < lTai2.Count; i++)
            {
                bTAI.Update(lTai2[i]);
            }
            if (numTQ > 0)
            {
                btnSave.Enabled = false;
                ClientScript.RegisterStartupScript(GetType(), "", "<script type='text/javascript'>alert('修改成功');window.location.href='VoteList.aspx';</script>");
            }
            #endregion
        }

 }