Friday, 24 April 2009

Using AJAX To Data Bind A Child Drop Down List Based On The Selected Option Of A Parent Select Control

Recently asked on Yahoo! Answers:

How to generate a submenu on the basis of value in a menu without refreshing the page?
I want to use if else construct to generate a submenu on the basis of value from a textfield in the same form. You can get it in the way that when someone selects a country in a field…the states menu of the form changes according to the country selected.

This question, rephrased: You have two HTML select lists; one contains parent values — for example, a list of states — and the other will contain child values — say, a list of cities in the selected state. Every time the value in the parent drop down list is changed, the values in the child drop down list are also changed. (I’ll be using the terms select list and drop down list interchangeably.)

This is a fairly common programming problem, and there are lots of examples out on the Web on how to solve it. So why write another one?

Primarily, because this question is so common. Most of the questioners on Yahoo! Answers are beginners, and as a result, a significant number don’t necessarily know how to phrase a Web search to find a tutorial. Far easier, then, to demonstrate the technique here and simply refer Yahoo! Answers users to this blog entry.

The executive summary: We’ll use AJAX to query a database seamlessly in the background every time the value of the parent select list is changed. More specifically, we’ll use the jQuery library to handle the AJAX requests, bind the child list and make sure the form can’t be processed until the request is complete.

Continue reading: Using AJAX To Data Bind A Child Drop Down List Based On The Selected Option Of A Parent Select Control »

Saturday, 21 February 2009

Moving ListItems From A ComboBox / DropDownList To A ListBox In .NET

Recently asked on Yahoo! Answers:

ComboBox Question in Visual Basic?
I am trying to create a combobox dropdown list of countries. When user click on a country, it should add to the listbox. I manage to do it. But I want to enhance some form of checking. If the country is already added in the listbox, the user should not be able to select the same country again. These are my codes, but it won’t work. I don’t know what’s wrong. Can someone please help? Thanks!

For Each country As String In lstCountry.Items
If country Is cbxCountries.SelectedItem Then
MsgBox(“County already selected.”, MsgBoxStyle.Information, )
Else
lstCountry.Items.Add(cbxCountries.Select…
End If
Next

Thanks to .NET’s totally object-oriented approach to programming — especially its modularity, encapsulation and polymorphism — moving the selected value of a ComboBox (in ASP.NET, a DropDownList) control to a ListBox control takes three lines of code. And removing that same item from the ComboBox takes just one more line.

Because I want to demo this, I’ll be writing this in ASP.NET. However, it will translate just fine to Windows Forms, as well; the questioner doesn’t state which he’s using, but my guess is it’s Windows Forms, since he’s using a ComboBox control.

Continue reading: Moving ListItems From A ComboBox / DropDownList To A ListBox In .NET »

Saturday, 7 February 2009

Revisited: Adding Non-Selectable ListItem Controls To An ASP.NET DataBound List Control

Last August I had quickly blogged on the subject of adding non-selectable ListItem controls to an ASP.NET DropDownList. At the time I didn’t have an easy way to demonstrate how that code worked — and, in all honesty, I hadn’t tested it thoroughly, so it had some problems.

But now, thanks to GoDaddy’s dirt-cheap ASP.NET Web hosting, I can run dougv.net, the ASP.NET demo site I had promised some time ago.

I’ve finally gotten around to configuring that new site and getting it to run the way I want, so at long last I can post this revisiting of the original post, which has actually been sitting around on my hard drive since last September.

Let’s first define some terms for less-experienced ASP.NET developers.

Control: An ASP.NET control is any Web page element — a text box, select list, image, div, whatever — that either allows the user to change its value (e.g., a text box or select list) or that had its contents / value / rendering changed on the server side (such as a GridView that is populated by a SqlDataSource).

So for most intents and purposes, an ASP.NET control is any Web page element that runs at the server.

Databound: A control is databound when its contents / options have been set with specific information. For example, a select list (DropDownList) is databound as soon as we add an option (ListItem) to it.

Some resources on the Web claim databinding only takes place if you use an external data store, such as an XML file or SQL Server database, to populate the child controls of a databound control (for example, to add ListItem controls to a DropDownList). That is incorrect: A control is technically databound if we set any of its properties. For example, a TextBox control is technically databound if we initially set its Text property.

OK, with those definitions out of the way, let’s visit the next obvious question: Why in the world would you want a non-selectable ListItem?

Continue reading: Revisited: Adding Non-Selectable ListItem Controls To An ASP.NET DataBound List Control »

Monday, 2 July 2007

A Simple YouTube Jukebox Using JavaScript / DOM And deconcept’s SWFObject

Recently asked on Yahoo! Answers:

HTML Help – Dropbox with Loading Option?

Okay, here’s my issue. I have a decent piece of coding that gives me a dropbox. The dropbox has a ‘go’ button, and it is filled with titles of music videos from YouTube.

Now, used alone, it’s fine. It opens a new page, which plays the youtube video.

However, I was clever enough to grab the *embed* names. So, it doesn’t load a simple youtube page, but simply the video in a website.

So now, I need some HTML help. I would like to create an object that sits next to the dropbox and plays the video when you select ‘go’, instead of it loading a new page.

I know how to make an embedded video. I know how to make the dropbox. Unfortunately, I don’t know how to make the dropbox and the embedded video interact, so that I can select the video I want to play.

This is very easy to do, thanks to deconcept’s excellent SWFObject JavaScript library and the questioner’s work in extracting the Flash FLV player URLs from all the videos he wants to show.

So, let’s get right on making a YouTube jukebox!

Continue reading: A Simple YouTube Jukebox Using JavaScript / DOM And deconcept’s SWFObject »

Monday, 15 January 2007

Dynamically Populating A Listbox From A Textbox Via JavaScript / DOM

Recently asked on Yahoo! Answers:

Where can I find the javascript that lets me move an item from a textbox to a list?

I have a text field, an ADD button, a list feld (empty) and a DELETE button. I want the user to be able to type something into the textfield, click ADD, and have that item appear in the list. If they select an item from the list and click DELETE, then the item would be deleted. Can someone post the code or a link to some free javascript?

You can actually find bits and pieces of this code out on the Interweb (as one of my clients calls it) quite easily, but packaged together isn’t so easy. So, I’ll tie it all together and explain how it works.

Continue reading: Dynamically Populating A Listbox From A Textbox Via JavaScript / DOM »