Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Form Controls & Page_Load View modes: 
User avatar
Member
Member
tspring-allegra - 10/3/2012 5:50:07 AM
   
Form Controls & Page_Load
Hi,

I'm trying to update some custom bizform code from Kentico4.1 to Kentico6 and have run into a problem I cant seam to fix.

Previously the 4.1 code wrote some data to a bizform textbox control during the Page_Load.

Which was simply:

protected void Page_Load(object sender, EventArgs e)
{
viewBiz.SiteName = "MySiteName";
viewBiz.FormName = "MyFormName";
this.viewBiz.BasicForm.DataRow["FormTextField"] = "My text";
}


But in 6, the function DataRow doesn't exist anymore.

I've tried using:

protected void Page_Load(object sender, EventArgs e)
{
viewBiz.SiteName = "MySiteName";
viewBiz.FormName = "MyFormName";
this.viewBiz.BasicForm.Data["FormTextField"] = "My text";
}

and

protected void Page_Load(object sender, EventArgs e)
{
viewBiz.SiteName = "MySiteName";
viewBiz.FormName = "MyFormName";
EditingFormControl TextField_ID = viewBiz.BasicForm.FieldEditingControls["FormTextField"] as EditingFormControl;
TextField_ID.Value = "My text";
}


But both produce a "System.NullReferenceException" error. Which I guess is because i'm not referencing the form properly.

How do I correctly access the form fields during Page_Load in Kentico6??

Thanks,
Tim

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 10/4/2012 4:13:26 AM
   
RE:Form Controls & Page_Load
Hello Tim,

this is caused because several changes were made to the Kentico v6 API.

Now you are supposed to use it in the following way:
((CMS.FormControls.EditingFormControl)viewBiz.BasicForm.FieldEditingControls["CustomField"]).Value = "your_text"
I hope this will help you.

Best regards,
Martin Danko

User avatar
Member
Member
tspring-allegra - 10/4/2012 4:56:07 AM
   
RE:Form Controls & Page_Load
Hi Martin,

Thanks for the help, but I still get the same error.

Basically I have a custom form webpart that automatically populates form fields with data from an external source. I've tried to write a test webpart with most of the custom code stripped so only the basic function of writing data into a form field remains.

My test code is:
BizformTest.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="~/CMSWebParts/Custom/BizformTest.ascx.cs" Inherits="CMSWebParts_Custom_BizformTest" %>

<cms:BizForm ID="viewBiz" runat="server" IsLiveSite="true" />


BizformTest.ascx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CMS.PortalControls;
using CMS.FormEngine;
using CMS.CMSHelper;
using CMS.GlobalHelper;

using CMS.SettingsProvider;
using CMS.DataEngine;
using CMS.EmailEngine;
using CMS.FormControls;

using CMS.PortalEngine;
using CMS.SiteProvider;
using CMS.EventLog;

using System.Data.SqlClient;


public partial class CMSWebParts_Custom_BizformTest: CMSAbstractWebPart
{
/// <summary>
/// Content loaded event handler
/// </summary>
public override void OnContentLoaded()
{
base.OnContentLoaded();
SetupControl();
}

/// <summary>
/// Reloads data for partial caching
/// </summary>
public override void ReloadData()
{
base.ReloadData();
SetupControl();
}

/// <summary>
/// Initializes the control properties
/// </summary>
protected void SetupControl()
{
if (this.StopProcessing)
{
// Do nothing
}
else
{
// Set BizForm properties
viewBiz.SiteName = "MySite";
viewBiz.FormName = "MyTestForm";
viewBiz.AlternativeFormFullName = "BizForm.MyTestForm.Test";
((CMS.FormControls.EditingFormControl)viewBiz.BasicForm.FieldEditingControls["TestFormTextField"]).Value = "My text";
}
}
}


Removing the line...
((CMS.FormControls.EditingFormControl)viewBiz.BasicForm.FieldEditingControls["CustomField"]).Value = "your_text";
...displays the alternative form and empty text field correctly, but as soon as I add the line I get a "System.NullReferenceException: Object reference not set to an instance of an object." error.

Is there a problem that the code is trying to write to the field before it is loaded?

I never had this problem in KenticoCMS4.1 though, with the exact same code but using the DataRow function.

Thanks,
Tim

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 10/4/2012 7:54:33 AM
   
RE:Form Controls & Page_Load
Hi Tim,

I finally realize that there is the difference in method calls in v6. In the SetupControl() method the BizForm isn't initialized so add your modification after the SetupControl() method, here is a little example of method which I've tried:
protected void SetupControl()
{
...
}

protected override void OnPreRender(EventArgs e)
{

var myForm = viewBiz.BasicForm.FieldEditingControls;
if (myForm != null)
{
myForm["message"].Value = "test";
}

base.OnPreRender(e);
}
Please, don't forget to test your Form to the null value.

Best regards,
Martin Danko

User avatar
Member
Member
tspring-allegra - 10/4/2012 8:58:01 AM
   
RE:Form Controls & Page_Load
Hi Martin,

That did the trick!

Though I did have to reference "CMS.FormControls.EditingFormControl" as in your original solution, otherwise it gave me a "no definition for 'Value'" error.

So I ended up with the combined version:

protected override void OnPreRender(EventArgs e)
{
if (viewBiz.BasicForm.FieldEditingControls != null)
{
((CMS.FormControls.EditingFormControl)viewBiz.BasicForm.FieldEditingControls["TestTextbox"]).Value = "Testing";
}

base.OnPreRender(e);
}
...which works perfectly.

Thanks again!!

Tim

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 10/4/2012 3:29:58 PM
   
RE:Form Controls & Page_Load
Hi Tim,

I'm very proud of you! It's great that you finally realize to combine previous examples into one functional method.

This solution is also your work :)

Best regards,
Martin Danko