Wednesday, November 10, 2010

Dynamic Combobox in asp.net

I provide you very simple example.This is just for your reference only. You can recreate that code based on your requirement. Look at the aspx code here I was used the asp:UpdatePanel  for reload the page without referesh.
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
ContentTemplate>
asp:UpdatePanel>
div>
form>

now look at the code behind in c#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DynamicCombo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList drplist2 = new DropDownList();
int count = 0;
ArrayList arraylist = new ArrayList();
drplist2 = new DropDownList();
drplist2.ID = "drplist2" + Convert.ToString(count);
drplist2.AutoPostBack = true;
drplist2.Attributes.Add("runat", "server");
drplist2.Items.Add("Fruits");
drplist2.Items.Add("Drinks");
UpdatePanel1.ContentTemplateContainer.Controls.Add(drplist2);
drplist2.SelectedIndexChanged += new EventHandler(drplist1_SelectedIndexChanged);
arraylist.Add(drplist2);
Session["arraylist"] = arraylist;
Session["count"] = count;
}
else
{
ArrayList arraylist = (ArrayList)Session["arraylist"];
for (int i = 0; i
{
DropDownList dd=(DropDownList) arraylist[i];
dd.ID = "drplist2" + Convert.ToString(i);
//dd.AutoPostBack = true;
//dd.Attributes.Add("runat", "server");
// dd.Items.Add("Mango");
//dd.Items.Add("Apple");
UpdatePanel1.ContentTemplateContainer.Controls.Add(dd);
//arraylist.Add(drplist2[i]);
dd.SelectedIndexChanged += new EventHandler(drplist1_SelectedIndexChanged);
}
}
}
protected void drplist1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList drplist2 = new DropDownList();
ArrayList arraylist = (ArrayList)Session["arraylist"];
int count = (int)Session["count"];
count++;
drplist2.ID = "drplist2" + Convert.ToString(count);
drplist2.AutoPostBack = true;
drplist2.Attributes.Add("runat", "server");
drplist2.Items.Add("Mango");
drplist2.Items.Add("Apple");
UpdatePanel1.ContentTemplateContainer.Controls.Add(drplist2);
drplist2.SelectedIndexChanged += new EventHandler(drplist1_SelectedIndexChanged);
arraylist.Add(drplist2);
Session["arraylist"] = arraylist;
Session["count"] = count;
}
}
}

Ask a Tech Support Specialist Online

We have partnered with JustAnswer so that you can get an answer ASAP.

JustAnswer

What? not this! then search here

Related post



0 comments:

Post a Comment

 

blogcatalog

Readers Time Pass Copyright © 2009 Shopping Bag is Designed by Ipietoon Sponsored by Online Business Journal