带有列表或数组列表的下拉列表绑定 [英] dropdown binding with list or arraylist
问题描述
plz帮帮我,
我需要的是使用arraylist或list下拉绑定
带有值字段和文本字段的
ID名称
---- -----
1辆车
2辆巴士
3 XYZ
这样,我有许多领域.帮助我做到这一点
plz help me,
what i need is drop down binding with arraylist or list
with value field and text field
ids Name
---- -----
1 Car
2 Bus
3 XYZ
in this way i have number of fields. help me in doing this
public int ids
{
get;
set;
}
public string Name
{
get;
set;
}
推荐答案
我假设我们有一个下拉列表(DropDownList1),其中一页具有以下结构,
I assume we have a drop down list (DropDownList1) one the page with following structure,
<asp:dropdownlist id="DropDownList1" runat="server" datatextfield="Name" datavaluefield="Id" xmlns:asp="#unknown">
</asp:dropdownlist>
以及后面的相关代码,以用数据填充DropDownList,
and related code behind to populate the DropDownList with data,
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IList persons = new List<Person>()
{
new Person(){ Id="1", Name="A"},
new Person(){ Id="2", Name="B"},
new Person(){ Id="3", Name="C"},
};
DropDownList1.DataSource = persons;
DropDownList1.DataBind();
}
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
}
}
因此,从上面的代码中我们可以看到,我创建了一个具有两个属性Id和Name的Person类型.我将ID设置为DropDownList的值字段,并将Name设置为Textfield.
我创建了一个人员列表,并将该列表设置为DropDownList的数据源.最后,使用DataBind()将数据源与DropDownList绑定.
注意:我已经在名为Default.aspx
的页面上完成了所有这些操作
希望对您有所帮助:)
So from the above code we can seer, I created a Person type with two property Id and Name. I set the Id as value field for the DropDownList and Name as Textfield.
I Created a List of Person and set that list as data source of the DropDownList. Finally to Bind the data source with the DropDownList using DataBind().
Note: I have done all these on a page named Default.aspx
Hope it helps :)
Dictionary<int,string> list=new Dictionary<int,string>();
list.Add(1," Car");
list.Add(2," Bus");
list.Add(3," XYZ");
dropItems.DataSource=list.toList();
或为您的问题,我们需要创建车辆类别列表为
or for your problem We need Create List of Vehicle Class as
public class Vehicle
{
public int ids { get;set;}
public string Name{get;set;}
}
然后加载它............................
Then load it ...........................
List<vehicle> Vehicles=new List<vehicle>;
Vehicles.Add(1," Bus");
Vehicles.Add(2," Car");
Vehicles.Add(3," XYZ");
dropItems.DataSource=Vehicles;
或者您可以从Vehicle Table或其他数据库中获取它.
我认为您有解决方案.
or you can get it from Database from Vehicle Table or something.
I think you have your solution.
如果您从数据库查询中获取值,则为
那么你可以做...
if you get the values from a db query
then you can make...
var result = RESULT_OF_YOUR_QUERY;
dropItems.DataSource=result;
dropItems.DataValueField = "ids";
dropItems.DataTextField = "Name";
dropItems.DataBind();
这篇关于带有列表或数组列表的下拉列表绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!