如图所示:
出勤开始与结束都是上面ddl下拉框传过来的。
4个下拉拉,数据都是从数据库来的。
如果我在把班次选择为  请选择的时候,就会报错。麻烦大家帮我修改一下代码。
    protected void onduty_SelectedIndexChanged(object sender, EventArgs e)
    {
  
    }
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        string banciID = this.DropDownList3.SelectedValue;
        SqlConnection cononduty = jizhong.createConnection();
        cononduty.Open();
        SqlCommand cmdontudy = new SqlCommand("select * from onduty where banciID='" + banciID + "'", cononduty);
        SqlDataReader sdronduty = cmdontudy.ExecuteReader();
        this.onduty.DataSource = sdronduty;
        this.onduty.DataTextField = "ondutyName";
        this.onduty.DataValueField = "ondutyID";
        this.onduty.DataBind();
        sdronduty.Close();
        cononduty.Close();        string banciID2 = this.DropDownList3.SelectedValue;
        SqlConnection conoffduty = jizhong.createConnection();
        conoffduty.Open();
        SqlCommand cmdofftudy = new SqlCommand("select * from offduty where banciID='" + banciID2 + "'", conoffduty);
        SqlDataReader sdroffduty = cmdofftudy.ExecuteReader();
        this.offduty.DataSource = sdroffduty;
        this.offduty.DataTextField = "offdutyName";
        this.offduty.DataValueField = "offdutyID";
        this.offduty.DataBind();
        sdroffduty.Close();
        cononduty.Close();

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            TextBox cbox1 = (TextBox)GridView1.Rows[i].FindControl("TextBox5");
            TextBox cbox2 = (TextBox)GridView1.Rows[i].FindControl("TextBox6");
            cbox1.Text = this.onduty.SelectedItem.Text.ToString();
            cbox2.Text = this.offduty.SelectedItem.Text.ToString();
        }
    }

解决方案 »

  1.   

    前台加个验证 必选 或者修改代码判断this.onduty.SelectedItem.Text是否是“请选择”,然后写你的逻辑
      

  2.   

    请选择 也给设置个value.
      

  3.   

    请选择   是从数据库里面加载的,也可以设置VALUE吗?
      

  4.   


    //加一个判断
            if (combox.SelectIndex != -1){
                这个时候才读取
    }
      

  5.   

    dropdown默认的时候是没有选择任何项(selectedIndex =-1)的,一开始应该用 findbytext().selected = true 指定选项,同样在使用这一语句指定默认选项前,用clearselection()清除选项。
      

  6.   

    this.onduty.DataSource = sdronduty;
      this.onduty.DataTextField = "ondutyName";
      this.onduty.DataValueField = "ondutyID";
      this.onduty.DataBind();
      sdronduty.Close();
      cononduty.Close();
    this.onduty.ClearSelection();
    this.onduty.FindbyText("请选择").Selected = true;