Just Dean
Mr Collins is my Dad

Populating a list box dynamically

December 21, 2007 03:40 by dean

I've just had to fix a colleagues code, which had the same problem that I'd been struggling with yesterday - it's another one of those annoying quirks that has you tearing your hair out because you look at your code and even though it compiles it still wont work.

The issue was that we are trying to populate a dropdown list dynamically - using the c# funtion

dropdownlist.items.add("Text","value");

Although this does compile it didn't seem to work - I stand to be corrected. The way I got round it was to create a new listitem object and populate it with my text and value. i.e

dropdownlist.items.add(new ListItem("Text","value"));

Hope this helps somebody cus it's such an annoying thing to get stuck with - another point was that the value must always be a string - you can cast it when you retrieve it - (Convert.ToInt32(dropdownlist.selectedItem.value) but it seems to only want strings (I guess this is because the value is going to be rendered into HTML so any cast information will be obliterated anyway. A Little like the classic ASP Response.Write("Some HTML") used to work.

Blogged with Flock


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Technical
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag




Live preview

January 6. 2009 23:13

Gravatar