• Decrease Text SizeIncrease Text Size

Form State

Posted Date: 5/2/2023
    Printer Friendly Version   Email A Friend   Add This   Increase Text Size   Decrease Text Size
Example of retrieving data from Form State.

FormItem fiFirstName = FormState.Get(group, "FirstName");
FormItem fiLastName = FormState.Get(group, "LastName");

if (fiFirstName != null)
            HttpContext.Current.Trace.Warn("fiFirstName.Value", $"{fiFirstName.Value}");

if (fiLastName != null)
	HttpContext.Current.Trace.Warn("fiLastName.Value", $"{fiLastName.Value}");



Example of setting data to Form State.

string userNameId = "TestUserName";
NameValueCollection tbUserName = new NameValueCollection();
tbUserName["key"] = "formtextbox";
tbUserName["id"] = userNameId;
tbUserName["group"] = group;
tbUserName["defaultValue"] = "My New Value";

CpScripting.FormItem item = new CpScripting.FormItem(tbUserName);
FormState.Set(item.Group, item.Id, item.DefaultValue, tbUserName);

string newValues = "TestUserName control is not in Form State. Enter a value in the First Name Field and click submit.";
FormItem fi = FormState.Get(group, userNameId);
if (fi != null)
{
	newValues = String.Empty;
	foreach (string key in fi.Properties.Keys)
	{
		newValues += String.Format("<br>FormState.Properties:Key:{0} and Value:{1} ", key, fi.Properties.Get(key));
	}
}
if (CpScripting.FormItemExists(group, userNameId))
{
	FormItem fiUserName = FormState.Get(group, userNameId);
	newValues += String.Format("<br><br><b>UserName is: {0}</b>",Centralpoint.WebSite.FormState.HtmlDecode(fiUserName.Value));
}

dvFormStateNewValues.InnerHtml = newValues;



FormPreProcessor Diagram PreProcessor.png


Example of setting values in FormPreProcessor and retrieving/displaying that data in CpScripting file using Page.Items.

public string MultiPageForm(NameValueCollection collection)
{
    FormItem item = FormItemInit(collection);
    if (!String.IsNullOrEmpty(item.Alert)) return Alert(collection, item.Alert);

    this.FormAddPreProcessor(item, "MultiPageForm");
    String pageData = this.Page.Items["PageData"] as String;
    string html = !String.IsNullOrEmpty(pageData) ? 
        String.Format("<div class="\"alert\"">{0}</div>", pageData) : String.Empty;

    return html;
}


partial void FormPreProcessorMy(string key, FormGroup fg, ref bool isValid, bool isSubmit, string itemId)
{
    switch (key)
    {
        case "multipageform":
            isValid = this.MultiPageForm(fg, isValid, isSubmit, itemId);
            break;
    }
    return;
}


public bool MultiPageForm(FormGroup form, bool isValid, bool isSubmit, string itemId)
{
    Centralpoint.WebSite.FormItem fiItemName1 = form.Get("ItemName1");
    string itemName1 = (fiItemName1 != null) ? fiItemName1.Value : String.Empty;

    Centralpoint.WebSite.FormItem fiItemName2 = form.Get("ItemName2");
    string itemName2 = (fiItemName2 != null) ? fiItemName2.Value : String.Empty;

    Centralpoint.WebSite.FormItem fiItemName3 = form.Get("ItemName3");
    string itemName3 = (fiItemName3 != null) ? fiItemName3.Value : String.Empty;

    string formValues = String.Format("{0} {1} {2}", itemName1, itemName2, itemName3);


    this.Page.Items["PageData"] = formValues;
    return true;
}



Example of retrieving data from custom source and populating form controls.

	 private Dictionary<string, string> _customData;


	 public string GetCustomData(string parameter)
	 {
		if (String.IsNullOrEmpty(parameter)) return String.Empty;

		if (_customData == null)  
		{
		//TODO: retrieve information from your source repository. _customData can be stored in the session, page.Items or a view state to prevent additional lookups.
		_customData = new Dictionary<string, string>();
		_customData.Add("FirstName", "John");
		_customData.Add("LastName", "Doe");
		_customData.Add("CustomerIdentifier", "123456");
		_customData.Add("Email", "JaneDoe@gmail.com");
		_customData.Add("Phone", "1800-123-4564");
	 }


	return this._customData.ContainsKey(parameter) ? this._customData[parameter] : String.Empty;
}
 



Centralpoint Form

FirstName: [cp:scripting key='FormTextBox' id='FirstName' defaultValue='cpsys_Constant:CustomMethod:GetCustomData:FirstName' /]
LastName: [cp:scripting key='FormTextBox' id='LastName' defaultValue='cpsys_Constant:CustomMethod:GetCustomData:LastName' /]
CustomerIdentifier: [cp:scripting key='FormTextBox' id='CustomerIdentifier' defaultValue='cpsys_Constant:CustomMethod:GetCustomData:CustomerIdentifier' /]
Email: [cp:scripting key='FormTextBox' id='Email' defaultValue='cpsys_Constant:CustomMethod:GetCustomData:Email' /]
Phone: [cp:scripting key='FormTextBox' id='Phone' defaultValue='cpsys_Constant:CustomMethod:GetCustomData:Phone' /]




Keywords: Form, Form State



Related Code Samples Records
Related Taxonomy

Comments:

Be the first to leave a comment.
Please Login to post comments.