Refering to Object Members in A Collection in an ASP.NET BoundColu

G

Guest

I've created a number of business entities for an applciation I'm working on
in ASP.NET (Framework 1.1) and would like to quickly get data into a
BoundColumn for a control that consums a collection. Generally speaking,
when a databound control consums a collection, one simply sets the property
name of the collection members when setting the DataField property of the
BoundColum, as below:


<asp:BoundColumn Visible="False" DataField="ObjId"
HeaderText="id"></asp:BoundColumn>

So in the example above, the DataField "ObjId" is a property of a collection
member, we'll call "ObjectA" (i.e. ObjectA.ObjId").

Now consider we want to do the same thing, but that one of ObjectA's
properties is another object called ObjectB (i.e. ObjectA.ObjectB) which has
its own properties. Just to make things interesting, we'll say that ObjectA
and ObjectB inherit from the same parent class, and that ObjId is an abstract
integer property of that parent class. Thus ObjId is a property of both
ObjectA and ObjectB, and still that ObjectA has ObjectB as one of its
properties.

So as above, we want to refer to ObjectB.ObjId in our BoundColumn, but
unfortunately the collection being consumed is a collection of ObjectA's and
every time I try to refer to any of the properties of ObjectB (i.e.
ObjectA.ObjectB.ObjId) I get an exception being cast.

The question, then is - what syntax would I use in the .aspx file, within
the BoundColumn tag's DataField proeprty to set that property to retrieve
data from ObjectA.ObjectB.ObjId?

Very gratefully,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I have never used it in a boundcolumn but do it all the time with a
TemplateColumn , it's very easy you have to cast it to the correct type and
after that you can access the properties as needed.

Here goes an example:

<itemtemplate>
<span ><%# ((CtpRecord)Container.DataItem).DateRequested.ToString(
"MM/dd/yy", DateTimeFormatInfo.InvariantInfo) %></span>
</itemtemplate>


CtpRecord is a class and the grid is binded to a strong typed collection of
CtpRecords

DateRequestes is a DateTime property
 
S

sloan

This may or may not help.

While I do most of my development in asp.net, and not winforms, what you're
asking I only have a winforms snipplet.

Me.txtPubId.DataBindings.Add(New Binding("Text", Me.m_model,
"CurrentPublisher.PublisherId"))

m_model is a business object. Its actually reflects a title from the pubs
database.

One of the the things a title has, is a publisher.

class Tittle
public Publisher CurrentPublisher
{get{return m_publisher;}}

class Publisher
public string PublisherId


thats the base line objects and properties.

Maybe that can help. My winfroms example works, but I haven't done alot of
MVC in asp.net to tell the truth. Esp with the WebControls.DataBind stuff.

.....

If you find an answer, please post it.
 
G

Guest

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

I have never used it in a boundcolumn but do it all the time with a
TemplateColumn , it's very easy you have to cast it to the correct type and
after that you can access the properties as needed.

Here goes an example:

<itemtemplate>
<span ><%# ((CtpRecord)Container.DataItem).DateRequested.ToString(
"MM/dd/yy", DateTimeFormatInfo.InvariantInfo) %></span>
</itemtemplate>

Ya know, I've gotta learn to do more complete research before posting to a
newsgroup. ;) In fact I'd found this exact solution in a CodeProject article
concernging databinding:
http://www.codeproject.com/aspnet/Mastering_DataBinding.asp

Even so, the prompt assistance is greatly appreciated - as always!

Sincerely,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top