如何获得名单,LT的单个值;对象> [英] How to get single value of List<object>

查看:155
本文介绍了如何获得名单,LT的单个值;对象>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新ASPX,希望你不介意,如果我的问题是与别人这么简单。

我用列表与LT;对象> selectedValues​​;

  selectedValues​​ = ...对象的列表(项目1,项目2,..)

每个对象都有3个字段: ID 标题内容

 的foreach(对象[]在selectedValues​​项)
{
  的foreach(在项目对象的值)
  {
    字符串结果+ =的String.Format({0},值);
    Textbox1.Text =结果; - >所有的字段会显示在一个TextBox。
  }
}

我的问题是:我怎么能拿到单场,我的意思是:

 的foreach(在项目对象的值)
            {
                TextBox1.Text = ID ...?
                TextBox2.Text =标题...?
                TextBox3.Text =内容... ???
}


解决方案

您可以通过索引对象数组访问字段:

 的foreach(对象[]在selectedValues​​项)
{
  idTextBox.Text =项目[0];
  titleTextBox.Text =项目[1];
  contentTextBox.Text =项[2];
}

这是说,你会更好存储在一个小班自己的领域,如果项目的数量不是动态的:

 公共类为MyObject
{
    公众诠释标识{搞定;组; }
    公共字符串名称{搞定;组; }
    公共字符串内容{搞定;组; }
}

然后,你可以这样做:

 的foreach(在selectedValues​​为MyObject项)
{
  idTextBox.Text = item.Id;
  titleTextBox.Text = item.Title;
  contentTextBox.Text = item.Content;
}

I'm a new to ASPX, hope you dont mind if my problem is so simple with somebody.

I use a List<object> selectedValues;

selectedValues=...list of object(item1, item2,..)

Each object has 3 fields: id, title, and content.

foreach (object[] item in selectedValues)
{
  foreach (object value in item)
  {
    string result += string.Format("{0}    ", value);
    Textbox1.Text= result;--> all field is displayed in one Textbox.
  }
}

My problem is: how can I get the single field, I mean:

foreach (object value in item)
            {
                TextBox1.Text = id...???
                TextBox2.Text= title...???
                TextBox3.Text= content...???
}

解决方案

You can access the fields by indexing the object array:

foreach (object[] item in selectedValues)
{
  idTextBox.Text = item[0];
  titleTextBox.Text = item[1];
  contentTextBox.Text = item[2];
}

That said, you'd be better off storing the fields in a small class of your own if the number of items is not dynamic:

public class MyObject
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
}

Then you can do:

foreach (MyObject item in selectedValues)
{
  idTextBox.Text = item.Id;
  titleTextBox.Text = item.Title;
  contentTextBox.Text = item.Content;
}

这篇关于如何获得名单,LT的单个值;对象&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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