I am facing problem with drop down list.I am controlling from from one more drop down list.I need to clear
all the items present in second drop down list and add new items my following code is not working .
int n = Convert.ToInt32(DropDownList1.SelectedIndex);
if (n == 0)
{
DropDownList2.Items.Clear();
DropDownList2.Items.Add("B.E");
DropDownList2.Items.Add("M.C.A");
DropDownList2.Items.Add("M.B.A");
}
if (n == 1)
{
DropDownList2.Items.Clear();
DropDownList2.Items.Add("Software engineer");
DropDownList2.Items.Add("Manager");
DropDownList2.Items.Add("Sales excetive");
DropDownList2.Items.Add("System operator");
DropDownList2.Items.Add("Graphics engineer");
}
WHY it is not working?
Suggest me the way.

Hemant KumarRank #1,202 · 1.5K reputationPosted Oct 4, 2011, 12:41 AM EDT
maheswar reddy27 reputationPosted Oct 4, 2011, 1:29 AM EDT
But It sis not working .... Still I am facing same error ...
Satyapriya NayakRank #47 · 31.5K reputationPosted Oct 4, 2011, 2:14 AM EDT
Try this
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add("0");
DropDownList1.Items.Add("1");
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Make sure that Autopostback property to True
int n = Convert.ToInt32(DropDownList1.SelectedIndex);
if (n == 0)
{
DropDownList2.Items.Clear();
DropDownList2.Items.Add("B.E");
DropDownList2.Items.Add("M.C.A");
DropDownList2.Items.Add("M.B.A");
}
if (n == 1)
{
DropDownList2.Items.Clear();
DropDownList2.Items.Add("Software engineer");
DropDownList2.Items.Add("Manager");
DropDownList2.Items.Add("Sales excetive");
DropDownList2.Items.Add("System operator");
DropDownList2.Items.Add("Graphics engineer");
}
}
}
Thanks
If this post helps you mark it as answer