Loading

Building a parent-child relationship with the forms

In this post I'd like to walk you through a process of building parent-child relation with the forms. Those, who know Kentico and available range of features could ask absolutely obvious question: why would I build parent-child relation with forms while there are modules, or, at least, custom tables? I can see two good reasons for doing this:
  • parent object is already created and there is cost related with conversion it to a module class
  • it allows non-technical users, such as content editors, creation of the parent-child data structure

Recently I've got a request to add 7 exactly the same sections to an existing form. The form was job application and already contained huge list of fields. It was presented on front end with great Multi-steps Online Forms web part from Kentico markeplace and that was quite beneficial for me (I'll explain why later). Those 7 sections I needed to add were previous employments. Each section contained around 20 fields, so I could end up adding 140 fields to an existing form. But this approach did not seem to me enough flexible: what if user will populate just 2 sections? Or what if user would like to enter more than 7 employments? Also adding 140 fields is not the most exciting thing to do, so I've starter to design some better solution, which I want to share with all of you.

Goal

So I thought about creation of a form control that would list all the child record with possibility to edit them as well as adding new one. 

Prerequisites

Before beginning actual development we need to create 2 forms to run tests against. Let's pretend we need to build form for event registration, which allows user to register his friends or colleagues as well. So the first form is EventRegistration and second one Attendee that will store any additional attendee that user registers along. EventRegistration form will contain one extra field for attendees, which could be with or without database representation, and it use SubForm (control we are building) as a form control. We will get back to this field later. There is requirement for Attendee form: it should contain a fields-reference to its parent object - EventRegirstration record.    

Control

Following is the markup for the control that would allow us to reach my goal:
  1. <asp:UpdatePanel ID="up" runat="server">
  2. <ContentTemplate>
  3. <asp:Repeater ID="rptRecords" runat="server" OnItemDataBound="rptRecords_ItemDataBound">
  4. <ItemTemplate>
  5. <cms:BizForm ID="bfItem" runat="server" IsLiveSite="true"
  6. FormName='<%# Eval("Form") %>' SiteName='<%# Eval("Site") %>' AlternativeFormFullName='<%# Eval("AlternativeForm") %>'
  7. ItemID='<%# Eval("ID") %>' />
  8. </ItemTemplate>
  9. </asp:Repeater>
  10. <cms:BizForm ID="viewBiz" runat="server" IsLiveSite="true" FormClearAfterSave="true"/>
  11. </ContentTemplate>
  12. </asp:UpdatePanel>

As you can see it contains a repeater listing existing records. Each item of the repeater is a BizForm. Also control contains a separate BizForm, that would be responsible for creation of a new record. Real advantages of using BizForm here is the fact that it handles creation and/or update of the records as well as it knows how to present an item either by building default layout, or using alternative form. The entire control is wrapped with update panel, so it will not cause page reload, which is always good from user experience prospective.

As we are developing custom form control, control should inherit FormEngineUserControl and implement its methods and property: IsValid(), GetOtherValue(string name) and Value. We need to add extra properties to the control:
  • BizFormName - code name of the child form
  • AlternativeFormName - layout of the child form (optional)
  • SiteName
  • ReferenceColumnName - this will tell our custom control which form fields should be used to store reference to parent record (EventRegistration in our case)
I've also added private property in order to get easy access to parent form object:
  1. private BizForm ParentForm
  2. {
  3. get { return (BizForm)(this.NamingContainer); }
  4. }

Now let's add data binding for a list of added attendees: 
  1. private void BindData()
  2. {
  3. var classInfo = CacheHelper.Cache(cs => GetFormClassInfo(cs), new CacheSettings(30, string.Format("{0}|{1}", "formclassinfo", BizFormName)));
  4. var items = BizFormItemProvider.GetItems(classInfo.ClassName)
  5. .Where(i => i.GetIntegerValue(ReferenceColumnName, 0) == ParentForm.ItemID)
  6. .Select(i => new
  7. {
  8. Form = BizFormName,
  9. Site = SiteName,
  10. AlternativeForm = AlternativeFormName,
  11. ID = i.ItemID
  12. });
  13. rptRecords.DataSource = items;
  14. rptRecords.DataBind();
  15. }
  16. private DataClassInfo GetFormClassInfo(CacheSettings cs)
  17. {
  18. BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo(BizFormName, SiteContext.CurrentSiteID);
  19. DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
  20. return formClass;
  21. }

