Hi,
I tried to use the FindForm() (and ParentForm) of a control in a DataRepeater and it does not seem to be set. Then I checked the FindForm() for the ItemTemplate and it is also null. Somehow this does not seem to work for the children of the DataRepeater control.
Best,
Alex
Hi Alex,
It works for me. I used FindForm() in the UserControl in the property setter:
[Bindable(true)] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public string Test { get { return this.textBox1.Text; } set { var form = FindForm(); this.textBox1.Text = $"Form: {form?.Name}; Value: {value}"; } }
It gets the form correctly. If you are not in a form but in a page, FindForm() will return null and FindPage() will return the page.
HTH
Hi Luca and Levie,
Luca you are right. I was creating and adding the usercontrols programmatically and, at the point I was asking for the Form, the controls were not yet placed on the form. My bad, sorry for the false alert!
All the best,
Alex
Hi Levie, thanks for your answer.
Following my previous question about binding a usercontrol as a whole in an ItemTemplate, I would like to get hold of the containing Form from within the UserControl. When I try this, I get null.
My structure is:
a. Form
b. UserControl with DataRepeater
c. UserControl in the ItemTemplate of b
From within c I get:
this.FindForm() = null
this.Parent = ItemTemplate
this.Parent.FindForm() = null
this.Parent.Parent = null (I would expect this to be either the DataRepeater control or UserControl b)
Hope this is clearer now.
Best,
Alex
Hi Alex,
You should be able to use FindForm or FindPage when working with DataRepeaterItemEventArgs (ItemUpdate, ItemClone, ItemAdded, etc) inside of the DataRepeaterItem property.
Here’s an example where you can use it:
private void dataRepeater1_ItemUpdate(object sender, DataRepeaterItemEventArgs e) { var item = e.DataRepeaterItem; AlertBox.Show(item.Controls["label1"].FindForm().Text); }
Please let me know if you have any other questions or this isn’t what you need!
Best regards,
Levie
Please login first to submit.