I am using the following code to add items to a panel but the problem is its only allowing me to add one instance of a control, I want to be able to add as many items as I want to the panel and i thought the following code would acheieve that.
if (ctrlType.SelectedValue == "TextBox")
{
listElements.Add(new XElement(@"TextBox", new XElement("name"),
new XElement("Type", "System.String"),
new XElement("displayName", this.txtTitle.Text.ToString()),
new XElement("length", txtMaxLength.Text.ToString()),
new XElement("key", false),
new XElement("required", chkRequired.Checked)));
TextBoxUserControl tb2 =
(TextBoxUserControl)LoadControl(@"~\UserControls\TextBoxUserControl.ascx");
tb2.XMLText = listElements;
tb2.Text = txtTitle.Text;
tb2.Name = "TextBox" + " " + ctrlSource.SelectedValue.ToString() + " " + Guid.NewGuid().ToString();
pnlControls.Controls.Add(tb2);
}
if (ctrlType.SelectedValue == "DropDown" && ctrlSource.SelectedValue == "0") {
listElements.Add(new XElement(@"ClassficationEnum", new XElement("name", "TestForm"),
new XElement("Guid", "1f77f0ce-9e43-340f-1fd5-b11cc36c9cba"),
new XElement("Type", "System.String"),
new XElement("displayName", this.txtTitle.Text.ToString()),
new XElement("length", txtMaxLength.Text.ToString()),
new XElement("key", false),
new XElement("required", chkRequired.Checked)));
Classfication clafficationDp =
(Classfication)LoadControl(@"~\UserControls\Classfication.ascx");
clafficationDp.ID = "clafficationDp" + " " + ctrlSource.SelectedValue.ToString() + " " + Guid.NewGuid().ToString();
clafficationDp.Text = txtTitle.Text;
pnlControls.Controls.Add(clafficationDp);
}
else if (ctrlType.SelectedValue == "DropDown" && ctrlSource.SelectedValue == "1")
{
listElements.Add(new XElement(@"SourceEnum", new XElement("name", "TestForm"),
new XElement("Guid", "5d59071e-69b3-7ef4-6dee-aacc5b36d898.xml"),
new XElement("Type", "System.String"),
new XElement("displayName", this.txtTitle.Text.ToString()),
new XElement("length", txtMaxLength.Text.ToString()),
new XElement("key", false),
new XElement("required", chkRequired.Checked)));
SourceEnum dpsource =
(SourceEnum)LoadControl(@"~\UserControls\SourceEnum.ascx");
dpsource.ID = "DropList" + " " + ctrlSource.SelectedValue.ToString() + " " + Guid.NewGuid().ToString();
dpsource.Text = txtTitle.Text;
pnlControls.Controls.Add(dpsource);
}
UpdateActiveControl("Test", "Form Name", "Test Control", listElements);
pnlControls.Controls.Add(new LiteralControl("<br />"));
// formControls = formGen.GetFormDataFromService();
foreach (XElement formControl in listElements)
{
FormStructure frmstructure = new FormStructure();
frmstructure.displayname = formControl.Element("displayName").Value;
frmstructure.Required = Convert.ToBoolean(formControl.Element("required").Value);
frmstructure.length = formControl.Element("length").Value;
frmstructure.ControlType = formControl.Element("Type").Value;
formControls.Add(frmstructure);
}
This is where I load the details of form controls
public partial class _default : System.Web.UI.Page
{
PortalContext portalContext = new PortalContext();
DataAccess da = new DataAccess();
FormGenerator formGen = new FormGenerator();
List<FormStructure> formControls = new List<FormStructure>();
List<XElement> listElements = new List<XElement>();
protected void Page_Load(object sender, EventArgs e)
{
da.SqlInstanceName = "CDDEVSVR-SQL";
da.PortalDatabaseName = "PortalCms";
da.IntegratedSecurity = true;
SetEntityContextConnectionStrings();
if (!IsPostBack)
{
formControls = formGen.GetFormDataFromService();
}
foreach (FormStructure formControl in formControls)
{
if (formControl.ControlType == "TextBox")
{
listElements.Add(new XElement(@"TextBox", new XElement("name"),
new XElement("Type", "System.String"),
new XElement("displayName", this.txtTitle.Text.ToString()),
new XElement("length", txtMaxLength.Text.ToString()),
new XElement("key", false),
new XElement("required", chkRequired.Checked)));
TextBoxUserControl textBoxControl =
(TextBoxUserControl)LoadControl(@"~\UserControls\TextBoxUserControl.ascx");
textBoxControl.XMLText = listElements;
textBoxControl.Text = formControl.displayname;
pnlControls.Controls.Add(textBoxControl);
pnlControls.Controls.Add(new LiteralControl("<br />"));
}
if (formControl.ControlType == "DropDown")
{
listElements.Add(new XElement(@"ClassficationEnum", new XElement("name", "TestForm"),
new XElement("Guid", "1f77f0ce-9e43-340f-1fd5-b11cc36c9cba"),
new XElement("Type", "System.String"),
new XElement("displayName", this.txtTitle.Text.ToString()),
new XElement("length", txtMaxLength.Text.ToString()),
new XElement("key", false),
new XElement("required", chkRequired.Checked)));
SourceEnum dpsource =
(SourceEnum)LoadControl(@"~\UserControls\SourceEnum.ascx");
dpsource.ID = "DropList" + " " + ctrlSource.SelectedValue.ToString() + " " + Guid.NewGuid().ToString();
dpsource.Text = formControl.displayname;
pnlControls.Controls.Add(dpsource);
}
}
}