标签:blog http 使用 os strong for art ar
根据Asp.NET的机制,在html markup有写DropDownList控件与动态加载的控件有点不一样。如果把DropDownList控件写在html markup,即.aspx网页,网友的代码是没有问题的。但是,如果是在.aspx.cs动态加载的控件,那就不能使用Page_Load事件内加载,如果以此加载,就算是使用AutoPostBack = true也没有效果。
正确做法是在Page_Init事件中加载。
下面动态实时操作中,首先是运行网友的代码,确定无法运行。然后Insus.NET把Page_Load事件以及Showpage方法Comment out之后,并把动态产生DropDownList控件拷贝至page_Init事件。最后奇迹发生,能正确运行了。
演示中的代码:
protected void Page_Init(object sender, EventArgs e) { DropDownList ddl = new DropDownList(); ddl.AutoPostBack = true; ddl.Items.Add(new ListItem("国家", "c")); ddl.Items.Add(new ListItem("中國", "c1")); ddl.Items.Add(new ListItem("美國", "c2")); ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged); this.Form.Controls.Add(ddl); } protected void Page_Load(object sender, EventArgs e) { //if (!IsPostBack) //{ // ShowPage(); //} } //public void ShowPage() //{ // DropDownList ddl = new DropDownList(); // ddl.AutoPostBack = true; // ddl.Items.Add(new ListItem("国家", "c")); // ddl.Items.Add(new ListItem("中國", "c1")); // ddl.Items.Add(new ListItem("美國", "c2")); // ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged); // this.Form.Controls.Add(ddl); //} protected void ddl_SelectedIndexChanged(object sender, EventArgs e) { DropDownList dl = (DropDownList)sender; Response.Write("<script>alert(‘" + dl.SelectedValue + "‘)</script>"); }
protected void Page_Init(object sender, EventArgs e) { DropDownList ddl = new DropDownList(); ddl.AutoPostBack = true; ddl.Items.Add(new ListItem("国家", "c")); ddl.Items.Add(new ListItem("中國", "c1")); ddl.Items.Add(new ListItem("美國", "c2")); ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged); this.Form.Controls.Add(ddl); } protected void Page_Load(object sender, EventArgs e) { //if (!IsPostBack) //{ // ShowPage(); //} } //public void ShowPage() //{ // DropDownList ddl = new DropDownList(); // ddl.AutoPostBack = true; // ddl.Items.Add(new ListItem("国家", "c")); // ddl.Items.Add(new ListItem("中國", "c1")); // ddl.Items.Add(new ListItem("美國", "c2")); // ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged); // this.Form.Controls.Add(ddl); //} protected void ddl_SelectedIndexChanged(object sender, EventArgs e) { DropDownList dl = (DropDownList)sender; Response.Write("<script>alert(‘" + dl.SelectedValue + "‘)</script>"); }
DropDownList 控件的SelectedIndexChanged事件触发不了,布布扣,bubuko.com
DropDownList 控件的SelectedIndexChanged事件触发不了
标签:blog http 使用 os strong for art ar
原文地址:http://www.cnblogs.com/lschenblog/p/3894716.html