How to get data into second drop down list by selecting item in first drop down list

Posted by Srujana under ASP.NET on 4/12/2012 | Points: 10 | Views : 4164 | Status : [Member] | Replies : 7
Hi friends,
I have two drop down lists
in first drop down list I added two items fruits and vegtables
my requirement is If I selected fruits in first drop down list then
list of fruits(like apple,mango)will display in second list box
for this I wrote the following code but it not working
protected void Page_Load(object sender, EventArgs e)
{

}


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

switch(DropDownList1.SelectedItem.ToString())
{
case "Fruits":
DropDownList2.Items.Add("Apple");

DropDownList2.Items.Add("mango");
DropDownList2.Items.Add("Grapes");
break;

case "Vegtables":

DropDownList2.Items.Add("Alu");
DropDownList2.Items.Add("Brinjal");
DropDownList2.Items.Add("Carrot");
break;
case "Nuts":
DropDownList2.Items.Add("Badam");
DropDownList2.Items.Add("Kismis");
DropDownList2.Items.Add("Kaaju");
break;




}
}
}
if any one know the answer please tell me.




Responses

Posted by: Self-Innovator on: 4/12/2012 [Member] Bronze | Points: 25

Up
0
Down
HI chek this..

Design .aspx page
<table>

<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="Country"></asp:Label></td>
<td style="width: 211px">
<asp:DropDownList ID="DropDownList1" runat="server" Width="176px" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList><asp:RequiredFieldValidator
ID="RequiredFieldValidator4" runat="server" ErrorMessage="Select Country" ControlToValidate="DropDownList1" ValidationGroup="v1">*</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>
<asp:Label ID="Label6" runat="server" Text="State"></asp:Label></td>
<td style="width: 211px">
<asp:DropDownList ID="DropDownList2" runat="server" Width="176px">
</asp:DropDownList><asp:RequiredFieldValidator
ID="RequiredFieldValidator5" runat="server" ErrorMessage="Select State" ControlToValidate="DropDownList2" ValidationGroup="v1">*</asp:RequiredFieldValidator></td>
</tr>
</table>


.Cs

Page Load Event

 if (!IsPostBack)

{
FillDropDownByCountry();
}

Create UserDefined Fun

protected void FillDropDownByCountry()
{
SqlConnection cnn = new SqlConnection(Conn);
SqlDataAdapter ada = new SqlDataAdapter("sp_GetCountryList", cnn);
DataSet ds = new DataSet();
ada.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "CountryName";
DropDownList1.DataValueField = "CountryCode";
DropDownList1.DataBind();
}


ddlCountry Change Event

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{
string CountryCode = DropDownList1.SelectedItem.Value;
SqlConnection cnn = new SqlConnection(Conn);
SqlCommand cmd = new SqlCommand("sp_GetStates", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CountryCode", CountryCode);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ada.Fill(ds);
DropDownList2.DataSource = ds;
DropDownList2.DataTextField = "StateName";
DropDownList2.DataBind();
}


Join Hands Change lives
Thanks & Regards
Straight Edge Society

Srujana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Srujana on: 4/12/2012 [Member] Starter | Points: 25

Up
0
Down
presently I dont need database just A static app

Srujana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/12/2012 [Member] Bronze | Points: 25

Up
0
Down
Oh..Then try This
Design
 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>


Page Load
 if (!Page.IsPostBack)

{
DropDownList1.Items.Add("Fruits");
DropDownList1.Items.Add("Vegtables");
DropDownList1.Items.Add("Nuts");
}


IndexChange Event

 switch (DropDownList1.SelectedItem.ToString())

{
case "Fruits":
DropDownList2.Items.Clear();
DropDownList2.Items.Add("Apple");
DropDownList2.Items.Add("mango");
DropDownList2.Items.Add("Grapes");
break;

case "Vegtables":
DropDownList2.Items.Clear();
DropDownList2.Items.Add("Alu");
DropDownList2.Items.Add("Brinjal");
DropDownList2.Items.Add("Carrot");
break;
case "Nuts":
DropDownList2.Items.Clear();
DropDownList2.Items.Add("Badam");
DropDownList2.Items.Add("Kismis");
DropDownList2.Items.Add("Kaaju");
break;
}


Join Hands Change lives
Thanks & Regards
Straight Edge Society

Srujana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/12/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi rujana if helps mark as answer

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Srujana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/12/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi

http://www.bizsupportonline.net/browserforms/dynamically-fill-drop-down-list-box-browser-form-using-other-form-as-source.htm

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Srujana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kk86kiran on: 4/12/2012 [Member] Starter | Points: 25

Up
0
Down
hi Srujana,

Just do a simple modification in your code then you will the solution i.e, just set auto postback property true for 1st dropdownlist

k.m.j.kiran

Srujana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kk86kiran on: 4/12/2012 [Member] Starter | Points: 25

Up
0
Down
hi Srujana,

If you got solution plz mark it as answer

k.m.j.kiran

Srujana, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response