如何将数据绑定到下拉列表? [英] How to bind data to dropdown?

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

问题描述

我有ddlCountryDropdown用于显示国家/地区名称。我有表tblCountry,它有库存名称CountryName,我想在页面加载事件时绑定到ddlCountryDropdown。我创建了一个文件夹DTO,我添加了类文件MasterDTO.Cs,我在下面使用实体框架编写代码。但我不知道如何将数据绑定到page_load event下拉。请帮助我解决我的问题。



 public List <   tblCountry  >  GetCountries()
{

列表< tblCountry > d = null;
using(var context = new HealthEntities())
{
d = context.tblCountries
.ToList < < span class =code-leadattribute> tblCountry > ();

}

返回d;
}

解决方案

试试这个..

在page_load事件中添加此代码。< br $>




 MasterDTO obj =  new  MasterDTO ; 
ddlCountryDropdown.DataSourse = obj.GetCountries();
ddlCountryDropdown.DataTextField = CountryName; // 用于显示
ddlCountryDropdown.DataValueField = CountryId; // 对于值,如果有,可以绑定国家/地区ID。其他明智的分配countryName。
ddlCountryDropdown.DataBind();







希望这会有用...


试试这个链接。



* http://blogs .msdn.com / b / dataaccesstechnologies /存档/ 2012/08/09 /错误QUOT最底层提供商失败的上开-QUOT-在实体框架-application.aspx [< a href =http://blogs.msdn.com/b/dataaccesstechnologies/archive/2012/08/09/error-quot-the-underlying-provider-failed-on-open-quot-in-entity-framework- application.aspxtarget =_ blanktitle =New Window> ^ ]



* http://social.msdn.microsoft.com/Forums/en-US/sqlexpress/thread/ 091b1eac-4ed6-43b8-b184-a713d1f2b9eb / [ ^ ]

i have ddlCountryDropdown which is used to dispaly country name. I have table tblCountry which has coulmn name CountryName which i want to bind to ddlCountryDropdown at page load event.I am create one folder DTO in that i add class file MasterDTO.Cs in that i write below code using entity framework.but i don''t how to bind data to dropdown at page_load event.please help me to solved my problems.

public List<tblCountry> GetCountries()
       {

           List<tblCountry> d = null;
           using (var context = new HealthEntities())
           {
               d = context.tblCountries
                   .ToList<tblCountry>();

           }

           return d;
       }

解决方案

try this..
add this code in your page_load event.


MasterDTO obj = new MasterDTO;
ddlCountryDropdown.DataSourse = obj.GetCountries();
ddlCountryDropdown.DataTextField = "CountryName"; // for display
ddlCountryDropdown.DataValueField = "CountryId";  // for value u can bind country id if you have. other wise assign countryName.
ddlCountryDropdown.DataBind();




hope this will work...


try this link.

* http://blogs.msdn.com/b/dataaccesstechnologies/archive/2012/08/09/error-quot-the-underlying-provider-failed-on-open-quot-in-entity-framework-application.aspx[^]

* http://social.msdn.microsoft.com/Forums/en-US/sqlexpress/thread/091b1eac-4ed6-43b8-b184-a713d1f2b9eb/[^]


这篇关于如何将数据绑定到下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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