10.02.2014 Views

Beginning Ajax With ASP.NET (2006).pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6<br />

Using a for loop to iterate over each element of the ids array, you create a new selection option using the<br />

following line:<br />

htmlCode = document.createElement(‘option’);<br />

This creates something like the following markup:<br />

<br />

This is then added to the list of options for the drop-down list, and the corresponding id and textual display<br />

values are then assigned to this newly added drop-down list option. Once all the options have been<br />

added, the drop-down list is enabled by setting the disabled flag to false:<br />

ddl.disabled = false;<br />

The drop-down list is now populated with values from your asynchronous server-side call and ready for<br />

selection. To illustrate that the drop-down list has been populated correctly, the drop-down list has had<br />

its onchanged event assigned a JavaScript method to display the selected value, or ID of the textual<br />

selection element. The following is the complete code listing for both web page and server-side code.<br />

Try It Out<br />

A More Complex Asynchronous Client Script Callback Example<br />

Web Page (AsyncDropDownListExample.aspx):<br />

<br />

<br />

<br />

<br />

Asynchronous Drop Down List Example<br />

<br />

function LoadListItems()<br />

{<br />

StartAsyncCall(null,null);<br />

}<br />

function OnServerCallComplete(arg, ctx)<br />

{<br />

var idsAndNames = arg.split(“~”);<br />

var ids = idsAndNames[0].split(“|”);<br />

var names = idsAndNames[1].split(“|”);<br />

var htmlCode;<br />

var ddl = document.getElementById(“ddlList”);<br />

for (var i=0; i < ids.length; i++)<br />

{<br />

htmlCode = document.createElement(‘option’);<br />

150

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!