Hi guys,
I would like to seek for your advice on how should i go about removing dynamic control.
I have a method that does the removing currently, however, it doesn't remove when postback, and.. i wonder why, the code seems okay to me.
could anyone help on this? thanks
Regards,
Hakri
Here's my code
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Main.aspx.cs" Inherits="DynamicTools.Main" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddTextBox" />
<asp:Button ID="btnRemove" runat="server" Text="Remove" OnClick="RemoveTextBox" />
<br />
<asp:PlaceHolder ID="txtPh" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
Code Behind
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DynamicTools
{
public partial class Main : System.Web.UI.Page
{
int maxId = 41;
ArrayList listOfId = new ArrayList();
protected void Page_PreInit(object sender, EventArgs e)
{
var tools = new List<string> {
"TextBox",
"DropDownList",
"RadioButton",
"Button",
"Panel"
};
tools.Sort();
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
int i = 1;
foreach (string key in keys)
{
this.CreateLabel("lblDynamic" + i, maxId + i);
this.CreateTextBox("txtDynamic" + i);
this.CreateLabelTools("Tools : ");
this.CreateToolsDdl("ddlToolsDynamic" + i, tools);
this.CreateGroupIdLabel("Group ID Value : ");
this.CreateGroupIdTextBox("txtGroupIdDynamic" + i);
this.CreateParentQnIdLabel("Parent Qn Id : ");
this.CreateParentQnIdDdl("ddlParentQnIdDynamic" + i, CreateDDLItem(i));
i++;
}
}
protected void Page_Load(object sender, EventArgs e)
{
listOfId.Add(maxId).ToString();
}
protected void AddTextBox(object sender, EventArgs e)
{
var tools = new List<string> {
"TextBox",
"DropDownList",
"RadioButton",
"Button",
"Panel"
};
tools.Sort();
int index = (txtPh.Controls.OfType<TextBox>().ToList().Count / 2) + 1;
int id = maxId + index;
this.CreateLabel("lblDynamic" + index, id);
this.CreateTextBox("txtDynamic" + index);
this.CreateLabelTools("Tools : ");
this.CreateToolsDdl("ddlToolsDynamic" + index, tools);
this.CreateGroupIdLabel("Group ID Value : ");
this.CreateGroupIdTextBox("txtGroupIdDynamic" + index);
this.CreateParentQnIdLabel("Parent Qn Id : ");
this.CreateParentQnIdDdl("ddlParentQnIdDynamic" + index, CreateDDLItem(index));
listOfId.Add(maxId + index).ToString();
}
private Array CreateDDLItem(int index)
{
int it = maxId;
List<int> item = new List<int>();
for (int i = 0; i < index; i++)
{
item.Add(it);
it++;
}
return item.ToArray();
}
private void CreateTextBox(string id)
{
TextBox txt = new TextBox();
txt.ID = id;
txtPh.Controls.Add(txt);
}
private void CreateLabel(string id, int qnId)
{
string value = qnId.ToString();
Label lbl = new Label();
lbl.ID = id;
lbl.Text = value;
txtPh.Controls.Add(lbl);
}
private void CreateLabelTools(string text)
{
Label lbl = new Label();
lbl.Text = text;
txtPh.Controls.Add(lbl);
}
private void CreateToolsDdl(string id, List<string> data)
{
DropDownList ddl = new DropDownList();
ddl.ID = id;
ddl.DataSource = data;
ddl.DataBind();
txtPh.Controls.Add(ddl);
}
private void CreateGroupIdLabel(string text)
{
Label lbl = new Label();
lbl.Text = text;
txtPh.Controls.Add(lbl);
}
private void CreateGroupIdTextBox(string id)
{
TextBox txt = new TextBox();
txt.ID = id;
txtPh.Controls.Add(txt);
}
private void CreateParentQnIdLabel(string text)
{
Label lbl = new Label();
lbl.Text = text;
txtPh.Controls.Add(lbl);
}
private void CreateParentQnIdDdl(string id, Array data)
{
DropDownList ddl = new DropDownList();
ddl.ID = id;
ddl.DataSource = data;
ddl.DataBind();
txtPh.Controls.Add(ddl);
Literal lt = new Literal();
lt.Text = "<br />";
txtPh.Controls.Add(lt);
}
// Remove
protected void RemoveTextBox(object sender, EventArgs e)
{
int lastId = listOfId.Count - 1;
this.RemoveLabel("lblDynamic" + lastId);
this.RemoveTextBox("txtDynamic" + lastId);
this.RemoveLabelTools("Tools : ");
this.RemoveToolsDdl("ddlToolsDynamic" + lastId);
this.RemoveGroupIdLabel("Group ID Value : ");
this.RemoveGroupIdTextBox("txtGroupIdDynamic" + lastId);
this.RemoveParentQnIdLabel("Parent Qn Id : ");
this.RemoveParentQnIdDdl("ddlParentQnIdDynamic" + lastId);
listOfId.Remove((maxId + lastId - 1).ToString());
}
private void RemoveTextBox(string id)
{
TextBox txt = new TextBox();
txt.ID = id;
txtPh.Controls.Remove(txt);
}
private void RemoveLabel(string id)
{
Label lbl = new Label();
lbl.ID = id;
txtPh.Controls.Remove(lbl);
}
private void RemoveLabelTools(string text)
{
Label lbl = new Label();
lbl.Text = text;
txtPh.Controls.Remove(lbl);
}
private void RemoveToolsDdl(string id)
{
DropDownList ddl = new DropDownList();
txtPh.Controls.Remove(ddl);
}
private void RemoveGroupIdLabel(string text)
{
Label lbl = new Label();
lbl.Text = text;
txtPh.Controls.Remove(lbl);
}
private void RemoveGroupIdTextBox(string id)
{
TextBox txt = new TextBox();
txt.ID = id;
txtPh.Controls.Remove(txt);
}
private void RemoveParentQnIdLabel(string text)
{
Label lbl = new Label();
lbl.Text = text;
txtPh.Controls.Remove(lbl);
}
private void RemoveParentQnIdDdl(string id)
{
DropDownList ddl = new DropDownList();
ddl.ID = id;
txtPh.Controls.Remove(ddl);
}
}
}