dropdownlist选择后向数据库提交数据不是部门名而是ID,请各位大侠看看哪儿错了
 <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div style="float: left" mce_style="float: left">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" Width="90px" AutoPostBack="true"
                        OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
                    </asp:DropDownList>
                    <asp:DropDownList ID="DropDownList2" runat="server" Width="110px">
                    </asp:DropDownList>
                    <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1"
                        DataTextField="duty_name" DataValueField="duty_name">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:new_cqConnectionString %>"
                        SelectCommand="SELECT [duty_name] FROM [c_duty] WHERE ([s] = @s)">
                        <SelectParameters>
                            <asp:Parameter DefaultValue="true" Name="s" Type="Boolean" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </div>前台的
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Dd1DataBind();
            Dd2DataBind();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Dd2DataBind();
    }
    public void Dd1DataBind()
    {
        string sqlStr = "Select id,unit_sname From c_unit where unit_sb='0'and s='1' ";
        FillDropList(sqlStr, DropDownList1);
    }    public void Dd2DataBind()
    {
        int PreID = Convert.ToInt32(DropDownList1.SelectedValue);
        string sqlStr1 = "Select unit_id,unit_sname From c_unit Where unit_id=' " + PreID.ToString() + "'";
        FillDropList(sqlStr1, DropDownList2);
    }    public void FillDropList(string SQLString, DropDownList drp)
    {
        SqlConnection connection = SqlOperation.GetConnection();
        SqlCommand cmd = new SqlCommand(SQLString, connection);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds, "c_unit");
        drp.DataSource = ds.Tables["c_unit"].DefaultView;
        
        drp.DataTextField = ds.Tables["c_unit"].Columns[1].ColumnName;
        drp.DataValueField = ds.Tables["c_unit"].Columns[0].ColumnName;
        drp.DataBind();
    }
protected void Button1_Click(object sender, EventArgs e)
    {
string sql="insert into t_salary (department)values('" + DropDownList1.Text.ToString()+ "''" + DropDownList2.Text.ToString() + "')";
so.NumSQL(sql);
}

解决方案 »

  1.   

    string sql="insert into t_salary (department)values('" + DropDownList1.SelectValue.ToString()+ "''" + DropDownList2.SelectValue.ToString() + "')";
    so.NumSQL(sql);
      

  2.   

    string sql="insert into t_salary (department)values('" + DropDownList1.SelectedValue.ToString()+ "''" + DropDownList2.SelectedValue.ToString() + "')"; 
    so.NumSQL(sql); 
      

  3.   

    你要显示部门名称就用DropDownList1.SelectItem.Text.ToString()
      

  4.   

    Select id,unit_sname From c_unit 
    Select unit_id,unit_sname From c_unit drp.DataTextField = ds.Tables["c_unit"].Columns[1].ColumnName;
    drp.DataValueField = ds.Tables["c_unit"].Columns[0].ColumnName;
    以上是你的sql语句和部分代码片段,由此推断,你后台获取的下拉列表的值应该是Id。如果你想要获取部门名称,可以在sql语句中把两个字段的位置对调一下。
      

  5.   

    DropDownList.SelectedItem.Text 
    DropDownList.Items[DropDownList.SelectedItem].Text
      

  6.   

    改了之后,DropDownList1显示的是正常了
    但是不管DropDownList1立了选择哪项,DropDownList2显示的总是同样的一项
      

  7.   

      protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Dd2DataBind();
        }你这样绑定的,当然总是显示相同的一项了
      

  8.   

    怎么看代码越看越像我写的啊
      protected void Page_Load(object sender, EventArgs e)
        {        if (!IsPostBack)
            {
                //绑定第一个下拉框
                Dd1DataBind();
                //显示第一个下拉框对应的第二个下拉框的内容
                Dd2DataBind();
                //显示第二个下拉框对应的第三个下拉框的内容
                Dd3DataBind();
            }
        }
        //当下拉框改变时,显示相应的内容
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Dd2DataBind();
            Dd3DataBind();
        }
        //当下拉框改变时,显示相应的内容
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Dd3DataBind();
        }
        public void Dd1DataBind()
        {
            DBhelp dd = new DBhelp();
            string sqlStr = "Select TypeID,TypeName From a ";
           dd.FillDropList(sqlStr, DropDownList1);
        }
        public void Dd2DataBind()
        {
            DBhelp ddd = new DBhelp();
            int PreID = Convert.ToInt32(DropDownList1.SelectedValue);
            string sqlStr1 = "Select ID,name From b Where TypeID=' " + PreID.ToString() + "'";
            ddd.FillDropList(sqlStr1,DropDownList2);
        }
        public void Dd3DataBind()
        {
            DBhelp dddd = new DBhelp();
            int PreID = Convert.ToInt32(DropDownList2.SelectedValue);
            string sqlStr2 = "Select IDcun,name From c Where ID=' " + PreID.ToString() + "'";
            dddd.FillDropList(sqlStr2,DropDownList3);
        }
      

  9.   

    DropDownList.SelectedItem.Text 
    DropDownList.Items[DropDownList.SelectedItem].Text 
    你绑定的问题显示一样的!