How to add Items in Dropdown list programatically ??
Hi,
There is two dropdown list on page. One is for State and second one is for City related to that sate only.
I have addded the Name of state in first dropdownlist and would like to write programming in such a way so
that when I select the name of state the city related to that state adds automatically in second dropdownlist.
I tried many code but all are giving error.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="State_City_List.aspx.cs" Inherits="Test_Pages_State_City_List" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"
ontextchanged="DropDownList1_TextChanged" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedInde xChanged">
<asp:ListItem Selected="True"></asp:ListItem>
<asp:ListItem> Andhra Pradesh </asp:ListItem>
<asp:ListItem> Arunachal Pradesh </asp:ListItem>
<asp:ListItem> Assam </asp:ListItem>
<asp:ListItem> Bihar </asp:ListItem>
<asp:ListItem> Chhattisgarh </asp:ListItem>
<asp:ListItem> Goa </asp:ListItem>
<asp:ListItem> Gujarat </asp:ListItem>
<asp:ListItem> Haryana </asp:ListItem>
<asp:ListItem> Himachal Pradesh </asp:ListItem>
<asp:ListItem> Jammu and Kashmir </asp:ListItem>
<asp:ListItem> Jharkhand </asp:ListItem>
<asp:ListItem> Karnataka </asp:ListItem>
<asp:ListItem> Kerala </asp:ListItem>
<asp:ListItem> Madhya Pradesh </asp:ListItem>
<asp:ListItem> Maharashtra </asp:ListItem>
<asp:ListItem> Manipur </asp:ListItem>
<asp:ListItem> Meghalaya </asp:ListItem>
<asp:ListItem> Mizoram </asp:ListItem>
<asp:ListItem> Nagaland </asp:ListItem>
<asp:ListItem> Odisha </asp:ListItem>
<asp:ListItem> Punjab </asp:ListItem>
<asp:ListItem> Rajasthan </asp:ListItem>
<asp:ListItem> Sikkim </asp:ListItem>
<asp:ListItem> Tamil Nadu </asp:ListItem>
<asp:ListItem> Telangana </asp:ListItem>
<asp:ListItem> Tripura </asp:ListItem>
<asp:ListItem> Uttar Pradesh </asp:ListItem>
<asp:ListItem> Uttarakhand </asp:ListItem>
<asp:ListItem> West Bengal </asp:ListItem>
<asp:ListItem> Andaman -Nicobar </asp:ListItem>
<asp:ListItem> Chandigarh </asp:ListItem>
<asp:ListItem> Dadar Nagar Haveli </asp:ListItem>
<asp:ListItem> Daman and Diu </asp:ListItem>
<asp:ListItem> Delhi </asp:ListItem>
<asp:ListItem> Lakshadweep </asp:ListItem>
<asp:ListItem> Pondicherry </asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
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 Test_Pages_State_City_List : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void DropDownList1_TextChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue.ToString() == "Bihar")
{
DropDownList2.Items.Add("state");
}
}
}
Post back is enable for both Dropdown list
but these codes are not working please help me.
I do not want to use database for that.
Last edited by Abhishek Kumar; September 21st, 2014 at 10:22 AM..
Reason: Error In Posting
|