Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

Im building a web page in aspx and so far i can show some values from my SQL database in a listbox, but now i want to be able to select an item from that listbox and show a diferent item from the same database.

Best Regard
Thank You.
Posted

You mean to say you want some sort of cascade List boxfunctionality?
i.e. you have shown some DEPARTMENTs from your SQL server into a List Box
and now you want that based on what selection has been made in List Box

i.e. let's say "Accounts" department has been selected rom list box and you want to fetch data from sql database for that department.

Enable Autopostback property of the list box and make a database call similar to exisitng one in that Autopostback handler.

Try something like this :

ASP.NET
<asp:listbox id="ListBoxA" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ListBoxA_SelectedIndexChanged"></asp:listbox>


and codebehind :

C#
protected void ListBoxA_SelectedIndexChanged(object sender, EventArgs e)
{
     // fetch data from sql server now
}
 
Share this answer
 
v2
Comments
Member 8956437 23-May-12 10:42am    
Thank You, but i already had that part figured out, now the part to fetch data from sql server is a bit harder to me, maybe you can give me a hand.
Ashish Sehajpal 24-May-12 3:52am    
tell me how can i help you in that ?
Member 8956437 23-May-12 10:46am    
The autopostback property fixed my problem thank you very much.
able to select an item from that listbox and show a diferent item from the same database
So, what is the issue?

In any of your trigger event when server code is executed, find the selected item of listbox. If you want to use it to get other item from DB, get it. Show the new data the way you like.

You can use button control click event to postback your page or even use Listbox AutoPostback property to postback the page once you have selected item in our listbox. If you want to replace the selected item than you need to set the value with new one. If you just want to use it, then use SelectedValue/SelectedItem to get the current selected item and use it.
 
Share this answer
 
Comments
Member 8956437 23-May-12 10:46am    
Thank you, the autopostback worked.
Sandeep Mewara 23-May-12 14:13pm    
Great!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900