As you can see each item of the repeater is a BizForm control and we are passing just basic setting to it: form name, layout, site name and record ID, so now we need to make sure BizForm will load data of an actual record. This could be achieved by adding one line of code to the item data bound event handler:
  1. protected void rptRecords_ItemDataBound(object sender, RepeaterItemEventArgs e)
  2. {
  3. (e.Item.FindControl("bfItem") as BizForm).ReloadData();
  4. }

Once we have code, that will load data we can start adding code, that will setup the control:
  1. protected override void OnLoad(EventArgs e)
  2. {
  3. base.OnLoad(e);
  4. if (ParentForm.ItemID > 0 && rptRecords.Items.Count == 0)
  5. {
  6. BindData();
  7. }
  8. viewBiz.FormName = BizFormName;
  9. viewBiz.SiteName = SiteName;
  10. viewBiz.AlternativeFormFullName = AlternativeFormName;
  11. viewBiz.ControlContext.ContextName = CMS.ExtendedControls.ControlContext.LIVE_SITE;
  12. viewBiz.OnBeforeSave += viewBiz_OnBeforeSave;
  13. viewBiz.OnAfterSave += viewBiz_OnAfterSave;
  14. if (!IsPostBack)
  15. {
  16. viewBiz.ReloadData();
  17. }
  18. }

Here we simply setup BizForm control responsible for adding new records (Attendees) and calling data load for already created items. Also we assign for before and after save evens. Here are appropriate handlers:
  1. void viewBiz_OnAfterSave(object sender, EventArgs e)
  2. {
  3. ParentForm.ReloadData();
  4. }
  5. void viewBiz_OnBeforeSave(object sender, EventArgs e)
  6. {
  7. if (ParentForm.ItemID <= 0)
  8. {
  9. ParentForm.SaveData("");
  10. }
  11. if (string.IsNullOrEmpty(ReferenceColumnName) || ParentForm.ItemID <= 0)
  12. {
  13. viewBiz.StopProcessing = true;
  14. }
  15. viewBiz.Data.SetValue(ReferenceColumnName, ParentForm.ItemID);
  16. }

Before save handler sets reference to the parent record (EventRegistration) as well as saves parent record in case it has not been saved yet. Save after handler ensures parent form reload.

Register Form Control

Now it is time to register our new control in Kentico. Go to Form Controls application and create a new form control. Set following values:
  • Display name: SubForm
  • Type: Multifield
  • File name: select path to your control
  • Use control for: Text
  • Show control in: Forms
Save your changes and navigate properties tab. Add following properties:
  • BizFormName: Text, Required, BizForm selector
  • AlternativeFormName:Text, Alternative form selector
  • SiteName: Text, Site selector
  • ReferenceColumnName: Text, Text box
Now SubForm control should be available as editing control for text fields in your form.

SubForm Usage

Let's try to set up SubForm as an editing control for EventRegistration form mentioned earlier.
First of all we need to make sure that field for storing reference to EventRegistration has been added to Attendee form. I've called it ParentId.
Now navigate fields on EventRegistration form and new one called Attendees. Set Text as type and select SubForm as form control, expand Editing control setting section and configure child object: select Attendee form for Biz Form field and ParentId for Reference Column Name. You may also specify alternative form for child object. Save your changes. That should be it.

Variations

I've mentioned Multi-Step Online form web part earlier, saying it gave me some advantages. So the main advantage was that it saves forms on each step, which mean there is no necessary to check if parent record has been saved, so no need to save it. Saving of parent record introduces complication and extra processing to this control that I'd recommend to avoid if possible. So if you're good with the scenario where user has to save parent object before adding children, you could optimize this control a bit.
However, example in this post gives you generic control that could be reused with multiple scenarios.

Result

As a result of this 'lab' you should get something like this. If you don't  want to mess around with the pieces of code provided in this blog, you can download export packages for version 8.2 and 9 directly from our site, import them to your site and enjoy!


Comments
New Kingroot Download
Thanks for the tips
2/20/2018 6:14:56 AM

Kolkata Escorts
Renu Das Kolkata Escorts Services has gorgeous females provides Independent Escorts Service in Kolkata call girls at 100% satisfaction with VIP models. Provided Kolkata escorts at our agency are professional in nature and are eager to serve you at your place.
<a href="http://www.renudas.in/">Kolkata Escorts</a>
<a href="http://www.renudas.in/">Escorts in Kolkata</a>
http://www.renudas.in
2/9/2018 8:24:15 PM

