ASP.NET動態產生控制項

後端網頁代碼.cs
protected System.Web.UI.WebControls.Button bt;
protected System.Web.UI.WebControls.FileUpload fu;

private void Page_Load(object sender, System.EventArgs e)
{
    //根據網址列參數產生上傳檔案物件
    if (Request["Type"] != null && 
Information.IsNumeric(Request["Type"]) && 
Convert.ToInt32(Request["Type"]) >= 1 && 
Convert.ToInt32(Request["Type"]) <= 3)
    {
        for (int i = 1; i <= Convert.ToInt32(Request["Type"]); i++)
        {
            System.Web.UI.WebControls.PlaceHolder objPlaceHolder =
 (System.Web.UI.WebControls.PlaceHolder)this.FindControl(string.Format("PlaceHolder{0}", i));
            fu = new FileUpload();
            fu.ID = string.Format("FileUpload{0}", i);
            objPlaceHolder.Controls.Add(fu);
        }
        //產生按鈕及宣告按鈕事件
        bt = new Button();
        bt.Click += new System.EventHandler(this.Button_Click);
        PlaceHolder1.Controls.Add(bt);

    }

}
private void Button_Click(object sender, System.EventArgs e)
{
    Response.Write("ClickBtn");
}
前端網頁代碼.aspx



留言