DataRepeater with UserControl in Itemtemplate

0
0

Hi,

I have a custom Usercontrol with various controls in it (textboxes, datetimepickers etc). The Usercontrol has a DataSource property of type, let’s say, Order. I am using the usercontrol to display a single Order, by databinding the various controls to the properties of Order in the DataSource.

I would like to use the same Usercontrol as an ItemTemplate in a DataRepeater to display a List<Order>. What I did was to add the UserControl in the ItemTemplate and the do the bindings as follows:

var template = new ucOrder();

template.DataBindings.Add(“Title”, _bs, “Title”, true, DataSourceUpdateMode.OnPropertyChanged);
template.txtOrderPrice.DataBindings.Add(“Text”, _bs, “Price”, true, DataSourceUpdateMode.OnPropertyChanged);

The first binding is a property (Title) of the UserControl and works as expected. What the property does in its “set” is to pass the value to a label within the UserControl.
The second binding, however, does not work. I tried to bind to the Text property of a TextBox, without exposing it as a property of the UserControl. It does not give me an error, however, it does not work either.

Is there a way to do it without creating as many properties of the UserControl as its children controls?

Best,
Alex

  • You must to post comments
0
0

Hi Alex,

Two solutions basically:

  1. OnItemUpdate bind your user control to the current record as Levie did in the sample.
  2. Create bindable properties on the UserControl:
[Bindable(true)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string CustomerName { 
   get {return this.textBox1.Text;}
   set {this.textBox1.Text = value;}
}


This allows you to bind directly to the inner objects while keeping the private and gives you control on what to do with each property. It also shows in the designer under (DataBindings).

HTH

  • Levie (ITG)
    Just wanted to add to Luca’s answer, if you’re trying to bind the whole List item (Order), to the “Item” property on the UserControl, you’ll need to create an additional property (getter) within your Order class that returns a reference to the current instance. You can then use something like this: this.myUserControl.DataBindings.Add(“Item”, myDataSource, “GetOrder”). -Levie
  • You must to post comments
0
0

Hi Levie. I had added the following as a reply comment to your comment, but since you haven’t answered I suspect you haven’t noticed it, that’s why I’m repeating it here as an answer. Best, Alex.

Now you got me confused. In your original answer you mentioned the DataRepeater_ItemUpdate event in both scenarios, now you say that in the databound scenario the event is not needed. and in fact I cannot make it work without the event. In my mind there should be somewhere a line of code like myOrderView.DataBindings.Add(XXXX, RepeaterDatasource, YYYY), i.e. bind the usercontrol MyOrderView as a whole to the Datarepeater DataSource, but I do not know what property XXX of the usercontrol to bind to what property YYY of the datasource. This is what I would expect, i.e. to deal with the UserControl in the ItemTemplate, as we deal with let’s say a TextBox in the Itemtemplate, in which case XXX = “Text” and YYY = “OrderNumber”. It would be very helpful if you added the scenario without the ItemUpdate event in your nice example code.

  • You must to post comments
0
0

Hi Levie,

Of course, when I was trying to bind to the inner controls, I had first set their modifiers to public.

Thanks for your sample, it clears quite a few things. I was basically there with the databound usercontrol apprach, but was missing the ItemUpdate event.
A question: What is the difference between setting the dataRepeater.ItemCount and setting the dataRepeater.DataSource. I was using the DataSource and when I tried to use the ItemCount it gave me the error that ItemCount can be set only for VirtualMode = true.

Thanks again,
Alex

  • Levie (ITG)
    Hi Alex, VirtualMode allows you to create DataRepeaterItems on the fly, reducing the load times for large datasets. It only renders the controls in the browser when the client needs them (it requests the changes in ItemUpdate). You only need to use ItemUpdate and ItemCount when your DataRepeater is not databound. You can also set a prefetch (PrefetchItems) to load a certain number of entries on the client. When your DataRepeater is databound, you’ll want to configure it similar to how you had it with this.myOrderView.DataBindings.Add(). I hope this helps clear it up! Please let me know if it’s not clear or you have any other questions! Best regards, Levie
  • Alex Prinias
    Hi Levie. Now you got me confused. In your original answer you mentioned the DataRepeater_ItemUpdate event in both scenarios, now you say that in the databound scenario the event is not needed. and in fact I cannot make it work without the event. In my mind there should be somewhere a line of code like myOrderView.DataBindings.Add(XXXX, RepeaterDatasource, YYYY), i.e. bind the usercontrol MyOrderView as a whole to the Datarepeater DataSource, but I do not know what property XXX of the usercontrol to bind to what property YYY of the datasource. This is what I would expect, i.e. to deal with the UserControl in the ItemTemplate, as we deal with let’s say a TextBox in the Itemtemplate, in which case XXX = “Text” and YYY = “OrderNumber”. It would be very helpful if you added the scenario without the ItemUpdate event in your nice example code. Best regards, Alex
  • You must to post comments
0
0

If you want to use the designer, you can also create public getters and setters for each control property.

For example:

[Bindable(true)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string CustomerName 
{ 
    get { return this.textBox1.Text }
    set { this.textBox1.Text = value }
}

and then in the designer select the ucOrder UserControl on the ItemTemplate and set DataBindings.CustomerName to your DataSource.

  • You must to post comments
0
0

Hi Alex,

You won’t be able to bind to an unexposed control within the UserControl since it’s Protected.

There are a few ways you can approach this issue.

  1. Without a DataBinding Approach
    1. Add a setter for your ucOrder’ Order object
    2. Set the control’s data (ucOrder.textBox1.Text = value.xxx)
    3. Add a handler for the DataRepeater’s ItemUpdate event.
    4. Update the ucOrder’s Order object with the new value in ItemUpdate.
  2. With a DataBinding Approach
    1. Add a BindingSource to your ucOrder.
    2. In the setter for ucOrder’s Order, update the BindingSource’s DataSource to the new object.
    3. In your ucOrder_Load, add the appropriate databindings (set to the bindingsource).
    4. In DataRepeater_ItemUpdate, set the ucOrder’s Order to the new item.

If this is at all confusing, just look at the attached sample project :-).

If you have any questions, let me know!

Best regards,

Levie

  • You must to post comments
Showing 5 results
Your Answer

Please first to submit.