Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#文件中获取asp Dropdownlist的选定值?_C#_Asp.net - Fatal编程技术网

如何在c#文件中获取asp Dropdownlist的选定值?

如何在c#文件中获取asp Dropdownlist的选定值?,c#,asp.net,C#,Asp.net,在我的aspx页面中有以下下拉列表: <asp:DropDownList ID="OrderPeriodStatus" runat="server" AutoPostBack="true" CssClass="ddlb" Width="100px" Height="30px" DataSourceID="SqlDataSource5" DataTextField="OrderPeriod" DataValueField="OrderPeriodID" onselecte

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

但是当我打印
值时
。它显示选定的索引,而不是值。为什么?

您在sqdatasource上出现不匹配错误,请替换为
SqlDataSource4

DataSourceID="SqlDataSource5"  <---

<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:ProdDB %> # blah blah query
 </asp:SqlDataSource>

DataSourceID=“SqlDataSource5”如果将dropdownlist上的AutoPostback属性设置为true,并将ViewStateMode设置为inherit,则应获取所选的值。我试着站在我这边,结果成功了。因为您已经有了SqlDataSource,并且dropdownlist的DataSourceId设置为SqlDataSource,所以应该自动填充它

---编辑

对不起,我想我可能误解了你的问题。如果你改用

DropDownListnew.SelectedItem.Text
你应该得到你想要的文本值

--另一编辑:

@Amit,您将从SelectedValue获得索引,因为在dropdownlist代码中,您已经设置了

DataValueField="OrderPeriodID"
它基本上是您的主键或索引。要获取文本值而不是索引,必须使用

SelectedItem.Text
在您的查询中:

myComm = new SqlCommand("SELECT OrderPeriodDate, OrderPeriodStatus, Notes FROM OrderPeriod WHERE OrderPeriod ='" + OrderPeriodStatus.SelectedValue + "'");
我认为你应该使用

WHERE OrderPeriodID ='" + OrderPeriodStatus.SelectedValue + "'");

OrderPeriod之后缺少ID位。

您可以尝试从
Request.Form
对象获取值,并使用
ddl.Items.FindByValue
方法获取文本

    var value = Request.Form[DropDownListnew.UniqeID];
    var text = DropDownListnew.Items.FindByValue(value);

如果dropdown失去了其选定值,则
DropDownList New.SelectedValue
应返回一个空字符串,但OP统计他正在获取
Index
而不是
value
,因为您选择的值绑定在OrderID主键上,并且您的主键类似于indexIt已经存在的主键:(我已经更新了问题,包括page_load方法和onlick方法的完整定义。抱歉,这是粘贴错误。现在我更新了它,现在我该怎么做?你可以更新和测试,我认为这非常好你的实现。你如何知道打印的是所选索引,而不是值?你正在给它OrderID,而不是。)值,因此SelectedValue返回OrderID…该代码在哪里?您在哪里打印值?因为当我从列表中选择另一个数据时。它显示索引号Just mismatch error AmityYou DataSourceId错误。当它应该是SqlDataSource4时,它被设置为SqlDataSource5?
    var value = Request.Form[DropDownListnew.UniqeID];
    var text = DropDownListnew.Items.FindByValue(value);