using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class drp: System.Web.UI.Page
{
int flag = 1, lastid;
SqlConnection con = new SqlConnection("Data Source=INSPIRE-1;Initial Catalog=new;Integrated Security=True");
public static int parent = 0;
int[] a = new int[50];
Panel pnlDropDownList;
protected void Page_PreInit(object sender, EventArgs e)
{
//Create a Dynamic Panel
pnlDropDownList = new Panel();
pnlDropDownList.ID = "pnlDropDownList";
pnlDropDownList.BorderWidth = 1;
pnlDropDownList.Width = 300;
this.form1.Controls.Add(pnlDropDownList);
}
protected void Page_Load(object sender, EventArgs e)
{
//string[] arr1 = new string[10];
DropDownList ddl = new DropDownList();
ddl.ID = ID;//ID :_Page
// ddl.Items.Clear();
//Session["myarray"] = arr1;
// Session["ddl"]= Session["myarray"][0];
//if (!IsPostBack)
//{
// ddl.AppendDataBoundItems = true;
// base.Page_Load(e);
//if (!IsPostBack)
//{
// RecreateControls("ddlDynamic", "DropDownList");
//}
int cnt = FindOccurence("ddlDynamic");
if (cnt == 0)
{
a[cnt + 1] = 1; a[1] = 0;
Session["parents"] = a;
CreateDropDownList("ddlDynamic-" + Convert.ToString(cnt + 1), a[cnt + 1]);//CreateDropDownList("ddlDynamic-1",0);a[1]=0;
}
else
{
RecreateControls("ddlDynamic", "DropDownList");
}
if (flag == 1)
{
flag = 0;
}
}
private int FindOccurence(string substr)
{
//return 2;
string reqstr = Request.Form.ToString();
return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
}
private void RecreateControls(string ctrlPrefix, string ctrlType)
{
DropDownList ddl = new DropDownList();
ddl.ID = ID;//ID :_Page
//ddl.ID = "ddlDynamic-1";
int value = 1;
string lastfocus = Request.Form.Get("__EVENTTARGET");
// lastid = Convert.ToInt32(lastfocus.Split('-')[1]);
string[] ctrls = Request.Form.ToString().Split('&');
int cnt = FindOccurence(ctrlPrefix);
a = (int[])Session["parents"];//a awalays 0
if (cnt > 0)
{
for (int k = 1; k <= cnt; k++)
{
for (int i = 0; i < ctrls.Length; i++)
{
if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) && !ctrls[i].Contains("EVENTTARGET"))
{
string ctrlID = ctrls[i].Split('=')[0];
int b = Convert.ToInt32(ctrlID.Split('-')[1]);
// int c = Convert.ToInt32(Session["ddl"]);
//if (lastid>b)
//{
// break;
//}
//else
//{
if (ctrlType == "DropDownList")
{
CreateDropDownList(ctrlID, a[value]);
value++;
//}
}
break;
}
}
}
}
else{
}
}
private void CreateDropDownList(string ID, int parent)//ID:ddlDynamic-1 parent:0
{
con.Open();
SqlDataAdapter abc;
if (parent == 0)
{
abc = new SqlDataAdapter("select catid,name from catgry where parent =" + parent, con);//
}
else
{
abc = new SqlDataAdapter("select catid,name from catgry where parent=" + parent, con);
}
DropDownList ddl = new DropDownList();
ddl.ID = ID;
DataTable b = new DataTable();
abc.Fill(b);
if (b.Rows.Count != 0)
{
ddl.DataSource = b;
ddl.DataTextField = "name";
ddl.DataValueField = "catid";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("Select Category", "0"));
con.Close();
ddl.AutoPostBack = true;
ddl.SelectedIndexChanged += new EventHandler(ddl_OnSelectedIndexChanged);
pnlDropDownList.Controls.Add(ddl);
//pnlDropDownList.Controls.Add(Remove);
//count++;
Literal lt = new Literal();
lt.Text = "<br />";
pnlDropDownList.Controls.Add(lt);
}
}
//for (int i = 0; i < b.Rows.Count; i++)
//{
// pnlDropDownList.Controls.Remove(pnlDropDownList.FindControl(b.Rows[i]["name"].ToString()));
//}
//RecreateControls("ddlDynamic", "C");
protected void Remove(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
string ID = ddl.ID;
foreach (Control DropDownList in PlaceHolder1.Controls)
{
//Here you need to take ID from ViewState["controlIdList"]
if (ddl.ID == "ddlDynamic")
{
Controls.Remove(DropDownList);
}
}
}
private void ddl_GotFocus(Object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
string ID = ddl.ID;
Session["ddl"] = ddl.ID.Split('-')[1];
}
protected void ddl_OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
string ID = ddl.ID;
// int[,] array6 = new int[10, 10];
// string[] arr1 = new string[10];
// Session["myarray"] = arr1;
// Remove(sender, e);
//Session["ddl"] = ddl.ID;
Session["ddl"] = ddl.ID.Split('-')[1];
int cnt = FindOccurence("ddlDynamic");
//int[] a = (int[])Session["parents"];
a = (int[])Session["parents"];
a[cnt] = int.Parse(ddl.SelectedValue);
Session["parents"] = a;
CreateDropDownList("ddlDynamic-" + Convert.ToString(cnt), a[cnt]);
//ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "<script type = 'text/javascript'>alert('" + ID + " fired SelectedIndexChanged event');</script>");
//parent = Convert.ToInt32(ddl.SelectedValue);
}
}
how to delete or remove a dropdown list,if the selected value in previous dropdown changed in dynamically created dropdown list