绑定下拉列表以在绑定时显示所选值

Bind the Drop Down to display the selected value on bind

假设我有 2 个下拉列表,一个显示州列表,另一个显示职业。在这种情况下两者都是独立的。

在数据库方面,我有一个用户配置文件记录,其中包含保存 "state_code" 和 "Occupation_desc"

的字段

保存状态详情的table会有两个字段:state_desciption、state_code 保存状态详情的table会有两个字段:Occupation_desc、id

加载配置文件时,绑定下拉控件时,我需要显示州名而不是快捷方式(我需要显示加利福尼亚而不是"CA", 但数据库记录将有 "CA"。)

sample code: 

        drpdwnstates.DataSource = //binding the results
                Me.drpdwnstates.DataTextField = "state_desciption"
                Me.drpdwnstates.DataValueField = "state_code"
                Me.drpdwnstates.DataBind()

例如,如果我要从加利福尼亚加载用户配置文件,那么我需要在下拉列表中 highlight/select 加利福尼亚。

合同于此,其他下拉将有职业列表需要显示描述本身(描述本身保存在用户个人资料记录中,而不是id)。

关于如何select加载保存的值有什么帮助吗??

您可以通过设置 DropDownList 的 selected 值来 select 一个项目:

drpdwnstates.SelectedValue = "CA";

如果州代码保存在用户配置文件中,您可以这样设置值:

drpdwnstates.SelectedValue = userProfile.StateCode;

您可以调整此代码以适应您的用户配置文件的特定数据结构。