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
Hi Alex,
Two solutions basically:
[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
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.
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
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.
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.
If this is at all confusing, just look at the attached sample project :-).
If you have any questions, let me know!
Best regards,
Levie
Please login first to submit.