如何绑定 DropDownList 的选定值 [英] How to bind the selected value of a DropDownList

查看:29
本文介绍了如何绑定 DropDownList 的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据绑定就是声明性代码,对吧?所以我用属性指定我想要的东西,框架负责其余的.除非我弄错了,数据绑定与 S&M 相关,对吗?

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?

那么,为什么 DropDownList 控件只为其数据源(即其列表源)提供绑定字段,而不为其实际数据字段提供绑定字段.即我如何将所选值我的名字 DropDownList 绑定到我的个人记录中的 Name 字段?这是微软的疏忽,还是我的疏忽?

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?

推荐答案

您可能想要执行类似于以下代码的操作.您不能以声明方式设置SelectedValue",而是说"SelectedValue=<%# [此处的代码] %> 当控件是数据绑定时,您实际上是在设置值.

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") %>'
                />

这篇关于如何绑定 DropDownList 的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