.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Bind dropdown list using Ajax

Posted By:varun bansal       Posted Date: July 27, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

Bind dropdown list using Ajax. Using Ajax one can postback only a part of web page which he want to need... so it is very useful.
for using ajax your web application always ajax enabled website..
then create a new webpage. here is drag and drop one scriptmanager, two dropdownlist (ddl_1 and ddl_2) and one updatepanel from the Ajax extension tools..
then ddl2 cut and paste into updatepanel1. (for exp. ddl_1 is for country and ddl2 is for city. when u select country form ddl1 then all city related to selected country bind in ddl_2..)

then set the autopostback property true of ddl_1 and select the event ddl_1_SelectedIndexChanged from the event properties...

after this, click on the trigger properties of updatepanel1 (open a dialog box) and click on add button (one asynpostback trigger add).
Asynpostback have two properties, 1. controlID 2. EventName.
Set ddl_1 in ContrlID and SelectedIndexChanged in Event name and press ok..now trigger is set.
Trigger is use for control your updatepanel. u can set more than one trigger on one updatepanel if required...
now your design page is ready...

now come the code behind page and
bind the ddl_1 on page load event (all country name)
and write code for bind city related to selected country in ddl_1_SelectedIndexChanged event...
now u can run your web page. u can see that all country already bind in ddl_1. when select any country. then updatepanel is postback and ddl_2 is bind for all city related to selected country..

code is below, it may be help u...

on ajax.aspx====



code behind page (ajax.aspx.cs)...

string connectionstring= "Data Source=.; Integrated Security=true; Database=realasn1;";
sqlconnection con;
SqlDataAdapter da;
Dataset ds = new Dataset();
protected void Page_Load(object sender, EventArgs e)
con = new (connectionstring);
da = new SqlDataAdapter("select id,name from country", con);
if (!IsPostBack)

public void bindcountry(Dataset dbset)
//bind office country
ddl_1.DataSource = dbset;
ddl_1.DataTextField = dbset.Tables[0].Columns[1].ToString();
ddl_1.DataValueField = dbset.Tables[0].Columns[0].ToString();
ListItem li = new ListItem();
li.Text = "--Select Country--";
li.Value = "0";
ddl_1.Items.Insert(0, li);

protected void ddl_1_SelectedIndexChanged(object sender, EventArgs e)
System.Threading.Thread.Sleep(500);//for update panel
if (int.Parse(ddl_1.SelectedValue) > 0)
con = new (connectionstring);
da = new SqlDataAdapter("select id,name from city where countryid=" + int.Parse(ddl_1.SelectedValue) , con);

public void bindcity(DataSet dbset)
ddl_2.DataSource = dbset;
ddl_2.DataTextField = dbset.Tables[0].Columns[1].ToString();
ddl_2.DataValueField = dbset.Tables[0].Columns[0].ToString();
ListItem li = new ListItem();
li.Text = "--Select City--";
li.Value = "0";
ddl_2.Items.Insert(0, li);

Varun Bansal
 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend