I have 3 drop down boxes, created using the HTML select tag. On page load, the first box has a few names. Now, when I click on one of the names in the first box, some more names should appear in the second and when I click on a name in the second box, some more names should appear in the third box. How can I achieve this using AJAX? I have to use ASP.Net and MS SQL Server only. I'm a complete noob to AJAX and I've been educating myself about it, but nothing's working out. I've been looking for the code for close to a week. I looked up w3schools.com, but when I tried that code, it didn't work. Please help me out and please tell me step by step, the things required in order to make it work, and what goes where. I have a deadline that is fast approaching and am at my wit's end trying to make it work. HELP ME!!
Populating a drop down list using AJAX
Asked Answered
I would recommend placing the three dropdownlists in an UpdatePanel and adding a trigger to each for the updatepanel. Then, when the value changes, re-populate the dropdown and let the updatepanel push the update. Also keeps session-state in case you want to submit the values.
I don't have a compiler in front of me, but if need be just post a comment and I'll post the code tomorrow.
Working Example
I'm using the "Traditional template" that comes with visual studio and the master page, so excuse the content place holders. But this should demonstrate what I mean:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="MultiDropDown.aspx.cs" Inherits="AppSettingsRetrieval.MultiDropDown" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
String[] options = new[] { "ABC", "DEF", "GHI", "JKL", "MNO", "PQR", "STU", "VWX", "YZA" };
foreach (String option in options)
{
this.DropDownList1.Items.Add(new ListItem(option, option));
}
}
}
public void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.DropDownList2.Items.Clear();
this.DropDownList2.Items.Add(new ListItem("--- Please Select One ---"));
String[] options = new[] { "123", "456", "789", "101", "112", "131", "415", "161", "718" };
foreach (String option in options)
{
var str = String.Format("{0} {1}", this.DropDownList1.SelectedValue, option);
this.DropDownList2.Items.Add(new ListItem(str, str));
}
this.DropDownList2.Enabled = true;
this.DropDownList3.Enabled = false;
}
public void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
this.DropDownList3.Items.Clear();
this.DropDownList3.Items.Add(new ListItem("--- Please Select One ---"));
String[] options = new[] { "test", "testing", "tester", "foo", "bar", "foobar" };
foreach (String option in options)
{
var str = String.Format("{0} {1}", this.DropDownList2.SelectedValue, option);
this.DropDownList3.Items.Add(new ListItem(str, str));
}
this.DropDownList3.Enabled = true;
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<fieldset>
<legend>Consecutive Dropdown List</legend>
<p>
Primary Filter:
<asp:DropDownList runat="server" ID="DropDownList1" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="---Please Select One---" />
</asp:DropDownList>
</p>
<p>
Secondary Filter:
<asp:DropDownList runat="server" ID="DropDownList2" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true" Enabled="false">
<asp:ListItem Text="---Please Select One---" />
</asp:DropDownList>
</p>
<p>
Final Filter:
<asp:DropDownList runat="server" ID="DropDownList3" Enabled="false">
<asp:ListItem Text="---Please Select One---" />
</asp:DropDownList>
</p>
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Would the effect be the same as using AJAX? Can it be implemented easily? And yeah, please post the code ASAP. I'll be really grateful! Also, I need to perform some SQL queries based on the selection in the last box. So, can I do that using an UpdatePanel? –
Farrish
Also, where should the query to fetch the data be given? In the code behind or in the aspx page? Please include even those details. Thanks! –
Farrish
@Akshay: The UpdatePanel uses AJAX, so I guess that gives you what you're looking for, but allows .NET to do the leg-work for you. The data fetch is within the code-behind. (Drawing up a sample for you, should have it for you soon). –
Excursus
Hey Brad, thanks a lot for the code! I'm really sorry but I've neglected to mention that the data in the 2nd the 3rd drop downs would be populated from a database. How would the approach change then? Thanks and sorry for the trouble. –
Farrish
@Akshay: Are you using a datasource on the page by any chance? You can use the dropdown value as a filter parameter on the datasource, or you can grab and re-bind the values in the selectionchange event. –
Excursus
@Brad: This is how it should work. I have a table with 3 columns. On page load, the first dropdown shows the values of the first column. When I click on a particular value in the first dropdown, it should search the second column of the table for the values that relate to the value selected in the first dropdown. This should be repeated similarly with the second and third dropdowns as well. So, how can I do this using the update panel? Also, thanks a lot for all the help! :) –
Farrish
@Brad: I used the UpdatePanel and its working like a charm. All of what I posted in my previous comment is working. Here is a new requirement though. I bound the list boxes to a table in the database. Now, how do I change the queries if multiple selections in either of the list boxes are made? Right now, it looks like:
SELECT DISTINCT [x] FROM [y] WHERE ([z] = ?)
with the ? being the control, ListBox1. How do I change it to account for the multiple selections? Also, I have to provide a check box to select all the list items in the box. How can I add this query too? Would I have to remove... –
Farrish @Brad: the data binding from each of the list boxes to achieve what I want to? And, how do I take the value from the last list box and execute an SQL query based on that selection? Thanks a lot for the help and sorry for the trouble!! :( –
Farrish
@Akshay: Sorry, wasn't ignoring your last question, I've just been caught up. Did you end up finding a solution to the databinding, or are you still having trouble? Though I'd follow-up and see where you're at. ;-) –
Excursus
@Brad: Thanks for your concern! :) I can use your help around now. I was put on something else too, but now am back on this. With a clearer definition of what I'm supposed to do. Here's the scenario: Three list boxes, each with a check box that says "Select All". The list boxes are to support multiple selections. When I click on Select All, obviously, all the values are to be selected, and the second box needs to be populated accordingly, i.e., either a single, multiple or all selections. How can I do this now? –
Farrish
@Brad: Oh, and the last list box's value(s) need to have a query run on it/them. It should dynamically generate the query depending on the selection(s) made in the list box. Also, there is the check box thing there as well. Any help would be greatly appreciated. Will try doing stuff and make it work too! Thanks a lot! –
Farrish
@Akshay: That's a little more than a quick demo. But a little googling and I'm sure you can find somethign that either suites your needs or is 95% of the way there and you can modify. Just concentrate on the initial listbox first (and the multiple selected). Once you have that down, modifying the selection change event on it to "filter" the second control is pretty straight-forward. –
Excursus
© 2022 - 2024 — McMap. All rights reserved.