簡體   English   中英

如何從下拉列表中獲取DataValueField?

[英]How to get DataValueField from dropdownlist?

我想用sql server從dropdownlist選擇的項目中獲取數據。但是它不起作用。

我的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>

現在我將值字段從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插入數據庫。

更新:

將您的SqlDataSource的SelectCommand綁定到page_load中的代碼中:

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

並在.aspx文件中清空其SelectCommand:

<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”在后面的代碼中獲得下拉列表的選定值。

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

(對象具有ID,文本等)

並將List綁定到ddl: ddl.DataSource = list<object>獲取值,使用: ddl.SelectedValue

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM