我有一个与sqlDataSource和TextBox连接的DropDownList。 我想每次用户从DropDownList上列出的列名中选择一个名称, 显示在TextBox文本
上的该项的列id的值我制作了这段代码,但似乎不起作用: (代码中没有错误)
protected void DropDownListIliaka_SelectedIndexChanged(object sender, EventArgs e)
{
string conString = "Data Source=icsd-db.aegean.gr\\icsdmssqlsrv;Initial Catalog=icsd12015;Integrated Security=True;";
SqlConnection con = new SqlConnection(conString);
string cmdText = "SELECT iliako_sistima_ID FROM iliako_sistima WHERE name = '" + DropDownListIliaka.Text + "'";
SqlCommand cmd = new SqlCommand(cmdText, con);
try
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
iliako_sistima_id.Text = (reader["Iliako_Sistima_ID"].ToString());
}
}
}
finally
{
con.Close();
}
}
答案 0 :(得分:2)
这个
string cmdText = "SELECT iliako_sistima_ID FROM iliako_sistima WHERE name = '" + DropDownListIliaka.Text + "'";
应该是
string cmdText = "SELECT iliako_sistima_ID FROM iliako_sistima WHERE name = '" + DropDownListIliaka.SelectedItem.Text + "'";
希望这有帮助
答案 1 :(得分:0)
您必须将DropDownList的属性AutoPostBack设置为true,因此它会回发到服务器并设置textbox的值