views:

113

answers:

1

I am using an html <select> (not the server control) on my asp.net webform, which I bound using asp.net ajax via a webservice call. In my webservice I basically do this:

Private Function GetStores() As String
  dim stores as DataTable = GetStores()
  dim html as new StringBuilder
  for each row as DataRow in stores.Rows
    html.append("<option>")
    html.append(row("store"))
    html.append("</option>")
  next
  return html.tostring()
End Function

From my js, I would then simply use:

$get("myddl").innerHTML = "<select>" + result + "</select>";

The reason why I do this is because the server is faster in creating the required HTML. If I were to fill the ddl from the client-side by just returning the dataTable, then I think it will take a bit longer, depending on the rows.

Also please note that I do this only once when the page is loaded.

What do you think about this? Is this bad? If yes, why?

A: 

I think it is bad because I have seen many issues from various browsers arise when you just set the innerHTML of an element.

If you try to create elements by just putting the html markup for them into some controls innerHTML, the html DOM does not always get updated. This can cause your values to not get passed back on form submits, or even make it impossible to refer to the elements using javascript.

You should instead have you WebService return JSON or XML data with just the info you need, (just the store name.) And then use javascript to dynamically create and add the options to the dropdown.

Something like this would work well:

// do your AJAX call and pass back the responseText to this function (For a JSON response)
function FillDDL(text)
{
    eval("var data="+text);
    var ddl=document.getElementById('ddlID');

    for( var i=0; i<data.items.count; i++ )
    {
        var option = document.createElement("option");
        option.text=data.items[i];
        option.value=data.items[i]; //IE wont automatically copy the text to the value
        ddl.options.add(option,0); //FF will error if you dont tell it where to add the option
    }
}

And if you aren't familar with JSON, this is the format to use with the code above:

{items:['name','name2','name3']}

Just return a string like the above from your WebService and you should be all set.

TJMonk15
Thanks for the info. Now since I am doing this only on the PageLoad, should I maybe consider using an asp:dropdownlist control and binding it directly from the server?
vikasde
@TJMonk15: the blurb about "I have seen many issues..." is vague and FUDdy-sounding. Every approach has issues, including your own answer (that you nicely commented by the way). Some links backing up these issues you've seen would be welcomed I'm sure. Such as http://stackoverflow.com/questions/1066443/ie-innerhtml-error or http://stackoverflow.com/questions/155426/why-does-ie-give-unexpected-errors-when-setting-innerhtml
Crescent Fresh
For the record however the OPs specific example has **no** issues in any current browser.
Crescent Fresh
@vikasde: Since you only do it on PageLoad, a asp:dropdownlist is a good approach (must have missed that on my first read through :) )
TJMonk15
@crescentfresh: sorry for not linking examples, I did not have any examples handy :-\ and as for being vague, I appologize. He was just asking for an opinion, so I was just talking for personal experience. Though I have not tried exactly what he was trying to do.
TJMonk15