具有datavaluefield常见值的下拉问题 [英] Dropdown problem with common values of datavaluefield
本文介绍了具有datavaluefield常见值的下拉问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我有一个数据源的下拉列表,如
empname dept
-----------------
Rahul 02
martin 01
sachin 02
我把这个表绑定了到我的下拉列表,当我尝试访问dept中的下拉选择事件更改每次我只获得Rahul。即使我选择了萨钦。
问题是什么?我假设我的数据值字段(02和02)中有两个常见值是一个问题吗?
请检查下面显示的代码..
Hi I've a dropdown list with datasource like
empname dept
-----------------
Rahul 02
martin 01
sachin 02
i've binded this table to my dropdown list and when i try to access dept in dropdown selected event change every time I'm getting Rahul only. even i selected sachin.
Whats the problem? What I assume is I've two common values in my datavalue field (02 and 02) is this a problem?
Please check my code shown below..
if (!IsPostBack)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("toget_emp", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
ddlReason.DataSource = ds.Tables[0];
ddlReason.DataTextField = "empname";
ddlReason.DataValueField = "dept";
ddlReason.DataBind();
ddlReason.Items.Insert(0, "--Select--");
}
}
catch (SqlException ex)
{
ScriptManager.RegisterStartupScript(this, GetType(), "alert", "javascript:alert('" + ex.Message + "')", true);
}
finally
{
con.Close();
}
}
推荐答案
写这样的代码
ddlReason.DataBind();
ddlReason.Items.Insert(0, - Select - ;);
ddlReason.Items [0] .value =0;
Write code like this
ddlReason.DataBind();
ddlReason.Items.Insert(0, "--Select--";);
ddlReason.Items[0].value="0";
是的,这可能是问题,但您可以使用DropDownList的SelectedIndex属性并在DropDownList中捕获选定的值和文本onselectedindexchanged = ddlReason_SelectedIndexChanged获取所选文本字段,即使您在值字段中具有相同的值,但建议在DataValueField中使用唯一值。
Yes ,It would be the problem but you can use DropDownList's SelectedIndex property and catch selected value and text in DropDownList onselectedindexchanged="ddlReason_SelectedIndexChanged" to get selected text field even you have same value in value field ,But it's suggested to use unique value in DataValueField.
protected void ddlReason_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
int index = ddl.SelectedIndex;
string Text = ddl.SelectedItem.ToString();
string Value = ddl.SelectedValue;
}
你可以使用Rahul id作为02,将sachin id作为002或
不要使用deptid你应该使用empid,它对所有记录都是唯一的......
You can use Rahul id as 02 and sachin id as 002 or
Dont use deptid u should use empid which is unique for all records...
这篇关于具有datavaluefield常见值的下拉问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文