views:

368

answers:

3

How can I increase the width of a DropDownList in ASP.NET?

A: 

I suggest you to use a dynamic width, like this:

<script type="text/javascript">
    function autoWidth()
    {
        var maxlength = 0;
        var mySelect = document.getElementById('Select1');
        for (var i=0; i<mySelect.options.length;i++)
        {
            if (mySelect[i].text.length > maxlength)
            {
                maxlength = mySelect[i].text.length;
            }
        }
        mySelect.style.width = maxlength * 10;
    }
</script>

And use it like this: onclick="autoWidth()"

Nathan Campos
select1 is the dropdown value is it?
Yes, of course it is.
Nathan Campos
Onclick event of dropdown? its says there is no onclick event for dropdown..or its a updatepanel onclick event?
It's just an example, I use it for other things.
Nathan Campos
+2  A: 

You can do this in a .CSS file, inline or with a STYLE tag. You can also do it in the code-behind by setting attributes(DropDownList1.Attribute.Add("style","etc...")).

CSS:

.ChangeWidth
{
    width:400px;    
}

Markup:

<asp:DropDownList ID="DropDownList1" CssClass="ChangeWidth" runat="server">
</asp:DropDownList>
rick schott
i did as you said; there is no change at alll
Make you sure you rebuild your application and whilst in the browser press Ctrl+F5 to load your current CSS file. Cache is ***** like that. :P
Serg
A: 
<asp:DropDownList ID="DropDownList1" width="50px" runat="server"> 
</asp:DropDownList> 
kralco626