I have a series of custom controls such as a DateBox, States/Country dropdown, etc in order to save me time for creating forms as well as allow me to load these controls into a form-builder i'm creating.
i have a class, FormField.cs that, depending on the element's type (input field, textbox, dropdown, DateBox, StateList, etc) dynamically create and return that control to be loaded into a placeholder.
I am able to set properties of the native controls with no issue. my issue is trying to set the properties on my custom user controls at runtime. currently, i use the LoadControl command and set it as a Control object (where I set its ID, thats all). I would like ot be able to set its object properties and this is where i'm having trouble.
I tried to cast it as cntStateList (NOT shown below), but the error I keep getting is that it cannot find the class.
Please help! Thanks!
Examples:
cntStateList.cs is the codebehind for cntStateList.ascx
cntStateList.cs
Code:
// C# Document
using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
public class cntStateList : System.Web.UI.UserControl
{
protected DropDownList drpStates;
protected void page_Load (Object s, EventArgs e)
{
DataSet dsStates = new DataSet();
dsStates.ReadXml(MapPath("../../XML/states.xml"));
drpStates.DataSource = dsStates;
drpStates.DataValueField = "abbrev";
drpStates.DataTextField = "name";
drpStates.DataBind();
}
public String Text
{
get { return drpStates.Text; }
}
public String SelectedValue
{
get { return drpStates.SelectedValue; }
}
}
Below is the FormField.cs class
FormField.cs
Code:
// inside a switch statement to get the field type...
case 5:
RadioButtonList rblRadioList = new RadioButtonList();
rblRadioList.ID = _strFieldLabel;
rblRadioList.DataSource = _cn.ExecQuery("select * from tblFieldValues where field_id = " + _iFieldId);
rblRadioList.DataValueField = "value";
rblRadioList.DataTextField = "value";
rblRadioList.DataBind();
return rblRadioList;
break;
case 6:
FileUpload fuFileUpload = new FileUpload();
fuFileUpload.ID= _strFieldLabel;
return fuFileUpload;
break;
// PROBLEM BEGINS HERE...
case 7:
Control cntTmpDateBox = (Control)Page.LoadControl("/cms/controls/cntDateBox/cntDateBox.ascx");
cntTmpDateBox.ID= _strFieldLabel;
return cntDateBox;
break;
case 8:
Control cntStateList = (Control)Page.LoadControl("/cms/controls/cntStateList/cntStateList.ascx");
cntStateList.ID= _strFieldLabel;
return cntStateList;
break;
// .....