Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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# DropDownList,获取C中返回的DataValueField_C#_Asp.net_.net_Visual Studio - Fatal编程技术网

C# DropDownList,获取C中返回的DataValueField

C# DropDownList,获取C中返回的DataValueField,c#,asp.net,.net,visual-studio,C#,Asp.net,.net,Visual Studio,我有一个下拉列表,可以从数据库中提取数据以显示其一些选项: <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="PK_Task" AppendDataBoundItems="true" CssClass="dropDownList"> <asp:ListI

我有一个下拉列表,可以从数据库中提取数据以显示其一些选项:

    <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1"
        DataTextField="Name" DataValueField="PK_Task" AppendDataBoundItems="true" CssClass="dropDownList">
        <asp:ListItem Selected="True">General Support</asp:ListItem>
        <asp:ListItem>Vacation</asp:ListItem>
        <asp:ListItem>Sick Leave</asp:ListItem>
        <asp:ListItem>Something Else</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
        SelectCommand="SELECT [PK_Task], [Name] FROM [Task] WHERE ([PointPerson] LIKE '%' + @PointPerson + '%') AND [Status] NOT LIKE 'Done'">
        <SelectParameters>
            <asp:QueryStringParameter Name="PointPerson" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
但我也从以下位置获得主键SELECT[PK_Task],[Name]:

DataValueField="PK_Task"
如何根据用户选择访问此主键。如果用户从下拉菜单中选择了一个特定的“名称”,我可以使用什么C来获取返回给我的相应主键,以便将其保存在变量中

我得到的最接近的结果是这个,但它所做的只是返回字符串PK_任务:

String taskID = DropDownList3.DataValueField;
试试这个

String taskID = DropDownList3.SelectedItem.Value;
您可以使用SelectedValue属性:

// to set
DropDownList3.SelectedValue = "1";

// to read
string value = DropDownList3.SelectedValue;
你能用什么方法 或者


可能重复:因为您使用了dropdown.datavaluefield,所以它会返回绑定字段的名称。
// to set
DropDownList3.SelectedValue = "1";

// to read
string value = DropDownList3.SelectedValue;
dropdown3.selectedvalue
dropdown3.selecteditem.value