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);
}
解决方案 »
- 大家都 在用什么web报表(能输入能打印,可自定义公式)。
- 求助,一隐秘文件地址被百度公布
- 2003转到2005的问题
- 我相信连接每位高手都会配置Web.config文件,用Access数据库时,Web.config这样配置为什么不对???
- 动态生成ImageButton控件,怎么click事件没有响应?
- 请教,在.net中,COM到底还有什么用?
- 这个正则[0-9]*.[0-9]{0,2}为啥不行?
- 关于打开文件的问题,急,帮帮忙吧
- 关于图片加上水印的问题,速结帖
- 关于$(document).ready(function(){}) 一个容易被忽略的问题
- 关于动态链接控件传值无效的问题,求高手解答!
- 服务区端怎么调用客户端的JS
so.NumSQL(sql);
so.NumSQL(sql);
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语句中把两个字段的位置对调一下。
DropDownList.Items[DropDownList.SelectedItem].Text
但是不管DropDownList1立了选择哪项,DropDownList2显示的总是同样的一项
{
Dd2DataBind();
}你这样绑定的,当然总是显示相同的一项了
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);
}
DropDownList.Items[DropDownList.SelectedItem].Text
你绑定的问题显示一样的!