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 »

Thursday, 14 December 2006

Dynamically Creating Links With JavaScript

Recently asked on Yahoo! Answers:

Javascript syntax question to get php passed?

submenuItem("Februa# ry-07",loc+"index.php?m=2&y=2007","","x2... 007_plain");

this is a line from my javascript for a drop down menu. I added two “# ” in the line just so yahoo wouldnt truncate it. The problem that i am having is that the menu works, but when i go to test it and hit a hyperlink from the submenu it chops everything to the right of the ? so i just get index.php . When i just remove the question mark, i get

index.phpm=2&y=2007

so i have determined that the question mark in the ahref is causing the problem. Im not too familiar with javascript syntax, is there something that i can do so i can get

index.php?m=2&y=2007

thanks

Additional Details

function submenuItem(text,url,tar,s){

if(NS4)return;
if(text.charAt(0)=='<')
d.write(text);
else if(text=="---")
d.write("<div class=\""+s+"\" style=\"width:"+menuw+"px\"><center><img src=\""+loc+"---.gif\" height=\"8\" width=\""+(menuw-6-(2*bd))+"\" border=0/></center></div>");
else{
d.write("<a ");
if(url!="")
d.write("href=\""+url+"\" ");
if(tar!="")
d.write("target=\""+tar+"\" ");
d.write("class=\""+s+"\" style=\"width:"+menuw+"px\"> "+text+" </...
}
}

this is the function

There’s no problem with using a question mark in JavaScript. Your syntax has some issues and the way you’re outputting your script is not paticularly sound, which is causing your problems. Also, you should convert your ampersands to entities.

To demonstrate, make an HTML page with the following DIV in the body:

Next, add this JavaScript function to the page:

function makeLink() {
	var mydiv = document.getElementById("test");
	var out = 'Dynamic Link';
	mydiv.innerHTML = out;
}

Change your body tag to this:


Run that test page. You’ll notice that there is now a link in the DIV, which links to test.php?m=2&y=2007.

Tuesday, 5 December 2006

JavaScript: Handling Multiple Selections In A Select Box

Recently asked on Yahoo! Answers:

javascript error plz help?

I have written the function in asp.net
function chkval(mval)
{
document.getElementById(“TextB… += mval;
}
<SELECT id=”ListBox1″ onclick=”chkval(value)” …….>
I want to assign the multiple value to TextBox1 from list box in following manner
INDIA + AMERICA + CHAINA
Plz help me

This is a bit confusing because the questioner is asking about JavaScript but mentions ASP.NET, and because his question was truncated by Yahoo! Answers. However, I gather from this that what he wants is the ability to display, in a textarea, multiple selections from a select.

The issue here is that in JavaScript, there is no native function to provide all the indexes of multiple selections. If you try to call the selectedIndex property on a select box with multiple choices checked off, only the index of the first selected item will be returned.

However, JavaScript will tell us whether an item is selected in a multiple select, if we iterate through all the items in the select. We’ll use that fact to do what the questioner wants.

Continue reading: JavaScript: Handling Multiple Selections In A Select Box »