kolkata escorts
Beauti Queen amazing kolkata escorts agency alwyas ready for you at your doorstep. Beauti Queen provide you high profile independent girl in kolkata, escorts service in kolkata, kolkata escorts. If you are looking for the ultimate encounter in Kolkata then I am your sophisticated young Kolkata escorts that provides upscale companionship for making your night becomes true.
<a href="http://www.kolkataqueen.com/">Escorts Service in Kolkata</a>
<a href="http://www.kolkataqueen.com/">Escorts in Kolkata</a>
http://www.kolkataqueen.com/
2/8/2018 11:51:35 PM

app apk download
The post of your article is amazing and interesting, visit our web app apk download at http://appapk.download/, to get info about android apps apk download free
2/8/2018 3:00:09 AM

iphone apk download
I see the article on your web very good and interesting ,, do not forget visit also my web iphone apk download - http://iphoneapk.download/
You can get info about all of latest update iphone ios apk download
2/7/2018 10:08:44 AM

app download 2018
Your website is very interesting, visit our web for info about (all apps download 2018 for android smartphone), at (app download 2018 - http://appdownload2018.website/)
2/7/2018 8:38:02 AM

app for windows download
Your website is very interesting, visit our web to get info about Download app for PC Desktop Dekstop Windows, in app for windows download - http://appforwindows.download/
2/7/2018 7:19:42 AM

app for pc windows
I see your article in your web very nice and interesting ,, do not forget visit also my web at app for pc windows - http://appforpcwindows.download/
. You can get info about apps for pc windows 10/8/7 free download
2/7/2018 6:50:42 AM

dissertation writing service
I want you to thank for your time of this wonderful read!!! I definately enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog
2/7/2018 5:50:39 AM

app for mac download
Your article is interesting, visit also our web. Get everything about mac app store download free at app for mac download - http://appformac.download/
Please check for the most popular apps
2/7/2018 3:34:47 AM

app for iphone download
Hi, i am very interested in your website, check also our web at app for iphone download
http://appforiphone.download/
And get info about Download app for iphone IOs & Android for free
2/6/2018 9:29:45 AM

app 2018 download
I am very interested in your article, visit also our web at app 2018 download
http://app2018.download/
Get info about Latest app version update on 2018 download free
2/6/2018 8:03:10 AM

Best App Market
Your articles are very interesting, check our web as well at Best App Market
http://bestappmarket.download/
There you can get info about Best App Market to download app/apk for android/iOs
2/6/2018 5:41:01 AM

apk for ios Download
your article is very good and the info is interesting ,, do not forget visit also my web apk for ios Download - http://apkforios.download/
Download apk for ios for free
2/6/2018 3:36:44 AM

joseph
hallo, I see your web very nice and interesting ,, do not forget visit also my web android game download
http://anroidgame.download/
Popular android games download list for free
2/5/2018 7:56:01 AM

shakeela
hallo, i see your web very nice and interesting ,, do not forget

visit also my web app1 download
You can get info about app download free for android iOs
2/5/2018 5:33:52 AM

Watch Movies
Movie Box is a free an ultimate entertaining application available to install for smart devices. This allows users to watch/download any movie, tv-show with HD quality for Free!
1/24/2018 6:12:26 AM

Free Movie Box App
MovieBox allows downloading and watching movies in your smartphones for free. It is one of the most popular apps
1/24/2018 6:05:05 AM

es file explorer
To Install ES File Explorer, you need to have APK file on your device.
1/12/2018 7:02:14 AM

Clark
The walk through on the various process of building parent-child relation with the forms really helped a lot in understanding the whole process.
1/4/2018 11:18:25 AM

cialis_�FFFFFFC3�FFFFFFBCberdosiert
History of generic pills for cheap cheaest professional world wide no .
12/29/2017 7:19:13 AM

homework helper
Your website is really cool and this is a great inspiring article. Thank you so much.
12/23/2017 11:56:11 AM

Terrarium tv latest apk
download terrarium tv apk for watching hd tv shows and movies online on any device. Download terrarium tv
12/19/2017 8:23:05 AM

Inspiring Top 10 Dubbed Anime
Thanks For Sharing this Wonderfull post really appreciating this post keep sharing Awesome post
12/14/2017 12:42:52 PM

Desert Safari Dubai
I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working and share more posts, you are doing great job.
11/30/2017 6:08:47 AM

TerrariumTVApk
TerrariumTVApk : Generally people like to spend their free time by watching movies or TV shows or video.
11/27/2017 5:56:13 AM

run 3 Game
thanks a lot for sharing this awesome post
11/23/2017 8:32:00 PM

Make Him Desire You
I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.
11/23/2017 6:20:12 PM

His Secret Obsession Review
A very awesome blog post. We are really grateful for your blog post. combat, law enforcement You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work.
11/22/2017 6:40:36 PM

SMS Gateway
Business SMS might be the most effective marketing method for its direct approach in communicating with consumers but it is also one of the cheapest to execute. One only needs creativity and a reliable SMS service.
11/20/2017 1:58:59 PM

replica watches
nice!
11/17/2017 3:41:52 AM

100 Questions
I think about it is most required for making more on this get engaged
11/16/2017 7:53:26 PM

Essential Oils For Nausea
Excellent effort to make this blog more wonderful and attractive.
11/15/2017 10:03:37 PM

hair extensions
Qi Qi Liu's elegant shape will be cheap hair extensions easy to create a sense of playfulness
11/10/2017 1:47:00 AM

waqar mazhar
nice content
10/31/2017 4:27:51 PM

terrariumtvapkdownloads
match effective article.
10/22/2017 3:34:07 PM

canvas montclair
Your content is nothing short of brilliant in many ways. I think this is engaging and eye-opening material. Thank you so much for caring about your content and your readers.
10/22/2017 5:40:11 AM

ten
Incidentally I have been ordering and Cialis not for the first month.
10/14/2017 5:39:20 AM

Dissertation Helper
Designers must comprehend the estimation of this article since it contain a great deal of good snippet of data. You researched great before distribute this article. Thusly, Happy to get this site because of educational articles.
10/4/2017 4:51:36 AM

digital signage cloud
You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant!
9/28/2017 12:50:37 PM

blackwork tattoo
You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant!
9/28/2017 11:12:58 AM

Volusion Customization Services
Volusion Customization Services
9/21/2017 11:45:44 AM

cartoon hd latest apk
nice post thank you for sharing :)
9/14/2017 4:12:39 PM

