0

consider the following classes (similar to the ones i am working with):

namespace Project
{
    public class Person
    {

        public Person()
        {

        }

      private string name;
      public string Name
{

 get { return name; }
            set { name = value; }
}

        private List<Pet> myPets;

        public List<Pet> MyPets
        {
            get { return myPets; }
            set { myPets = value; }
        }
    }
}
namespace Project
{
    public class Pet
    {

        public Pet()
        {

        }
        private int numberOfPaws;

        public int NumberOfPaws
        {
            get { return numberOfPaws; }
            set { numberOfPaws = value; }
        }
    }
}

Lets just say i bound my gridview to a List<Person>,

if i had template fields in the gridview markup how would I use Databinder.Eval() function to display 'numberOfPaws' for a pet at a certain index

eg.

<Gridview datasource=generic list of person>
<columns>
<TemplateField>
<ItemTemplate>
   <asp: Label id="person" text="<% #Eval(Person.name) %>"></Label>
</ItemTemplate>
</TemplateField>
  <TemplateField>
    <ItemTemplate>
         <asp: Label id="id" Text="<% [B][U]# (Container.DataItem).MyPets[0].NumberOfPaws.ToString()[/U][/B] %>"></Label>
     </ItemTemplate>
  </TemplateField>
</columns>
</Gridview>

i get error saying "server tag is not well formed " so that highlight code isnt how to do it, any suggestions or rough guidance???

Edited by __avd: Added [code] tags. Do wrap your programming code blocks within [code] ... [/code] tags

3
Contributors
4
Replies
10
Views
6 Years
Discussion Span
Last Post by Garr
0
<Gridview datasource=generic list of person>
<columns>
<TemplateField>
<ItemTemplate>
<asp: Label id="person" text="[U]<%# Eval(Person.name) %>[/U]"></Label>
</ItemTemplate>
</TemplateField>
<TemplateField>
<ItemTemplate>
<asp: Label id="id" Text="<%# [U]Eval(Container.DataItem,MyPets[0].NumberOfPaws.ToString())[/U] %>"></Label>
</ItemTemplate>
</TemplateField>
</columns>
</Gridview>

Edited by __avd: Added [code] tags. Do wrap your programming code blocks within [code] ... [/code] tags

0

i got error:

"The name MyPets doesn't exist in the current context"

0

I found a workaround to solve this problem.

I just programmatically set label text via the rowdatabound event of the gridview.

Thanks Shoaib for having a look at it.

0

You need to use single quotes:

Text='<%# [U]Eval(Container.DataItem,MyPets[0].NumberOfPaws.ToString())[/U] %>'

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.