如何在c#文件中获取asp Dropdownlist选定值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12216811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get asp Dropdownlist selected value in c# file?
提问by Amit Pal
I have a follow dropdown list in my aspxpage:
我的页面中有一个关注下拉列表aspx:
<asp:DropDownList ID="OrderPeriodStatus" runat="server" AutoPostBack="true"
CssClass="ddlb" Width="100px" Height="30px" DataSourceID="SqlDataSource5"
DataTextField="OrderPeriod" DataValueField="OrderPeriodID" onselectedindexchanged="OrderPeriodStatus_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:ProdDB %> # blah blah query
</asp:SqlDataSource>
I tried to access the value of selected value in C# by following code:
我尝试通过以下代码访问 C# 中选定值的值:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ProcessEligibleScenarios();
LoadOptions();
ddlbPeriod.DataBind();
GridView1.DataBind();
gvActiveLogs.DataBind();
}
}
protected void OrderPeriodStatus_SelectedIndexChanged(object sender, EventArgs e)
{
if (OrderPeriodStatus.SelectedValue != null)
{
SqlConnection myConn = default(SqlConnection);
SqlCommand myComm = default(SqlCommand);
myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ProdDB"].ConnectionString);
myConn.Open();
myComm = new SqlCommand("SELECT OrderPeriodDate, OrderPeriodStatus, Notes FROM OrderPeriod WHERE OrderPeriod ='" + OrderPeriodStatus.SelectedValue + "'");
try
{
myComm.Connection = myConn;
SqlDataReader Dr = myComm.ExecuteReader();
while (Dr.Read())
{
System.Diagnostics.Debug.Write("While reading the data");
TextBox1.Text = Dr["OrderPeriodDate"].ToString();
TextBox2.Text = Dr["OrderPeriodStatus"].ToString();
NotesArea.Value = (string)Dr["Notes"];
System.Diagnostics.Debug.WriteLine(OrderPeriodStatus.SelectedValue);
}
Dr.Close();
myConn.Close();
}
catch (SqlException sqx)
{
}
GridView1.DataBind();
}
else { }
}
But when i print the value. It shows the selected Index not the value. Why?
但是当我打印value. 它显示选定的索引而不是值。为什么?
采纳答案by Aghilas Yakoub
You have mismatch error on sqdatasource, replace with SqlDataSource4
您在 sqdatasource 上有不匹配错误,请替换为 SqlDataSource4
DataSourceID="SqlDataSource5" <---
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:ProdDB %> # blah blah query
</asp:SqlDataSource>
回答by tranceporter
If you set AutoPostback property on the dropdownlist to true, and ViewStateMode to inherit, you should get value you selected. I tried it on my side, and it worked. Since you already have SqlDataSource, and the DataSourceId of the dropdownlist is set to the SqlDataSource, it should be populated automatically.
如果您将下拉列表上的 AutoPostback 属性设置为 true,并将 ViewStateMode 设置为继承,您应该获得您选择的值。我在我身边尝试过,它奏效了。由于您已经拥有 SqlDataSource,并且下拉列表的 DataSourceId 设置为 SqlDataSource,因此应该自动填充它。
---edit
- -编辑
Sorry, I think I might have misread your question. If you instead use
抱歉,我想我可能误读了您的问题。如果您改为使用
DropDownListnew.SelectedItem.Text
you should get the text value you want.
你应该得到你想要的文本值。
-- another edit:
- 另一个编辑:
@Amit, you will get index from SelectedValue, because in your dropdownlist code, you have set
@Amit,您将从 SelectedValue 获得索引,因为在您的下拉列表代码中,您已设置
DataValueField="OrderPeriodID"
which is basically your primary key or index. To get the text value instead of index you have to use
这基本上是您的主键或索引。要获取文本值而不是索引,您必须使用
SelectedItem.Text
Also in your query:
同样在您的查询中:
myComm = new SqlCommand("SELECT OrderPeriodDate, OrderPeriodStatus, Notes FROM OrderPeriod WHERE OrderPeriod ='" + OrderPeriodStatus.SelectedValue + "'");
I think you should be using
我认为你应该使用
WHERE OrderPeriodID ='" + OrderPeriodStatus.SelectedValue + "'");
You are missing the ID bit after OrderPeriod.
您在 OrderPeriod 之后缺少 ID 位。
回答by Onur Topal
You can try get the value from Request.Formobject and to get the text use ddl.Items.FindByValuemethod
您可以尝试从Request.Form对象获取值并获取文本使用ddl.Items.FindByValue方法
var value = Request.Form[DropDownListnew.UniqeID];
var text = DropDownListnew.Items.FindByValue(value);

