-1

I have an ASP.NET control, for example a DropDownList. I want to set the width of the control to auto. How do I achieve this? The Width property in ASP.NET control does not seem to accept auto, but it can only accept like percentage, pixels, and points.

I know I can make some workaround by specifying style="width:auto" as the control's attribute, and as this attribute is not a valid attribute for the control, it will not be parsed by asp.net and hence passed directly to browser. Is there a more proper way to do this?

rcs
  • 6,713
  • 12
  • 53
  • 75
  • 1
    When you say width, what's the difference between auto and 100%? – Christian Payne Nov 06 '13 at 07:26
  • It's different. I am putting the control inside a `` element. Setting the width with 100% will make the control occupy the whole `` width, whereas for auto it only takes up the necessary space (according to the content of the dropdownlist). – rcs Nov 06 '13 at 07:42
  • @rcs you can read my answer for auto width according to the content of the ddl. Good luck ! – asma Nov 06 '13 at 07:44

2 Answers2

2

A few alternatives:

  • Adding the style=width:auto; into the ASP.NET webcontrol, as mentioned in the question
  • Adding element.Attributes.Add('style', 'width:auto'); in the code behind. Generally it is the same as option number 1, just that it's added from code behind. But option 1 seems to be better because it does not mix presentation with code behind, and changing the presentation does not require recompiling of the program.
  • Remove the Width property for the ASP.NET control. If the Width property is not specified, it will be set as auto. But here we need to be careful if there is any CSS rule that may affect the behavior, for example we specify input in the CSS file.
rcs
  • 6,713
  • 12
  • 53
  • 75
0

These links might help you:

http://www.telerik.com/help/aspnet-ajax/combobox-auto-width.html

http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/dropdownautowidth/defaultcs.aspx

http://www.codeproject.com/Articles/5801/Adjust-combo-box-drop-down-list-width-to-longest-s

UPDATE

http://www.kendoui.com/forums/kendo-ui-complete-for-asp-net-mvc/dropdownlist/how-to-make-the-dropdownlist-width-auto-resize.aspx

http://www.webdeveloper.com/forum/showthread.php?48901-Setting-Drop-Down-List-Width-To-Auto

Please do not forget to comment that which of these links helped you. This will be beneficial for others.

asma
  • 2,795
  • 13
  • 60
  • 87
  • 3
    Please extract the relevant information from these links and put it into your answer. Otherwise a link to google.com would have done also. – M4N Nov 06 '13 at 07:47
  • @asma. The links mostly use 3rd-party component, whereas I'm using original .NET webcontrol. The third one is far more complicated than just putting `style="width:auto"`. – rcs Nov 06 '13 at 07:56