简体   繁体   English

如何从下拉列表中获取DataValueField?

[英]How to get DataValueField from dropdownlist?

I want to get data from dropdownlist selected item with sql server.But it's don't working. 我想用sql server从dropdownlist选择的项目中获取数据。但是它不起作用。

My asp.net desing codes: 我的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>

Now I putting valuefield from sqlserver.After It is returning null value in selecteditem.Value. 现在我将值字段从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);
}

Greetings you can use 可以使用的问候

string value = DropDownList3.SelectedValue;

and insert the value to your database. 并将value插入数据库。

Update: 更新:

Bind your SqlDataSource's SelectCommand in your code behind in your page_load: 将您的SqlDataSource的SelectCommand绑定到page_load中的代码中:

if(!IsPostBack)
{
  SqlDataSourceSiparisYazan.SelectCommand = "Your Query";
  //Make Sure your query is right, trace it with breakpoint
}

And Empty its SelectCommand in your .aspx file: 并在.aspx文件中清空其SelectCommand:

<asp:SqlDataSource runat="server" ID="SqlDataSourceSiparisYazan" ConnectionString='<%$ ConnectionStrings:CPMMASTER_PGCS %>' SelectCommand=""></asp:SqlDataSource>

Then try to get the SelectedValue. 然后尝试获取SelectedValue。

Second Update: I looked at your Dropdownlist again you didn't bind the KOD to DataValueField (as if its the value?) And you didn't bind the Its Name (Whatever it is) to DataTextField and you expect magically get the value? 第二次更新:我再次查看了您的Dropdownlist ,您没有将KOD绑定到DataValueField (好像它的值吗?),也没有将Its Name (无论它是什么)绑定到DataTextField并且您期望神奇地得到值吗?

Edit your DropdownList Like: 像这样编辑您的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”在后面的代码中获得下拉列表的选定值。

 <asp:DropDownList runat="server" ID="ddl" DataValueField="ID" DataTextField="Text"></asp:DropDownList>

(object have ID, Text,...) (对象具有ID,文本等)

and bind List to ddl: ddl.DataSource = list<object> get value use: ddl.SelectedValue 并将List绑定到ddl: ddl.DataSource = list<object>获取值,使用: ddl.SelectedValue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM