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

Discussion in 'ASP .Net' started by =?Utf-8?B?Um9zcyBIb2xkZXI=?=, Mar 10, 2006.

  1. 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,

    --
    Ross Holder
    Ottawa, ON (Canada)
    http://spaces.msn.com/members/ross613
     
    =?Utf-8?B?Um9zcyBIb2xkZXI=?=, Mar 10, 2006
    #1
    1. Advertising

  2. 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

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation
     
    Ignacio Machin \( .NET/ C# MVP \), Mar 10, 2006
    #2
    1. Advertising

  3. =?Utf-8?B?Um9zcyBIb2xkZXI=?=

    sloan Guest

    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.





    "Ross Holder" <> wrote in message
    news:...
    > 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,
    >
    > --
    > Ross Holder
    > Ottawa, ON (Canada)
    > http://spaces.msn.com/members/ross613
     
    sloan, Mar 10, 2006
    #3
  4. Re: Refering to Object Members in A Collection in an ASP.NET Bound

    "Ignacio Machin ( .NET/ C# MVP )" wrote:

    > 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,

    --
    Ross Holder
    Ottawa, ON (Canada)
    http://spaces.msn.com/members/ross613
     
    =?Utf-8?B?Um9zcyBIb2xkZXI=?=, Mar 10, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TmljayBaZHVuaWM=?=

    ASP.Net 2.0 - refering to the web page class

    =?Utf-8?B?TmljayBaZHVuaWM=?=, Feb 9, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,445
  2. refering struct members

    , Sep 16, 2006, in forum: C Programming
    Replies:
    2
    Views:
    294
    Thad Smith
    Sep 17, 2006
  3. Armando
    Replies:
    4
    Views:
    438
    Jerry Coffin
    Jul 16, 2006
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    983
    Øyvind Isaksen
    May 18, 2007
  5. Toby Rodwell

    Refering to an object's 'parent'

    Toby Rodwell, Oct 5, 2007, in forum: Ruby
    Replies:
    3
    Views:
    131
    ara.t.howard
    Oct 5, 2007
Loading...

Share This Page