如何从下拉列表中获取DataValueField?
问题描述:
我想从下拉列表中选择项目与sql server.But数据,但它不工作。如何从下拉列表中获取DataValueField?
我的asp.net德兴代码:
<asp:DropDownList ID="drp_SiparisYazan" runat="server" DataSourceID="SqlDataSourceSiparisYazan" DataTextField="ACIKLAMA" DataValueField="KOD"></asp:DropDownList>
<asp:SqlDataSource runat="server" ID="SqlDataSourceSiparisYazan" ConnectionString='<%$ ConnectionStrings:.. %>' SelectCommand="SELECT ...'"></asp:SqlDataSource>
现在我把valuefield从sqlserver.After它在selecteditem.Value返回空值。
protected void btnSave_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmdSave = new SqlCommand(CommandText, con);
cmdSave.Parameters.AddWithValue("@siparisyazan", drp_SiparisYazan.SelectedItem.Value);
}
答
问候得到下拉列表中的代码所选择的值,你可以使用
string value = DropDownList3.SelectedValue;
,并插入value
到数据库。
更新:
绑定在你的Page_Load后面的代码你的SqlDataSource的SelectCommand:
if(!IsPostBack)
{
SqlDataSourceSiparisYazan.SelectCommand = "Your Query";
//Make Sure your query is right, trace it with breakpoint
}
而空了的SelectCommand在.aspx文件:
<asp:SqlDataSource runat="server" ID="SqlDataSourceSiparisYazan" ConnectionString='<%$ ConnectionStrings:CPMMASTER_PGCS %>' SelectCommand=""></asp:SqlDataSource>
然后试着获取SelectedValue。
第二次更新: 我看着你Dropdownlist
你又没有绑定KOD
到DataValueField
(好像它的价值呢?)你不结合Its Name
(不管它是什么),以DataTextField
和你期望神奇地获得价值?
编辑您的DropdownList
像:
<asp:SqlDataSource runat="server" ID="SqlDataSourceSiparisYazan" DataTextField="Field of the Name to be shown" DataValueField="KOD" ConnectionString='<%$ ConnectionStrings:CPMMASTER_PGCS %>' SelectCommand="SELECT KOD,ACIKLAMA FROM REFKRT ...'"></asp:SqlDataSource>
答
,你可以通过后面使用 “drp_SiparisYazan.SelectedValue”
+0
我试过了。它不工作这个代码 –
答
<asp:DropDownList runat="server" ID="ddl" DataValueField="ID" DataTextField="Text"></asp:DropDownList>
(对象有ID,文本,...)
和绑定列表来DDL:ddl.DataSource = list<object>
GET值使用:ddl.SelectedValue
什么不工作?你能解释一下吗?下拉列表的填充是否正在工作? – Esko
是工作dropdownlist,它采取dropdownlist.But中的数据,但我没有得到selecteditem值 –
可能重复的[DropDownList,获取DataValueField在C#中返回](http://stackoverflow.com/questions/17348678/dropdownlist-getting-datavaluefield-returned -in-c-sharp) –