zoukankan      html  css  js  c++  java
  • DropDownList 控件不能触发SelectedIndexChanged 事件

    相信DropDownList 控件不能触发SelectedIndexChanged 事件已经不是什么新鲜事情了,原因也无外乎以下几种:

    1、DropDownList 控件的属性 AutoPostBack="True" 没有写;

    2、DropDownList 控件的数据绑定没有放在if (!Page.IsPostBack) 里面;

    3、DropDownList 控件选定项的value 值只有在发生变化时,才将信息发往服务器;

    有人问
    (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');");
                }

    测试可知。

  • 相关阅读:
    我国教育技术期刊主要栏目的内容分析
    是互动还是告状 “家校通”通往何处?(转)
    美国高中的班级管理制度
    什么是决策支持系统?
    2009 AECT International Convention
    AECT94定义和AECT2005定义
    感受美国小学生的幸福校园生活! (转)
    教育管理信息系统的研究
    教学评价的新发展
    抽象方法与虚方法(转)
  • 原文地址:https://www.cnblogs.com/xiaoma-qi/p/5508407.html
Copyright © 2011-2022 走看看