Binding to a Transient Property

4 posts, 1 answers
  1. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 17 Jul 2010 Link to this post

    I have a class that includes a transient property...
    [Telerik.OpenAccess.Persistent(IdentityField = "id")]
    public class MyClass
    {
        int id;
        string name;
        [FieldAlias("id")]
        public int ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        [FieldAlias("name")]
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        [Telerik.OpenAccess.Transient()]
        public string Note;
    }

    Now, I'm getting an instance of MyClass from the DB using something like this...

    var x = from c in Scope.Extent<MyClass>()
            where c.ID.Equals(99)
            select c;

    Then I'm populating my 'Note' property locally...

    MyClass myClass = x.FirstOrDefault();
    if(myClass != null)
    {
      myClass.Note = "blah";
    }

    Finally, I'm creating an IEnumerable that includes my "myClass" object and binding that as the DataSource of an asp:DetailsView.

    If I look at the object assigned to the DetailsView.DataSource property I can see the Note property and I can see that it is populated.

    However, the markup contains this ...

    <asp:Label ID="Notes" runat="server" Text='<%# Eval("Note") %>' />

    And the system reports an exception when trying to execute it. The exception reads "DataBinding: 'RSD.Model.MyClass' does not contain a property with the name 'Note'."

    I don't understand why.

    Can anyone shed any light on this for me?

    TIA
    -- 
    Stuart
  2. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 17 Jul 2010 Link to this post

    Go figure!

    I've spent ages trying to work this out only to admit defeat and ask here for help.

    10 Minutes later and I'm struck by an idea and it works!!

    The trick, it appears is to change the class definition like this ...

    [Telerik.OpenAccess.Persistent(IdentityField = "id")]
    public class MyClass
    {
        int id;
        string name;
        [Telerik.OpenAccess.Transient()]
        public string note;
     
        [FieldAlias("id")]
        public int ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        [FieldAlias("name")]
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        [FieldAlias("note")]
        public string Note
        {
            get
            {
                return note;
            }
            set
            {
                note = value;
            }
        }
    }

    I'd love to know if this works 'cos this is how it's supposed to be done or it's just a happy accident.

    In any event, I can go to bed happy now!

    -- 
    Stuart
  3. DevCraft banner
  4. Answer
    Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 19 Jul 2010 Link to this post

    Hello Stuart Hemming,

     Yes the last setup that you did is a correct one in order to be able to bind an ASP .NET control to a datasource representing this class. The problem in the first case was that Note was declared as a field of the class instead of a property. The ASP .NET controls are looking for properties of a class when they perform data binding, so that is why "Note" was not visible. When you created the transient field "note" and then wrapped it up in a property, the details view is able to see the "Note" property and bind to it. I can only point you out that the "FieldAlias" attribute on the property that points to this transient field is not required, but however it does not cause any problems for the runtime if you have it.

    Sincerely yours,
    Zoran
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 19 Jul 2010 Link to this post

    Cheers Zohan,

    I'm learning that there are shedloads of things that I could learn about OA, but I have to say I am struggling to get much help from the docs.

    Still nice to know I'm going in the right direction.

    -- 
    Stuart
Back to Top