8

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...???
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
vyclarks
  • 854
  • 2
  • 15
  • 39
  • If each item has three fields, why do you hold it as a list of objects? Make a list of those classes, that have the three fields you are talking about. – nvoigt Oct 06 '13 at 08:01
  • Your problem relates to C# and not so much ASP.net – aqwert Oct 06 '13 at 08:09

2 Answers2

8

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;
}
Alex
  • 7,728
  • 3
  • 35
  • 62
  • Can you take a look at my precious problem, I cannot do anything because that problem http://stackoverflow.com/questions/19206186/how-to-get-field-value-of-selected-row-devexpress-gridview – vyclarks Oct 06 '13 at 08:28
  • It has an error with your code: Unable to cast object of type 'System.Object[]' to type 'MyObject – vyclarks Oct 06 '13 at 08:34
  • 1
    That's a problem with your code, not mine. You have to change the way you add objects to `selectedValues` if you want to use the second approach. Explaining how to do that is beyond the scope of this question (hint: `selectedValues.Add( new MyObject() { Id = 1, Titile = "Titile", Content = "Content } );`) – Alex Oct 06 '13 at 08:41
2

Define a class like this :

public class myclass {
       string id ;
       string title ;
       string content;
 }

 public class program {
        public void Main () {
               List<myclass> objlist = new List<myclass> () ;
               foreach (var value in objlist)  {
                       TextBox1.Text = value.id ;
                       TextBox2.Text= value.title;
                       TextBox3.Text= value.content ;
                }
         }
  }

I tried to draw a sketch and you can improve it in many ways. Instead of defining class "myclass", you can define struct.

User1551892
  • 3,236
  • 8
  • 32
  • 52