var productList;
var divId;
var divName;
var divPrice;
function pageLoad(sender, e)
{
WebService.GetProductId(onGetProductSuccess, onGetProductFailed);
}
function onGetProductSuccess(result)
{
productList = $get('Select1');
createSelectItem(productList, "[ALL]", "-1");
for(var index = 0; index < result.length; index++)
createSelectItem(productList, result[index].Name, result[index].ID);
$addHandler(productList, 'change', onProductChange);
}
function onGetProductFailed(arg) {
alert(arg);
}
function createSelectItem(listBox, text, value) {
var opt = document.createElement('option');
setText(opt, text);
opt.value = value;
listBox.appendChild(opt);
}
function onProductChange() {
if (productList.options[productList.selectedIndex].value > -1) {
WebService.GetProduct(parseInt(productList.options[productList.selectedIndex].value), onLoadSuccess);
}
else {
WebService.GetProductId(onLoadSuccess);
}
}
function onLoadSuccess(products)
{
var dataList = $find('DataList1');
dataList.set_dataSource(products);
dataList.dataBind();
}
function onItemDataBound(sender, e)
{
var item = e.get_item();
if (item.get_isDataItemType())
{
var product = item.get_dataItem();
divId = item.findControl('ProductId');
divName = item.findControl('ProductName');
divPrice = item.findControl('ProductPrice');
setText(divId, product.ID);
setText(divName, product.Name);
setText(divPrice, addFigure(product.Price));
}
}
// 参照:GAC なぜなにGAC -> JavaScript
// http://www.gac.jp/article/index.php?stats=question&category=9&id=4207&command=msg
function addFigure(str) {
var num = new String(str).replace(/,/g, "");
while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
return num;
}
function setText(element, text)
{
if (typeof element.textContent != 'undefined')
{
element.textContent = text;
}
else if (typeof element.innerText != 'undefined')
{
element.innerText = text;
}
}