有三个dropdownlist 都是可回发的
第一个DropDownList1_SelectedIndexChanged后会过滤绑定DropDownList2的内容,
但我点击DropDownList2时 为什么又执行了一遍DropDownList1_SelectedIndexChanged然后才执行DropDownList2_SelectedIndexChanged 我点DropDownList2就应执行DropDownList2_SelectedIndexChanged
为什么还要执行DropDownList1_SelectedIndexChanged请问如何才能让点DropDownList2不执行DropDownList1_SelectedIndexChanged

解决方案 »

  1.   

    给DropDownList1绑定数据的方法要写在protected void Page_Load(...)
    if(!Page.IsPostBack)
    {
      //这里
    }btw:楼主你真是"文不加点"啊
      

  2.   

    你最好把你要调用的事件,或者你写在Page_load事件中的代码放在
    if(!IsPostBack)
    {
       //here
    }要不然,不仅仅是你点击AutoPostBack属性为true的DropDownList时会重新执行一遍.你单击任何一个按钮也要执行,只要回发到服务器的都要执行.
    这样的问题问的太多了.
      

  3.   

    除了
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            InitData();//第一次请求时绑定初始化的数据,
        }之外你的问题的第一句话就是答案
      

  4.   

    同意,web程序和windows程序,这点上区别很大。IsPostBack是关键。
      

  5.   

    我是写在isPostBack里了,对了我所有的DropDownList都放在一个</asp:UpdatePanel>里了
    现在问题就是当我选一下BindDropDownList1它回发后执行DropDownList1_SelectedIndexChanged
    当我选一下DropDownList2它回发后还要执行一下DropDownList1_SelectedIndexChanged,然后又执行DropDownList2_SelectedIndexChanged
    选DropDownList2后应只执行DropDownList2_SelectedIndexChanged啊,请问为什么,如何解决
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropDownList1();
                BindDropDownList2();
                BindDropDownList3();
            }    }    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList dropDownList = sender as DropDownList;
            if (dropDownList.ID == "DropDownList2")
            {
                BindDropDownList3();
            }
        }    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList dropDownList = sender as DropDownList;
            if (dropDownList.ID == "DropDownList1")
            {
                BindDropDownList2();
                BindDropDownList3();
            }
            
        }//代码都不用看意思就是绑定第1个DropDownList
        private void BindDropDownList1()
        {
            Dictionary<String, String> hashtable = new Dictionary<string, string>();
            hashtable.Add("0", "=选择分类=");
            hashtable.Add("1", "AAA");
            hashtable.Add("2", "BBB");
            hashtable.Add("3", "CCC");        DropDownList1.DataSource = hashtable;
            DropDownList1.DataTextField = "value";
            DropDownList1.DataValueField = "key";
            DropDownList1.DataBind();
        
        }//代码都不用看意思就是绑定第2个DropDownList
        private void BindDropDownList2()
        {
            String tableName = DropDownList1.SelectedValue; 
            String sqlString = "";        if (tableName == "0")
            {
                Dictionary<String, String> hashtable = new Dictionary<string, string>();
                hashtable.Add("0", "=全部=");            DropDownList2.DataSource = hashtable;
                DropDownList2.DataTextField = "value";
                DropDownList2.DataValueField = "key";
                DropDownList2.DataBind();
            }        if (tableName == "1")
            {
                sqlString = "***********************";            DataTable dt = _boBase.GetDataTable(sqlString);
                DropDownList2.DataSource = dt;
                DropDownList2.DataTextField = "AAA";
                DropDownList2.DataValueField = "ID";
                DropDownList2.DataBind();            ListItem listItem = new ListItem();
                listItem.Value = "0";
                listItem.Text = "=全部=";
                DropDownList2.Items.Insert(0, listItem);
            }
           
            
        }
    //就是绑定第3个DropDownList
        private void BindDropDownList3()
        { 
            String tableName = DropDownList1.SelectedValue; 
            String sqlString = "";
            
            if (tableName == "0")
            {
                    Dictionary<String, String> hashtable = new Dictionary<string, string>();
                    hashtable.Add("0", "=全部=");                DropDownList3.DataSource = hashtable;
                    DropDownList3.DataTextField = "value";
                    DropDownList3.DataValueField = "key";
                    DropDownList3.DataBind();
                
            }
            if (tableName == "1")
            {
                String drpSelect = DropDownList2.SelectedValue;            if (drpSelect == "0")
                {
                    Dictionary<String, String> hashtable = new Dictionary<string, string>();
                    hashtable.Add("0", "=全部=");                DropDownList3.DataSource = hashtable;
                    DropDownList3.DataTextField = "value";
                    DropDownList3.DataValueField = "key";
                    DropDownList3.DataBind();
                
                }
           
        }
      

  6.   

    有这样的多选择,一定记得要通过判断是否页面是否回传过来的。
    if(!Page.IsPostBack)
    {}
      

  7.   

    因为回传时刷新了页面,刷新后DropDownList1重新加载自己的列表项,所以DropDownList1_SelectedIndexChanged事件又被触发。
      

  8.   

     if (!IsPostBack)
            {
                //绑定出激活的管理站,下的干支渠
                DAL.T_WaterBalanceDSTableAdapters.ganz_tabTableAdapter adp = new DAL.T_WaterBalanceDSTableAdapters.ganz_tabTableAdapter();
                this.ddlganzname.DataSource = adp.GetDataGanzName();
                this.ddlganzname.DataTextField = "ganz_mic";
                this.ddlganzname.DataValueField = "ganz_bih";
                this.ddlganzname.DataBind();
            }
     protected void ddlganzname_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlduanmane.Items.Clear();
            this.ddlduanmane.Items.Add("全部");
            DAL.T_WaterBalanceDSTableAdapters.duan_tabTableAdapter adp = new DAL.T_WaterBalanceDSTableAdapters.duan_tabTableAdapter();
            this.ddlduanmane.DataSource = adp.GetDataDuanName(this.ddlganzname.SelectedValue);
            this.ddlduanmane.DataTextField = "duan_mic";
            this.ddlduanmane.DataValueField = "duan_bih";
            this.ddlduanmane.DataBind();
        }
        protected void ddlduanmane_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddldouname.Items.Clear();
            this.ddldouname.Items.Add("全部");
            DAL.T_WaterBalanceDSTableAdapters.douq_tabTableAdapter adp = new DAL.T_WaterBalanceDSTableAdapters.douq_tabTableAdapter();
            this.ddldouname.DataSource = adp.GetDataDouqName(this.ddlduanmane.SelectedValue);
            this.ddldouname.DataTextField = "douq_mic";
            this.ddldouname.DataValueField = "douq_bih";
            this.ddldouname.DataBind();
        }
        protected void ddlxangname_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlcunname.Items.Clear();
            this.ddlcunname.Items.Add("全部");
            DAL.T_WaterBalanceDSTableAdapters.cunz_tabTableAdapter adp = new DAL.T_WaterBalanceDSTableAdapters.cunz_tabTableAdapter();
            this.ddlcunname.DataSource = adp.GetDataCunName(this.ddlxangname.SelectedValue);
            this.ddlcunname.DataTextField = "cunz_mic";
            this.ddlcunname.DataValueField = "cunz_bih";
            this.ddlcunname.DataBind();
        }
        protected void ddlcunname_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlzuname.Items.Clear();
            this.ddlzuname.Items.Add("全部");
            DAL.T_WaterBalanceDSTableAdapters.zubi_tabTableAdapter adp = new DAL.T_WaterBalanceDSTableAdapters.zubi_tabTableAdapter();
            this.ddlzuname.DataSource = adp.GetDataZuname(this.ddlcunname.SelectedValue);
            this.ddlzuname.DataTextField = "zubi_mic";
            this.ddlzuname.DataValueField = "zubi_bih";
            this.ddlzuname.DataBind();
        }
        protected void ddlguanjiyear_SelectedIndexChanged(object sender, EventArgs e)
        {        //在这里 SelectedIndex =0 会把"---"这个作为参数 来查询灌季名称,会提示"---"无法转换成int行,  ---为字符窜,
            
            if (ddlguanjiyear.SelectedIndex > 0)
            {
                this.ddlguanjiname.Items.Clear();
                this.ddlguanjiname.Items.Add("全部");
                DAL.T_WaterBalanceDSTableAdapters.guaj_tabTableAdapter adp = new DAL.T_WaterBalanceDSTableAdapters.guaj_tabTableAdapter();
                this.ddlguanjiname.DataSource = adp.GetDatayearName(this.ddlguanjiyear.SelectedValue);
                this.ddlguanjiname.DataTextField = "guaj_mic";
                this.ddlguanjiname.DataValueField = "guaj_mic";
                this.ddlguanjiname.DataBind();
            }
        }
    举个例子
      

  9.   


    每个DropDownList放在不同的UpdatePanel里试试
      

  10.   

    嗯,应该不用放几个UpdatePanel的.我做了试验了你检查一下DropDownList1的EnableViewState属性是不是true,要设置成true
      

  11.   

    To 风中聆听
    设成true了,还是没解决问题