rolex replica watches
yhjhgj
9/12/2017 3:32:46 AM

terrariumtv app for 2016 movies
terrarium tv is most used movie app in recent times with awesome features but sometimes it is not working with an overload of users at that moment apps like terrarium tv is a benefit for us
9/11/2017 3:53:38 PM

Help with Assignment writing
I think this is an extraordinary component and I need to dole out parental rights to administrators of some of my organizations. Be that as it may, the vast majority of my organizations have more than one administrator and this resembles every child can just have 1 parent. Is there an approach to make it so more than one individual can be the parent for all representatives of that firm?
9/9/2017 7:36:41 AM

UK Dissertation Writing Service
Certain child years encounters are long long-term enough to form our individualists and actions throughout our mature lifestyles, and connections with friends (whether excellent or bad) are certainly among these
9/1/2017 12:46:16 PM

Questions
Amazing, this is great as you want to learn more, I invite to This is my page.
7/26/2017 4:15:20 AM

Can Someone Do My Assignment
i imagine if a baby kills the relationship, it wasn't a lot of a relationship to commence with and may want to were killed some incorrect way finally!!! It gives you an entire new measurement - you're suited. there is not any longer something extra endearing to me than seeing my 2 year old get all satisfied and excited at the same time as Daddy receives homestead from artwork. The satisfied circumstances like that spread the love round - a baby's love is truly a organic element. on the turn part, gaining understanding of a thanks to address the complicated circumstances at the same time is a even greater progression interior the marriage, and in case you actually love one yet another you a thanks to help and help one yet another in a way you does no longer could do otherwise.
7/6/2017 7:49:02 AM

wholesaletabletcosts.com
It’s a really interesting form to think of. The walk through on the various process of building parent-child relation with the forms really helped a lot in understanding the whole process. The explanations did help to ease the complexity of the whole program.
6/29/2017 11:03:17 AM

sam
Great Blog
6/29/2017 11:02:36 AM

rolexes
great
5/10/2017 9:31:55 AM

like followers
Interesting for reading.
4/28/2017 8:04:18 AM

computer disposal companies
You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant!
4/10/2017 1:40:34 PM

 Security code