(论坛答疑点滴)为什么设置了DropDownList的AutoPostBack="True"还是不能触发SelectedIndexChanged事件?

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

有人问

(1)AutoPostBack="True"

<asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True"></asp:DropDownList>

(2)事件也注册了

this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);

(3)事件也写了

private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)

{

Response.Write(this.DropDownList1.SelectedItem);

}

怎么还是不能输出选定项?进行调试发现不能进入SelectedIndexChanged事件。

其实还有一种可能,就是你为DropDownList的不同option设置了相同的value

比如后台这么写:

if(!IsPostBack)

{

for(int i=0;i<10;i++)this.DropDownList1.Items.Add(new ListItem(i.ToString(),"same_value"));

}

这样不会触发SelectedIndexChanged事件,修改成

if(!IsPostBack)

{

for(int i=0;i<10;i++)this.DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString()));

}

一切些正常,根据msdn的解释:

ListControl.SelectedIndexChanged 事件

当列表控件的选定项在信息发往服务器之间变化时发生

这不同于js的onchange事件,改为

if(!IsPostBack)

{

for(int i=0;i<10;i++)this.DropDownList1.Items.Add(new ListItem(i.ToString(),"same_value"));

this.DropDownList1.Attributes.Add("onchange","alert('test');");

}

测试可知。

if(!IsPostBack)

{

for(int i=0;i<10;i++)this.DropDownList1.Items.Add(new ListItem(i.ToString(),"same_value"));

this.DropDownList1.Attributes.Add("onchange","alert('test');");

}

测试可知。

if(!IsPostBack)

{

for(int i=0;i<10;i++)this.DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString()));

}

一切些正常,根据msdn的解释:

ListControl.SelectedIndexChanged 事件

当列表控件的选定项在信息发往服务器之间变化时发生

这不同于js的onchange事件,改为

if(!IsPostBack)

{

for(int i=0;i<10;i++)this.DropDownList1.Items.Add(new ListItem(i.ToString(),"same_value"));

this.DropDownList1.Attributes.Add("onchange","alert('test');");

}

测试可知。

if(!IsPostBack)

{

for(int i=0;i<10;i++)this.DropDownList1.Items.Add(new ListItem(i.ToString(),"same_value"));

this.DropDownList1.Attributes.Add("onchange","alert('test');");

}

测试可知。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航