动态生成控制项

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //动态生成控制项的预设值
            DropDownList ddl = ( DropDownList)this.PlaceHolder1.FindControl("ddlChoose") ;
            ListItem li;
            li = new ListItem("...select", "0");
            ddl.Items.Add(li);
        }
    }
    protected void Page_Init(object sender, EventArgs e)
    {
        //动态生成控制项的程式码
        DropDownList dropdownlist = new DropDownList();
        dropdownlist.ID = "ddlChoose";
        this.PlaceHolder1.Controls.Add(dropdownlist);
    }
}

你可能感兴趣的:(动态生成控制项)