曾经遇到过这个问题,后来在LoveCherry的博客中找到了更详细的解释。
该文地址:http://lovecherry.cnblogs.com/archive/2005/04/26/145705.html
原文如下:
有人问
(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');");
}
测试可知。
[/color][color=black]
分享到:
相关推荐
DataList中如何响应DropDownList的SelectedIndexChanged事件
AutoPostBack="True" </asp:DropDownList> <span lang="zh-cn">市:</span><asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" </asp:DropDownList> <span lang=...
很多时候会出现这样的情况: 1、选择下拉列表中的某一...2、当选择一项之后无法执行SelectedIndexChanged事件。 其实很可能只是一个简单的问题,大家照着修改下试试,希望能帮到一点点小忙,这也是我之前遇到的问题。
%>' OnSelectedIndexChanged="ddl_e_week_p_SelectedIndexChanged" AutoPostBack="true"runat="server" Width="100px"> </asp:DropDownList> <asp:Label ID="lb_week_p_edit" Visible="false" runat="server" Text=...
详细介绍DROPDOWNLIST在数据网格中的更新应用.
dropdiwnlist的方法重写,实现listbox下拉框制定的值实现只读、不可选择的技巧(重写DROPDOWNLIST)
代码: <asp ID=ScriptManager1 runat=server> //ASP.NET中使用UpdatePanel实现局部异步刷新 ...asp:DropDownList runat=server ID=SelectProductType AutoPostBack=True OnSelectedIndexChanged=SelectProductTypeC
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { if (this.DropDownList1.SelectedIndex == 0) { this.DropDownList2.Items.Clear(); this.DropDownList2.Items.Add("南阳...
DropDownList既能选择又能输入
DropDownList 的常用属性和事件 asp.net 内含详细示例
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e) { Image1.Visible = true; { int i = DropDownList1.SelectedIndex; switch (i) { case 0: Image1.ImageUrl = "~/images/...
添加完这个事件还不能达到目的,还要设置 AutoPostBack 属性为 false,不让它回发后台事件。 以下就是为大家分享的代码: <html xmlns=http://www.w3.org/1999/xhtml > <head runat=server> <...
DropDownList
DetailsView控件里的Dropdownlist控件添加点击事件代码与分析
DropDownList事件更改显示图片.rar
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Width="98px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList> <div align=left style="position:...
由于html的Select元素式样不更改,自已用Javascript写了DropDownList控件,支持分组.调用相当方便,不用更改原来的HTML结构.调用方法:DropDownList.bind(selectId).例如: --------------------------------- <div style...
主要介绍了DropDownList设置客户端事件思路,需要的朋友可以参考下
常见的解决方法是将带有层次的数据写入XML,然后设置DropDownList的AutoPostBack属性为”True”开启自动回调,最后处理SelectedIndexChanged事件。这样不仅十分麻烦,过多的页面刷新会给用户带来反感。那么如何实现...
在ASP.NET 2.0 网站页面的开发过程中,经常需要把DropDownList等列表类控件的SelectedValue值设置为一个从数据库或其他地方读取出来的值。 最简单的办法就是直接进行指定: DropDownList1.SelectedValue = "中国"; ...