我想让我的DropDownList 每隔一秒index自动加一.
private void Button1_Click(object sender, System.EventArgs e)
{
wxyt w=new wxyt ();
Thread t=new Thread (new ThreadStart(w.bofang));
t.Start();
w.bofang();
} void bofang()
{
while(true)
{
    //Thread.Sleep(1000);
this.DropDownList1.SelectedIndex+=1;
if(this.DropDownList1.SelectedIndex==23)
{
  this.DropDownList1.SelectedIndex=0;
}
}
}
这样报错说是未处理的“System.NullReferenceException”类型的异常出现在 未知模块 中。其他信息: 未将对象引用设置到对象的实例。
大家帮帮忙,或者有没有更好的办法.this.DropDownList1.SelectedIndex+=1;会不会触发DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)  谢谢大家!!!!

解决方案 »

  1.   

    ASP.NET中,不要使用多线程操作UI
      

  2.   

    如果你了解Web机制,就会知道你的代码是不可能实现的。
      

  3.   

    当你的线程在运行时,服务器已经把HTML发到了客户端,所有的控件也已经不存在的,调用this.DropDownList1当然是null.建议你使用javascript在客户端实现这个功能
      

  4.   

    this.DropDownList1.SelectedIndex+=1;会触发DropDownList1_SelectedIndexChangedthis.DropDownList1.SelectedIndex+=1; 
    这句要加个判断,是否存在你加1后的索引项
    if(this.DropDownList1.SelectedIndex==23) //这里也要注意是否超出索引

      this.DropDownList1.SelectedIndex=0; 
    }
    else
    {
      this.DropDownList1.SelectedIndex+=1;
    }
      

  5.   

    同意二楼的说法,ASP。NET中不适合用线程,C/S可以考虑用,很少B/S用多线程的
      

  6.   

     <script type="text/javascript" language="javascript">
        var index=0;
        function dropDownChange()
        {
            var drop=document.getElementById("DropDownList1");
           // var showDiv=document.getElementById("showContent");    
            if(index==下拉列表的数量)
            {
             index=0;
            }
            else{
                index++;
            }
            //showContent.innerText=showContent.innerText+" "+drop.options[index].value;
            
            window.setInterval(dropDownChange,10000);
        }
          
        </script>
      

  7.   

    能详细一点吗,JS我不是太懂,我用7楼的代码不行呀!! 我的DropDownList是WEB控件