Databind为ListBox选择的值 [英] Databind Selected value for ListBox

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

问题描述

你好,

我想知道是否有一种方法可以对ListBox中的"Selected"属性进行数据绑定,类似于可以对DisplayMember或ValueMember进行数据绑定的方法.

我有一个List< myobject>其中MyObject包含一个ID(值成员),Name(显示成员)和Active布尔标志(已选择?),我想要一种简单的映射方式,以便当Active标志从一个值更改为另一个值时,列表框能够理解.

我环顾了网上,却找不到任何东西,因此我怀疑是否有可能,但以防万一...或者如果有人有替代解决方案,将不胜感激.

==编辑===

我尝试进行数据绑定的对象不是数据库表,而是对象列表(尽管我认为思路基本相同).该对象非常简单,如下所示(我使用的是.NET 2,因此我没有默认的get和set):

Hello,

I am wondering if there is a method to databind the "Selected" property in a ListBox similar to the way that you can Databind the DisplayMember or ValueMember.

I have a List<myobject> where MyObject contains an ID (valuemember), Name (displaymember) and Active boolean flag (selected?) and I would like an easy way to map it so that the Listbox will understand when the Active flag is changed from one value to the other.

I have looked around the web and yet to find something so I somewhat doubt it is possible but just in case... or if anyone has alternate solutions it would be greatly appreciated.

== EDIT ===

The object I am trying to databind is not a database table but a List of objects (although I presume the idea is basically the same). The object is very simple like the following (I am using .NET 2 so I don''t have default get and set):

<br />
public class MyObject<br />
{<br />
  private int id;<br />
  public int ID { get { return id; } set { id = value; } }<br />
<br />
  private string name;<br />
  public string Name { get { return name; } set { name = value; } }<br />
<br />
  private bool flag;<br />
  public bool Flag { get { return flag; } set { flag= value; } }<br />
}<br />
<br />
List<MyObject> myObjectList = new List<MyObject>();<br />
<br />



Sunasara,您的假设是正确的:如果Flag == true,则我希望ListBox将项目显示为选中;如果Flag == false,则将其取消选择.



Sunasara, your assumption is correct: I want the ListBox to show the item as selected if Flag == true and unselected if Flag == false.

推荐答案


Maikeru2000

我遇到了您的问题,没有方法可以将ListBox与multiselect选项绑定在一起.我假设您有一个数据表,其中包含三列,例如Id,Name或Flag.您必须绑定类似的东西吗?

我的假设是:
如果Flag包含true,则将选择一个项目,否则取消选择.


如果我的假设是正确的,请告诉我您的评论,所以我给您一个解决方案:)

谢谢与问候
Imdadhusen
Hi,
Maikeru2000

I got your problem and there is no methods to bind ListBox with multiselect options; i assumed you have a datatable which contains three column like Id,Name or Flag. You have to bind somthing like?

my assumption is:
if Flag contain true it will select an item else unselect.


please let me know your comments, if my assumption is correct, so i''ll give you a solution :)

Thanks & Regards
Imdadhusen


<br />
//This is the class<br />
public class MyObject<br />
{<br />
    private int id;<br />
    public int ID { get { return id; } set { id = value; } }<br />
<br />
    private string name;<br />
    public string Name { get { return name; } set { name = value; } }<br />
<br />
    private bool flag;<br />
    public bool Flag { get { return flag; } set { flag = value; } }<br />
<br />
    public MyObject(int id, string name, bool flag)<br />
    {<br />
        ID = id;<br />
        Name = name;<br />
        Flag = flag;<br />
    }<br />
}<br />
<br />
//Using above class<br />
public partial class Demo : System.Web.UI.Page<br />
{<br />
    protected void Page_Load(object sender, EventArgs e)<br />
    {<br />
        List<MyObject> myObjectList = new List<MyObject>();<br />
        myObjectList.Add(new MyObject(1, "Imdadhusen", true));<br />
        myObjectList.Add(new MyObject(2, "Sunasara", false));<br />
        myObjectList.Add(new MyObject(1, "ASP.Net", true));<br />
        myObjectList.Add(new MyObject(1, "C#", true));<br />
        myObjectList.Add(new MyObject(1, "Java", false));<br />
        myObjectList.Add(new MyObject(1, "JSP", false));<br />
        myObjectList.Add(new MyObject(1, "JavaScript", true));<br />
<br />
        bindListBox(myObjectList, ListBox1);<br />
    }<br />
<br />
    //Bind Listbox with object<br />
    //Note: please make sure your listbox SelectionMode property should be Multiple<br />
    public void bindListBox(List<MyObject> objList, ListBox lst)<br />
    {<br />
        if (objList != null)<br />
        {<br />
            for (int i = 0; i < objList.Count; i++)<br />
            {<br />
                ListBox1.Items.Add(new ListItem(objList[i].Name, objList[i].ID.ToString()));<br />
                if (lst.SelectionMode != ListSelectionMode.Single) ListBox1.Items[i].Selected = objList[i].Flag;<br />
            }<br />
        }<br />
    }<br />
<br />
}<br />


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

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