I am doing my fields as follows:
Field 1 is "Location" (like the Office example on the Corporate site)
When the Location is chosen I populate Field 2 "Phone Numbers" with all phone numbers associated with that particular location. This is working great!
I am having trouble with saving the Phone Number. I chose a location and then the phone list populates, I hit save and it stays on the screen, but if I navigate away and come back the phone does not save. The location does save. Here is my code (It is not finished and is a work in progress):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// Kentico Specific
using CMS.FormControls;
using CMS.GlobalHelper;
using System.Text.RegularExpressions;
using CMS.DataEngine;
using CMS.SettingsProvider;
using System.Data;
public partial class CMSFormControls_LocationPicker : FormEngineUserControl
{
/// <summary>
/// Gets or sets the value entered into the field
/// </summary>
public override Object Value
{
get
{
return DrpLocation.SelectedItem.Value;
}
set
{
// Ensure drop down list options
EnsureItems();
DrpLocation.SelectedValue = System.Convert.ToString(value);
}
}
/// <summary>
/// Property used to access the Location parameter of the form control.
/// </summary>
public string Phone
{
get
{
return ValidationHelper.GetString(DrpPhoneList.SelectedItem.Value,"");
}
set
{
SetValue("Phone", value);
}
}
/// </summary>
/// <returns>It returns an array where the first dimension is the attribute name and the second is its value.</returns>
public override object[,] GetOtherValues()
{
if (!String.IsNullOrEmpty(Phone))
{
object[,] result = new object[1, 2];
result[0, 0] = "Phone";
result[0, 1] = DrpPhoneList.SelectedItem.Value; //this is the variable that you will need to set to your selected state name.
return result;
}
else
{
return base.GetOtherValues();
}
}
//public override object[,] GetOtherValues()
//{
// object[,] array = new object[1, 2];
// array[0, 0] = "Phone";
// array[0, 1] = DrpPhoneList.SelectedItem.Value;
// return array;
//}
/// <summary>
/// Returns true if selected. Otherwise, it returns false and displays an error message.
/// </summary>
public override bool IsValid()
{
if ((string)Value != "")
{
return true;
}
else
{
// Set form control validation error message.
this.ValidationError = "Please choose a location.";
return false;
}
}
/// <summary>
/// Sets up the internal DropDownList control.
/// </summary>
protected void EnsureItems()
{
if (DrpLocation.Items.Count == 0)
{
DrpLocation.Items.Add(new ListItem("(select a location)", ""));
// Get SQL query text
string sqlText = "SELECT TestOfficeID, DocName FROM custom_TestOffice";
if (!sqlText.Contains("@"))
{
DataSet ds = null;
try
{
ds = ConnectionHelper.ExecuteQuery(sqlText, null, QueryTypeEnum.SQLQuery, false);
}
catch { }
if (!DataHelper.DataSourceIsEmpty(ds))
{
// Initialize control
DrpLocation.DataSource = ds;
DrpLocation.DataValueField = ds.Tables[0].Columns[0].ColumnName;
DrpLocation.DataTextField = ds.Tables[0].Columns[1].ColumnName;
DrpLocation.DataBind();
// Register OnAfterDataLoad handler
//this.Form.OnAfterDataLoad += new BasicForm.OnAfterDataLoadEventHandler(Form_OnAfterDataLoad);
////// HERE IS WHERE i COMMENTED IT OUT ///////
}
}
}
}
/// <summary>
/// Handler for the Load event of the control.
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Ensure drop-down list options
EnsureItems();
}
}
protected void DrpLocation_SelectedIndexChanged(object sender, EventArgs e)
{
string LocationID = DrpLocation.SelectedItem.Value;
DrpPhoneList.Items.Clear();
if (DrpPhoneList.Items.Count == 0)
{
//DrpPhoneList.Items.Add(new ListItem("(select a location)", ""));
// Get SQL query text
string sqlText = "SELECT customtable_cnphones.PhoneNumber, customtable_cnphones.PhoneNumber FROM customtable_cnphones INNER JOIN custom_TestOffice ON custom_TestOffice.TestOfficeID=customtable_cnphones.PhoneLocation WHERE customtable_cnphones.PhoneLocation =" + LocationID;
if (!sqlText.Contains("@"))
{
DataSet ds = null;
try
{
ds = ConnectionHelper.ExecuteQuery(sqlText, null, QueryTypeEnum.SQLQuery, false);
}
catch { }
if (!DataHelper.DataSourceIsEmpty(ds))
{
// Initialize control
DrpPhoneList.DataSource = ds;
DrpPhoneList.DataValueField = ds.Tables[0].Columns[0].ColumnName;
DrpPhoneList.DataTextField = ds.Tables[0].Columns[1].ColumnName;
DrpPhoneList.DataBind();
// Register OnAfterDataLoad handler
//this.Form.OnAfterDataLoad += new BasicForm.OnAfterDataLoadEventHandler(Form_OnAfterDataLoad);
////// HERE IS WHERE i COMMENTED IT OUT ///////
}
}
}
}
}