How to bind the selected value of a DropDownList How to bind the selected value of a DropDownList asp.net asp.net

How to bind the selected value of a DropDownList


You might want to do something like the code below.You can not set the "SelectedValue" declaratively, but by saying"SelectedValue=<%# [code here] %> you are effectively causing the value to be set when the control is data bound.

<asp:DropDownList                ID="DropDownInfoSource"                runat="server"                DataSourceID="_employeeDataSource"                DataTextField="EmployeeName"                DataValueField="EmployeeID"                SelectedValue='<%# Bind("EmployeeID") %>'                />


I don't know if it will really help you, but did you tried to setup the "SelectedValue" on Code behind?

Example:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load    DropDownInfoSource.SelectedValue = "1" ' your value, hereEnd Sub


There is a field where you define the datasource, the datatextfield (what shows up in the list) and the datavaluefield.

Example (I have a datatable with a column "EmployeeID" and a column "EmployeeName"):

dropdownlist1.datasource = DTdropdownlist1.datatextfield = "EmployeeName"dropdownlist1.datavaluefield = "EmployeeID"dropdownlist1.databind()