CraigJSte
CraigJSte

Reputation: 932

Text Align DropDownList List Item CENTER using datarowview

I have tried all CSS imaginable in Table and paraent controls, and I am populating and formatting a DropDownList using DataRowView in a DataList Control in .NET 3.5 C#. But I can not for the life of me, CENTER the LIST ITEMS in the Drop Down List.. What would be easiest it seems is use the same formatting procedures I am now using in page behind... here is code.

Trust me Div tags do not work on the List Items.. nor does CSS.

How do I set text-align: Center ??

    if (on == "True")
    {
        DropDownList dl1 = (DropDownList)e.Item.FindControl("ddlOn");
        dl1.BackColor = System.Drawing.Color.LightGreen;
        dl1.ForeColor = System.Drawing.Color.White;
        dl1.Font.Bold = true;
        ListItem li = dl1.Items.FindByValue("True");
        if (li != null)
        {
            li.Selected = true;
        }
    }

Here is DropDownList in ItemTemplate of DataList Control.

                  <tr>
                    <td>
                        <asp:DropDownList ID="ddlOn" runat="server"
                                AutoPostBack="True" Width="100%" CssClass="cen3">
                            <asp:ListItem>True</asp:ListItem>
                            <asp:ListItem>False</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                  </tr>

Upvotes: 1

Views: 10996

Answers (1)

Kelsey
Kelsey

Reputation: 47726

I am assuming you are trying to center the DropDownList control to the center of the column?

If so you need to remove Width="100%" from the DropDownList and set the columns text-align to centered.

<td style="text-align: center">
    <asp:DropDownList ID="ddlOn" runat="server" AutoPostBack="True" CssClass="cen3">
        <asp:ListItem>True</asp:ListItem>
        <asp:ListItem>False</asp:ListItem>
    </asp:DropDownList>
</td>

EDIT: Based on what I have been able to test, IE (at least up to version 8) does not support setting the text alignment on the select or options tags. I have been able to get it work on Chrome and Firefox by setting it directly in the produced HTML's select element. The same HTML does nothing in IE.

I think you might be stuck without the ability to adjust the alignment in IE.

EDIT: Found this question on SO that comes to the same conclusion:

.net dropdownlist align text

Upvotes: 1

Related Questions