Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,



I have added 3 items to DropDownList (India,
New Zealand,
United States of America)

How to resize DropDownList dynamically based on selection..?

Please help me

thanks in advance
Posted
Updated 3-Feb-11 22:49pm
v5
Comments
Sunasara Imdadhusen 4-Feb-11 4:54am    
I think the browser will take care for the value inside dropdown and it will automatically adjust width of it's.

Simply, just don't provide the 'width' property of the dropdown.

It will automatically take care of it and will set the width as per the values in it.

UPDATE:
As Ankur pointed out, what I have suggested is for having a dropdown of width based on the maximum length value in it. But, sounds like the question is to adjust the width at runtime based on the 'selected' value in dropdown. If so, you would have to use Javascript as suggested by others. BUT, I would not recommend this behavior as having a varying sized control at runtime on selection is irritating and is not considered a good UI design.
 
Share this answer
 
v2
Comments
Ankur\m/ 4-Feb-11 5:26am    
That doesn't work. It will set the width to contain value with maximum length.
Sandeep Mewara 4-Feb-11 5:28am    
It will set the width to contain value with maximum length.
I said the same thing!

Don't give width at all and the dropdown will automatically adjust based on the value having max length in it.
Ankur\m/ 4-Feb-11 5:32am    
But the user doesn't want that. What he is asking is the width of the dropdownlist should adjust to the selected value. Suppose a value takes less width, then the width should decrease and if it takes a lot of width, the width of dropdownlist should increase.
Sandeep Mewara 4-Feb-11 5:29am    
To add on, this is in my project, right now in front of me... working like a charm! :)
Ankur\m/ 4-Feb-11 5:34am    
I think you got the question wrong. :)
You should try javascript or jquery.

Find the width of the selected item text in pixel and set it to dropdown's width.
 
Share this answer
 
Comments
Ankur\m/ 4-Feb-11 5:37am    
I thought of a similar solution except finding the width of the selected item in pixel. I assumed a fixed (maximum) length for each character.
Can we find the width of the selected item text in pixel in JavaScript?
Coding C# 4-Feb-11 5:45am    
Yes - you are correct, you need to assume the max width of a character depending on the font size you are using.
Ankur\m/ 4-Feb-11 5:56am    
Firstly, use reply link (mouse over my comment and you will see it) to reply me because I don't get notified otherwise.

So I was right! :)
Coding C# 4-Feb-11 6:06am    
Yes.
You got notified now? :P
Ankur\m/ 4-Feb-11 6:09am    
Yep! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900