DumpsterDoggy

Articles

Search
Newest Articles
Recent Tutorials
Related Blogs
Get Microsoft Silverlight
Twitter Updates

Bookmark This

Missing Every Other Option in Drop-Down Fail

Chris Missal, August 25, 2008

So here is my "Missing Every Other Option in Drop-Down" fail. I was working on an old project at work (I consider working in .Net 1.1 legacy, Chris Sutton knows what I'm talking about) and I needed to change a year value drop down on an ASPX page (VB.Net). It was coded like this:

<option value="<%=DateTime.Now.Year%>"></option>
<option value="2007" <%=SelectedBikeYear("2007")%>>2007</option>
<option value="2006" <%=SelectedBikeYear("2006")%>>2006</option>
<option value="2005" <%=SelectedBikeYear("2005")%>>2005</option>
<option value="2004" <%=SelectedBikeYear("2004")%>>2004</option>
<option value="2003" <%=SelectedBikeYear("2003")%>>2003</option>
<option value="2002" <%=SelectedBikeYear("2002")%>>2002</option>
...

Which worked, and produced the output just fine. I wasn't missing any options. However, it wasn't scalable. It had to be updated each year to include the new year... I don't even have to tell you why this is bad, you already know. Instead of adding the year, I thought I'd take the time to improve the code. Here is what I changed it to:

<option value="<%=DateTime.Now.Year%>"></option>
<% For year As Integer = DateTime.Now.Year To 1901 Step -1 %>
<option value="<%=year%> <%=SelectedBikeYear(year)%>><%=year%></option>
<% Next %>

Bob Barker with a fail sign Can you spot my fail? Here is what the drop down looked like:



Because this was part of the output I saw when I viewed the source:

<option value="1988 >1988</option>
<option value="1987 >1987</option>
<option value="1986 >1986</option>
<option value="1985 >1985</option>
<option value="1984 >1984</option>
<option value="1983 >1983</option>
<option value="1982 >1982</option>
<option value="1981 >1981</option>
<option value="1980 >1980</option>
<option value="1979 >1979</option>
<option value="1978 >1978</option>
<option value="1977 >1977</option>
...

Yeah, I was just missing a quote (") after the option's value, this caused the browser to render it the best it could, which was not good.

Filed Under: .Net   HTML   VB