code_junkie

Drop Down List Issue

Nov 11th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. <script type="text/javascript">
  2. var MAX_WIDTH = 500; //the biggest width the select is allowed to be
  3.  
  4. function biggestOption(elem) {
  5. var biggest = 0;
  6. for (var i=0;i<elem.options.length;i++) {
  7. if ( elem.options[i].innerHTML.length > biggest ) {
  8. biggest = elem.options[i].innerHTML.length;
  9. }
  10. }
  11. return roughPixelSize(biggest);
  12. }
  13.  
  14. function roughPixelSize(charLength) {
  15. //this is far from perfect charLength to pixel
  16. //but it works for proof of concept
  17. roughSize = 30 + (charLength * 6);
  18. if (roughSize > MAX_WIDTH) {
  19. return MAX_WIDTH;
  20. } else {
  21. return roughSize;
  22. }
  23. }
  24.  
  25. function resizeToBiggest(elem) {
  26. elem.style.width = biggestOption(elem);
  27. }
  28.  
  29. function resizeToSelected(elem) {
  30. elem.style.width = roughPixelSize(elem.options[elem.selectedIndex].innerHTML.length);
  31. }
  32.  
  33. </script>
  34.  
  35. <select onmouseover="resizeToBiggest(this)" style="width:70px" onchange="resizeToSelected(this)" onblur="resizeToSelected(this)" >
  36. <option>test 1</option>
  37. <option>test 2</option>
  38. <option>test 3</option>
  39. <option>this is some really really realy long text</option>
  40. <option>test 4</option>
  41. <option>test 5</option>
  42. </select>
  43.  
  44. private void BindData()
  45. {
  46. List<Foo> list = new List<Foo>();
  47. list.Add(new Foo("Hello"));
  48. list.Add(new Foo("bye bye"));
  49. list.Add(new Foo("this is a reall asd a s das d asd as da sf gfa sda sd asdasd a"));
  50.  
  51. ddl1.DataSource = list;
  52. ddl1.DataTextField = "Text";
  53. ddl1.DataBind();
  54. }
Add Comment
Please, Sign In to add comment