Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind the selected value of a DropDownList

Tags:

Data binding is all about declarative code, right? So I specify what I want with attributes, and the framework takes care of the rest. Unless I'm mistaken and data binding relates to S&M, right?

So, why does the DropDownList control only provide binding fields for its data source, i.e. its list source, and not for its actual data field. i.e. how the heck to I bind the selected value my name DropDownList to the Name field in my Person record? Is this a gross oversight on Microsoft's part, or on mine?

What is the point of two way data binding if I still have to manually set and read the selected value?

like image 821
ProfK Avatar asked Mar 02 '09 21:03

ProfK


People also ask

How do you bind a selected value in a DropDown?

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.

How do you bind a selected item in a drop-down list to a text box and edit it?

You can bind a selected value from the drop-down list using the ngModel and edit it inside the text box or numeric input box. The selected item text and value can be got using the change event of the drop-down list through the args. itemData.

How do you bind a static value to a DropDownList in MVC?

Binding MVC DropDownList with Static Values Just add an Html helper for DropDownList and provide a static list of SelectListItem. The values added as SelectListItem will be added and displayed in the DropDownList. In this way, you do not need to add anything to Controller Action.


2 Answers

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") %>'                 /> 
like image 117
Andrew Shepherd Avatar answered Sep 28 '22 07:09

Andrew Shepherd


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, here End Sub 
like image 38
oviniciusfeitosa Avatar answered Sep 28 '22 07:09

oviniciusfeitosa