tags:

views:

276

answers:

4

I am using the below code inside of a table:

<tr>
    <td>User Language:</td>
    <td>
        <asp:DropDownList ID="Language" runat="server" Width="200px">
            <asp:ListItem Selected="True">English</asp:ListItem>
        </asp:DropDownList>
    </td>
</tr>
<tr>
    <td><span class="important">*</span>Company:</td>
    <td><asp:TextBox ID="Company" runat="server" width="200px" /></td>
</tr>

When the code appears on the site the <asp:Textbox> control is 205px and the <asp:DropDownList> is 200px wide. What is causing that? They both are set to a width of 200px.

+1  A: 

The DropDown is resized regarding the size of the options you provide in it. So, If you have longer options, you will get longer drop down. In addition it takes more place for the arrow that is the right corner.

anthares
+3  A: 

Because the textbox has 2px of border and 1px of padding on the sides.

Pent Ploompuu
This is the correct answer.
SLaks
A: 

When you insert data in the dropdownlist it resizes if you do not set the width property, but if you set the width it will not resize.

nmiranda
A: 

Not quite related, but here's a quick tip that will save you a lot of headache down the road:

Never use ASP:Textbox or ASP:DropDownList

<textbox runat=server> and <select runat=server> will work in every single case you could ever need, and they don't add any confusing properties such as "width" that don't quite work right.

Use real HTML tags with CSS classes for everything you do, and only add runat=server to the ones you actually intend to mess with from the backend.

Jason Kester
Is this good advice? There must be some reason these controls exist. Can you databind HTML textboxes and dropdownlists?
Daniel Coffman
Jason Kester
Yes, in answer to your question, you'll find that HtmlSelect has a .DataSource property and a .DataBind() method, as well as Text and Value members.
Jason Kester