locked
Binding properties of a Custom control in a ListView RRS feed

  • Question

  • User-1729585438 posted

     I have a custom control derived from CompositeControl that I'm using in the EditItemTemplate and InsertItemTemplate of a ListView. The ListView's data source is an ObjectDataSource.

    I use <%#Bind('fieldname') %> to bind the properties of my custom control. I have found that the data will be displayed in my custom control, but when I try to update or insert, the data is not retrieved. I checked the onitemupdating event and saw that NewValues.Count was zero.

    Does my custom control need to implement somthing special in order to return its data?

    Thanks for any help.

    Monday, July 6, 2009 4:06 PM

Answers

  • User-1729585438 posted

     Never mind... I figured it out.

    My ObjectDataSource sets DataSourceTypeName. I was mistakenly thinking this provided some way of binding the entire object to my custom control, but I still need to <%# Bind('property') %> each property separately.

    I'd like to Bind() the whole object instead of each property, but it seems that is not possible. Container.DataItem has what I want, but seems to work only one way.

    An alternative would be to have the class referred to by DataSourceTypeName have a property such as:

    public MyClass Self
    {
       get { return this; }
       set {
               this.Prop1 = value.Prop1;
               this.Prop2 = value.Prop2;
               [etc....]
              }
    }

     

    Then I could use  <%# Bind('self') %>, instead of binding all the individual properties. However, I don't want to clutter up my classes with extra properties.

    What I have now works, so unless anybody has a more elegant solution, I'll stick with it.


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 14, 2009 4:31 PM

All replies

  • User-2106054853 posted

     Hi,

    I wrote a sample that can help you understand it:

    http://cid-2fa13ebc6cc8e80f.skydrive.live.com/self.aspx/Public/Understanding%7C_ITemplate%7C_lvl%7C_300.zip

    Wednesday, July 8, 2009 1:55 AM
  • User-1729585438 posted

    Thanks for your reply.

    However, my custom control is not a templated control. It inherits from CompositeControl. So I was unable to see how your ITemplate example relates to my problem. The solution may be in there, but I can't extract it from the template-related code.

    I created ItemUpDating and ItemInserting handlers for the ListView that contains the custom control. If I put a stop on them, I see that the ListViewUpdateEventArgs.NewValues and ListViewInsertEventArgs.Values IOrderedDictionaries have nothing in them.

    My current solution is to use these handlers to retrieve the values directly from my control and add them to the dictionaries.

    That's relatively simple, but I'd prefer to implement the custom control so that no such fiddling is necessary. I created an IOrderedDictionary in my control and set it as the default property, but that didn't work.

     

    Wednesday, July 8, 2009 6:53 PM
  • User-2106054853 posted

    Hi,

    Could you upload a demo project that can reproduce this issue to http://skydrive.live.com/ and paste the download link here for me to test? 

    Wednesday, July 8, 2009 11:45 PM
  • User-1729585438 posted

     Never mind... I figured it out.

    My ObjectDataSource sets DataSourceTypeName. I was mistakenly thinking this provided some way of binding the entire object to my custom control, but I still need to <%# Bind('property') %> each property separately.

    I'd like to Bind() the whole object instead of each property, but it seems that is not possible. Container.DataItem has what I want, but seems to work only one way.

    An alternative would be to have the class referred to by DataSourceTypeName have a property such as:

    public MyClass Self
    {
       get { return this; }
       set {
               this.Prop1 = value.Prop1;
               this.Prop2 = value.Prop2;
               [etc....]
              }
    }

     

    Then I could use  <%# Bind('self') %>, instead of binding all the individual properties. However, I don't want to clutter up my classes with extra properties.

    What I have now works, so unless anybody has a more elegant solution, I'll stick with it.


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 14, 2009 4:31 PM