Mauro Bilotti
Mauro Bilotti

Reputation: 6242

Load data in custom form by using Kentico 9

I searched the whole web and I couldn't found any good topic which expose the proper way to do it.

I have a very simple web which I developed using Kentico 9 CMS. This web only contains two subpages and a header to navigate between those.

The "Home" subpage contains a custom form which remains connected to a SQL table which is populated every time you press submit with certain data.

enter image description here

On the other hand, the other page, shows the stored data by using a custom web part which connects to the DB by using BizFormItemProvider and this object is used as a layer to binding the data in a control.

enter image description here

Now is my point. If you see, there is a button to "Edit" a certain row and my intention is to Redirect to "Home" (which contains the form) and sending via QueryString the ID of the row attempted to edit.

I was unable to understand how can you re-fill the form with its data using an ID.

Maybe because I never worked with a CMS before, I'm looking the development such as pure ASP.NET and it could not be the correct one.

Upvotes: 0

Views: 753

Answers (2)

Kristian Bortnik
Kristian Bortnik

Reputation: 838

Custom

Given that your solution uses a custom form for entering the data, as well as a custom web part for listing the stored data, you will need to use a custom solution to handle the editing of data, as well.

In the custom webpart on the Home page, in a load event, you can retrieve the form data and set the values on the form controls.

protected void Page_Load(object sender, EventArgs e)
{
    // Ensure that the form is not being posted back,
    // to prevent entered data from being overwritten
    if(!IsPostBack)
    {
        // Get the form item ID from the query string
        var personId = QueryHelper.GetInteger("personId", 0);
        if(personId > 0)
        {
            // Get the biz form item, and set form control values
            var bizFormItem = BizFormItemProvider.GetItem(personId, "customFormClassName");
            txtFirstName.Text = bizFormItem.GetStringValue("FirstName", string.Empty);
        }
    }
}

Similarly, when Submit is clicked, you can update the existing form item with the new data

protected void btnSubmit_OnClick(object sender, EventArgs e)
{
    // Get the form item ID from the query string
    var personId = QueryHelper.GetInteger("personId", 0);
    if(personId > 0)
    {
        // Retrieve the existing biz form item,
        // and update it from the form control values
        var bizFormItem = BizFormItemProvider.GetItem(personId, "customFormClassName");
        bizFormItem.SetValue("FirstName", txtFirstName.Text);
        bizFormItem.Update();
    }
    else
    {
        // Your code for inserting a new form item...
    }
}

The Kentico way

You should really consider using the Kentico form engine for accomplishing this task. Instead of using a custom form for entering the data, use the built-in On-line form webpart.

The benefits are numerous, such as:

  • Ability to set the form layout through the CMS, and use alternative layouts
  • Automatic confirmation email to the submitter of the form, as well as notification emails to the administrators

To accomplish your task, you can customise the On-line form webpart to support loading of existing data. In the bizform.ascx.cs file, add code to the SetupControl method:

protected void SetupControl()
{
    if (StopProcessing)
    {
        // Existing code...
    }
    else
    {
        // Existing code...

        // Get the form item ID from the query string
        var personId = QueryHelper.GetInteger("personId", 0);
        if(personId > 0)
        {
            // Get the biz form item, and set form control values
            var bizFormItem = BizFormItemProvider.GetItem(personId, "customFormClassName");
            if(bizFormItem != null)
            {
                // Set the item ID
                viewBiz.ItemID = bizFormItem.ItemID;
            }
        }
    }
}

This will automagically switch the form to Edit mode, instead of Insert mode, as soon as you set the ItemID property. Clicking the Submit button will save changes on the existing form item. You will not need to worry about validation in your code, and inserting data will still work.

Upvotes: 3

Laura Frese
Laura Frese

Reputation: 331

Is this a contact form that you are using Kenticos built in form application for, or is it a custom form? If it is a custom form you can create a transformation with a link that will contain the ID. If it is a biz form, you can still create a transformation in Page Types (create a new page type and select "The page type is only a container without custom fields"), then write a custom query to get the biz form data, and use a repeater to display the data with that transformation.

Upvotes: 1

Related Questions