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?
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.
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.
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.
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, here End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With