简体   繁体   中英

How to populate a ListBox with DropDownList Text using ASP.NET C#?

I have an Access database with four different headers: BUSINESS AND FINANCE, COMPUTERS, TECHNOLOGY, EDUCATION. I have connected it to a ListBox using an asp:AccessDataSource, and it is showing up perfectly.

I am now trying to set it that when i select an option in a DropDownList (either Business and Finance, Computers, Technology, Education) that the Listbox will dynamically populate with the corresponding data from the data source. I have no knowledge of how to do this, and i don't want to manually enter each value by hand because i need it to be easily editable.

I have no code to show as of now, because i have no idea how or where to start. Please help

For your DropDownList, you'll need to add the OnSelectedIndexChanged event and the AutoPostBack property. For example:

<asp:DropDownList ID="ddlHeaders" runat="server"
OnSelectedIndexChanged="ddlHeaders_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>

If you are using Visual Studio for your aspx and aspx.cs web page, Intellisense will prompt you to create a new event as soon as you type "=" after OnSelectedIndexChanged . This will automatically create the event in your C# code. The AutoPostBack will fire the event each time the value in the DropDownList is changed.

In the code for the event, you'll set the source of your ListBox where your data source's Header field equals ddlHeaders.SelectedValue .

OnSelectedIndexChanged is where you want to fire a call to the DataBase to get the data based on the selection in the DropDown, use that data to fill the list box.

If you are storing the data from your original call you could use a loop to grab all the data out of that source.

Honestly I would use a call to populate the dropdown and a separate call to populate the listbox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM