Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 21st, 2014, 09:02 AM
Authorized User
 
Join Date: Jun 2010
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old September 21st, 2014, 09:11 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

As I said in my previous message, please post the full code for the page as otherwise it's impossible to tell what's going on.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old February 16th, 2015, 03:11 PM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

implement the selected index changed event. if you want to add more than one data bind a result set to the ddl # 2.

Code:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
        var states = sender as DropDownList;
        var citySvc  = new CityService();


        DropDownList2.DataSource = citySvc.GetCities( states.SelectedText );
        DropDownList2.DataBind();
  
}

Last edited by mmorgan30; February 16th, 2015 at 03:27 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add Items in Dropdown list programatically ?? Abhishek Kumar BOOK: Beginning ASP.NET 4 : in C# and VB 1 September 21st, 2014 08:55 AM
Issues trying to add code programatically Rowin_aboat Excel VBA 0 November 16th, 2010 11:35 AM
adding Dropdown list Items from SQLSERVER 2005 database biswa Pro VB Databases 1 October 23rd, 2009 10:46 AM
How to add a site programatically millerthegorilla SharePoint Development 1 June 17th, 2008 11:43 AM
fill dropdown list with items when parent list isaac_cm Pro PHP 1 July 10th, 2006 05:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.