I've created a custom form control for use with a document type. It contains two fields: a dropdown of available meetings, and a checkboxlist that populates with a list of sessions when a meeting is selected.
Everything is working fine as far as the display and writing back to both fields in the database when a user submits a form. The one thing I'm having trouble with is having the fields populate with the previously saved information when a user returns to the form. The dropdown populates correctly, but not the checkbox list.
.ascx code:
<cms:CMSUpdatePanel ID="pnlUpdate" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:DropDownList ID="ddMtg" runat="server" OnSelectedIndexChanged="ddMtg_SelectedIndexChanged" AutoPostBack="true" CssClass="form-control"></asp:DropDownList>
<asp:CheckBoxList ID="cblFunctions" runat="server" CssClass="checkbox checkbox-list-vertical"></asp:CheckBoxList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostbackTrigger ControlID="ddMtg" EventName="SelectedIndexChanged" />
</Triggers>
</cms:CMSUpdatePanel>
.cs code:
protected string connectionString = ConfigurationManager.ConnectionStrings["DataSource.iMIS.Connection"].ConnectionString;
/// <summary>
/// Gets or sets the value entered into the field
/// </summary>
public override object Value
{
get
{
return ddMtg.SelectedValue;
}
set
{
// Selects the matching value in the drop-down
EnsureItems();
ddMtg.SelectedValue = System.Convert.ToString(value);
}
}
/// <summary>
/// Returns an array of values of any other fields returned by the control.
/// </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()
{
object[,] array = new object[1, 2];
//array[0, 0] = "mtgCode";
//array[0, 1] = ddMtg.SelectedItem.Value;
array[0, 0] = "listTktFunctions";
List<string> listFunctions = new List<string>();
foreach (ListItem thisItem in cblFunctions.Items)
{
if (thisItem.Selected)
{
listFunctions.Add(thisItem.Value);
}
}
array[0, 1] = string.Join("|", listFunctions);
return array;
}
/// <summary>
/// Returns true if a meeting is selected. Otherwise, it returns false and displays an error message.
/// </summary>
public override bool IsValid()
{
if ((string)Value != "")
{
return true;
}
else
{
// Sets the form control validation error message
this.ValidationError = "Please select the iMIS meeting code.";
return false;
}
}
/// <summary>
/// Sets up the internal DropDownList control.
/// </summary>
protected void EnsureItems()
{
// Generates the options in the drop-down list
if (ddMtg.Items.Count == 0)
{
DataSet data = new DataSet();
string qs = @"
SELECT MEETING, TITLE
FROM [APSA_Test].[dbo].[Meet_Master]
ORDER BY MEETING";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter(qs, connection);
adapter.Fill(data);
}
if (data.Tables[0].Rows.Count > 0)
{
foreach (DataRow row in data.Tables[0].Rows)
{
ddMtg.Items.Add(new ListItem(row["MEETING"].ToString() + " - " + row["TITLE"].ToString(), row["MEETING"].ToString()));
}
}
}
}
protected void populateFunctions(string mtgCode)
{
DataSet data = new DataSet();
string qs = @"
SELECT PRODUCT_CODE, PRODUCT_CODE
FROM [APSA_Test].[dbo].[Product_Function] pf
WHERE PRODUCT_CODE LIKE @mtgCode + '/%'
ORDER BY pf.PRODUCT_CODE";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter(qs, connection);
adapter.SelectCommand.Parameters.Add("@mtgCode", SqlDbType.NVarChar, 10);
adapter.SelectCommand.Parameters["@mtgCode"].Value = mtgCode;
adapter.Fill(data);
}
if (data.Tables[0].Rows.Count > 0)
{
foreach (DataRow row in data.Tables[0].Rows)
{
cblFunctions.Items.Add(new ListItem(row["PRODUCT_CODE"].ToString(), row["PRODUCT_CODE"].ToString()));
}
}
}
protected void ddMtg_SelectedIndexChanged(object sender, System.EventArgs e)
{
cblFunctions.Items.Clear();
populateFunctions(ddMtg.SelectedValue);
}
/// <summary>
/// Handler for the Load event of the control.
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
// Ensure drop-down list options
EnsureItems();
}
Any insight is much appreciated.
Thanks,
Pam