DropDownList, getting DataValueField returned in C#

38,162

Solution 1

You can use the SelectedValue property:

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

// to read
string value = DropDownList3.SelectedValue;

Solution 2

try this

String taskID = DropDownList3.SelectedItem.Value;
Share:
38,162
David Tunnell
Author by

David Tunnell

https://www.davidtunnell.com Seasoned IT professional with years of experience working on software projects in various capacities (Developer, Tester, Scrum Master, PM) in industries including federal consulting, real estate, industrial construction, healthcare and internet startups. Educated with a Computer Science degree from University of Maryland – University College, an MBA from University of Texas at Austin and most recently completed University of North Carolina at Chapel Hill’s Fullstack Certification. Enjoys being challenged and engaged with projects that require me to work outside my comfort zone and knowledge set, especially while collaborating with a team. Have worked on a variety of technology stacks on both greenfield and existing code bases and have managed/facilitated software teams and implemented Agile/Scrum from the ground up resulting in major successes. Looking to move back into a software development role which is my original passion. The ability to create something that is useful and provides value with my mind, colleagues, and a computer drives me.

Updated on July 09, 2022

Comments

  • David Tunnell
    David Tunnell over 1 year

    I have a drop down list that pulls data from a database to show some of its options:

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

    As you can see, I only display the [Name] column:

    DataTextField="Name" 
    

    But I am also getting the primary key SELECT [PK_Task], [Name] FROM:

    DataValueField="PK_Task"
    

    How do I get access to this primary key based on user selection. If a user selects a specific 'name' from the drop down menu, what C# can I use to get the corresponding primary key returned to me so I can save it in a variable?

    The closest I have gotten is this, but all it does is return the string "PK_Task":

    String taskID = DropDownList3.DataValueField;