How To Dynamically Add Controls to a form

benyamin jain asked on March 29, 2018 12:40

HI

is it possible to add controls dynamically to form like this video tutorial?

How To Dynamically Add Controls to a form

Correct Answer

Trevor Fayas answered on March 29, 2018 17:37

Benyamin, before you go that route...you may want to take a look the new submission i put on the Marketplace, it is for your exact situation:

https://devnet.kentico.com/marketplace/inline-controls/multiple-child-form

This allows you to set up a Multiple "Child" form for your form, AKA you could have a Certificate Submission Form, with a Child form of "Certificate Item" that they can add any number and submit.

Just read the PDF and follow along.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on March 29, 2018 14:37

Asp.net form, yes, basically in code you do a

TextBox myTextboxControl = new TextBox();
// Configure text box properties
MyPanelControl.Controls.Add(myTextboxControl);

You will have a hard time adding controls to a Kentico BizForm however, but it should still be possible by digging into the sub controls and replacing controls, not sure why you would though.

If you search Kentico's entire solution for ".Controls.Add(" you will find many examples.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 29, 2018 14:40

A form or a page/webpart? They are different things inside and outside of Kentico. Dynamically adding a control to a page/webpart is as simple as:

<asp:PlaceHolder ID="ph1" runat="server"/>

protected void Page_Load(object sender, EventArgs e)
{
    LiteralControl lit = new LiteralControl();
    lit.Text = "Hello World!";
    lit.ID = "litText";
    ph1.Controls.Add(lit);
}
0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 29, 2018 15:21

I can really really think of realistic scenario when need to add a control. You probably will be better off hiding/showing control in the form based on some conditions. You should check how to work with depending fields

0 votesVote for this answer Mark as a Correct answer

benyamin jain answered on March 29, 2018 17:23

Thanks Peter Mogilnitski

But what is really need is one field with multiple values that is based on user needs.

for example we have a UserCertificate Field that user enter all certificates that he/she passed.

probably he/she passed only one certificate or multiple.

when he/she has multiple certificate then we ask him to add another one in one place.we see such functionality in grouped attachments where user can only upload one attachment or multiple attachments.

as a result the scenario you suggest is not suited here.

0 votesVote for this answer Mark as a Correct answer

benyamin jain answered on March 29, 2018 17:28 (last edited on March 29, 2018 17:30)

Thanks Brenden Kehren

I need this functionality in custom table form.a group of fields filled by user and user can add another group of same fields if required (see my previous reply).adding controls done in AJAX way.

also i need to develop information gathering form manually and i can not use kentico built in form engine.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 29, 2018 17:36

There are a couple ways to handle this:

  • Create a custom usercontrol - least desired as it doesn't follow Kentico best practices
  • Create a custom webpart to handle the custom logic - ok but can be very complicated in some aspects and have many dependencies. Webparts are supposed to be reusable, this would not be.
  • Create a global event handler to handle the custom logic - ok but again can be very complicated and dependent on other items.
  • Create a custom form control -most desireable as it is reusable across all objects and very dynamic in nature. Your form control would handle adding 1 or multiple items to whatever table you decide and be reusable across other objects like page types, forms and custom tables.
0 votesVote for this answer Mark as a Correct answer

benyamin jain answered on March 29, 2018 18:06

Thanks Trevor Fayas

That is what i need exactly

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.