views:

284

answers:

2

Hi,

So, here's the scenario:

I have a .aspx page having two dropdowns, the data to be loaded in them is usually static(but may change everyday), the first dropdown always displays the main categories and the second dropdown is initially empty.

Now, the requirement is:

As the user selects a category from the first dropdownlist, the second dropdownlist should populate based on the selection just made.

The road-block:

This should be done without a postback.
My boss wants something like the drop downs on this page.

So what I gather is, I'd have to retrieve the data from the database on page load event, store it in the cache(or somewhere else?), then use javascript to handle the selectedindexchanged event of the 1st dropdown, use the data from database stored at pageload and populate the second dropdown according to the selected value of first dropdown.

It's been 2 days since I'm stuck on this and can't seem to find a feasible solution, and in need of great help.

Thanks in advance for throwing any light on the matter.

P.S.- I'm a beginner ASP.Net C# programmer and have no knowledge of Javascript.

+2  A: 

Microsoft released the Ajax Control Toolkit which has a Cascading dropdownlist control.

Edit: to boost performance on retrieving the data I suggest that you make use of the Cache object in ASP.NET and cache the data as close to the place as possible where you need it to give it a boost.

Another trick you can do is to send all the information as a json object when you render the page the first time so you don't need to use webservice calls but simply use javascript parsing to read out the json object and extract the information needed from it to fill up your second dropdownlist.

XIII
Darn, I forgot about that. +1 to you...
David Stratton
I did try that also. Well even that causes a webservice to run and takes about 1-2 seconds to populate the dropdown, My boss wants it to work like it was in windows application.
Anchit
@Anchit: next time mention that in your question that you already tried it. If your boss wants it to work like a windows application then suggest to make one. Web applications have pros but also cons compared with winforms. With the coming of ajax already quite a lot can be made more performing compared with the full postback paradigm.
XIII
Yes, sir I will be careful next time. It is actually a website, that displays information to the users and the dropdowns are basically filters to take them to the desired page(dynamic). The reason for the demand of such a functionality is because the nearest competing website has such a functionality though I guess that website is php based.
Anchit
The difference between php and asp.net is not really relevant I think. Both generate html to a browser. With both you can create great web applications. Also it doesn't mean that your competitor does something that you have to mimic them. Try to be original...
XIII
I agree sir but that is a very basic functionality, and I as a user wouldn't want to have to wait for something on one site which I can get on another without waiting at all. It's all about a better user experience.
Anchit
+1  A: 

The hard way: Truly do it client-side. There's a sample here to get you started.

The easy way.. Use an Ajax UpdatePanel and do it server-side, but the UpdatePanel will make it not post do a full post-back. (Tutorial here)

The UpdatePanel will be WAY easier and much quicker to code.

David Stratton
I guess I forgot to add, I did initially use an updatepanel, but it takes a lot of time to perform the async postback.The I used the Cascading DropDown from the AJAX Control Toolkit, but even that causes a postback. My boss wants not even a seconds' delay in populating the second dropdown.
Anchit
I'd say go with the "hard" way then... The only way to learn Javascript is to dig in, so take a look at the first link I posted. Sorry 'bout that...
David Stratton
I pretty much figured that out that I'd have to spend more time honing my Javascript skills, anyways thanks for the reply.One question: Can I recall a datatable stored in the cache on pageload, using javascript, and then using javascript refine that datatable and then populate the dropdown?
Anchit
No, I think you'll have to build the entire list of drop-down items at Page_Load and use javascript to show or hide them in the drop-down.
David Stratton
So sir, what should be my approach to build the list of dropdown items? I mean, what type of object must I store these values in so that they can be read by javascript. I guess I cant use ListItem[].
Anchit
Assuming you know how to get a DataReader and read from the database.... Do a While Reader.Read() loop, and use the Dropdownlist.Items.Add to add items to the list.
David Stratton
Or you can databind. This should help. http://www.asp101.com/articles/john/addtodropdown/default.asp
David Stratton
Sir, that is fine, but then how do I prevent the second dropdown from displaying the items added on pageload, as I only want to display the Items in the 2nd dropdown after the first has been changed. Furthermore,(pardon me for my lack of knowledge in Javascript), but can an asp.net control(aka dropdownlist) be accessed in javascript?
Anchit
Personally, I would use a hack. I would have a third, empty drop-down list with no items. I would set the second one to be invisible initially (possible put inside a DIV with the css style display = "none". Then, when the first drop-down is changed, toggle the visibility on the second list, making it visible, and on the third, making it invisible.
David Stratton
Did I mention that this was the hard way? I don't know of anyone that would do it this way, actually. Most bosses can be reasones with, and they have to understand that just to figure this out is probably going to take even a seasoned developer a lot of time... Perhaps the boss would be open to an alternative?
David Stratton
Actually, my boss just wants this functionality, it doesn't matter what approach I use to achieve it.Though I guess I am now on the right track to figuring it out, thanks to you. And I think I can build the module based on the lines of: http://www.velocityreviews.com/forums/t81504-javascript-access-dropdownlists-current-selectedtext-value.html
Anchit
Sir, now I finally have implemented the dropdownlists as the way both you and I initially thought we must do i.e. through UpdatePanel.Actually the site was 50% done when I was recruited to finish it, and the earlier developers had put loads of code in the pageload event that's why it was taking a heck of a lot of time to populate the second dropdown. I just cleared up the clout and presto, it worked! :)
Anchit
And yes, I've also used caching to make it faster.
Anchit
Sweet! Good to hear.
David Stratton
errr....oops...this question is again up for debate.. Sir, actually when i used caching, I didn't realize it'd be server side caching..so, while the module worked fine when I run it on localhost(as the server is local) but when I moved it online, it is again taking a lot of time...so I guess we're back to square one...no update panels cannot be used(even with caching) :|
Anchit