如何设置下拉列表中选择的存在值,而无需在...中对项目进行硬编码 [英] How to set exist value as selected in dropdown list without hardcoding items in...

查看:79
本文介绍了如何设置下拉列表中选择的存在值,而无需在...中对项目进行硬编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net mvc中有一个从数据库检索记录的表单.此表单有下拉列表.在控制器的编辑"操作中,必须选择已经插入的项目.假设我要对用户信息进行编辑,那么必须在插入之前选择其城市,州和国家/地区.任何人都可以告诉我如何在加载页面时从下拉列表中选择的数据库中设置值吗? div class ="h2_lin">解决方案

如果我理解正确,则可以使用其中任何一个.

if (DropDownList1.Items.FindByText("ITEMTEXT") != null)
      DropDownList1.Items.FindByText("ITEMTEXT").Selected = true;



if (DropDownList1.Items.FindByValue("ITEMTEXT") != null)
      DropDownList1.Items.FindByValue("ITEMTEXT").Selected = true;


Hi, i have form in asp.net mvc that retrieves records from database.this form have dropdowns. in Edit action of controller it is must to have selected the already inserted items. suppose if i am taking the User information for edit, then its City,State and country must be selected as they inserted before.Can any one tell me how to set values from database as selected in drop down list when page loads???

解决方案

If I am understanding correct then you can use any one of them.

if (DropDownList1.Items.FindByText("ITEMTEXT") != null)
      DropDownList1.Items.FindByText("ITEMTEXT").Selected = true;


OR

if (DropDownList1.Items.FindByValue("ITEMTEXT") != null)
      DropDownList1.Items.FindByValue("ITEMTEXT").Selected = true;


这篇关于如何设置下拉列表中选择的存在值,而无需在...中对项目进行硬编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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