文本使用datarowview对齐DropDownList列表项CENTER

时间:2011-03-07 15:14:58

标签: .net drop-down-menu web-controls

我已经尝试了Table和paraent控件中可以想象的所有CSS,并且我在.NET 3.5 C#中的DataList控件中使用DataRowView填充和格式化DropDownList。但我不能为我的生活,在下拉列表中列出列表项目。看起来最简单的是使用我现在在页面后面使用的相同格式化程序...这里是代码。

相信我的Div标签不适用于List Items ..也不是CSS。

如何设置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;
        }
    }

这是DataList Control的ItemTemplate中的DropDownList。

                  <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>

1 个答案:

答案 0 :(得分:1)

我假设您正试图将DropDownList控件置于列的中心位置?

如果是,您需要从Width="100%"中移除DropDownList并将列text-align设置为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>

编辑:根据我能够测试的内容,IE(至少版本8)不支持在select或options标签上设置文本对齐方式。通过直接在生成的HTML的select元素中设置它,我已经能够在Chrome和Firefox上运行它。相同的HTML在IE中没有任何作用。

我认为如果没有能力调整IE中的对齐方式,你可能会陷入困境。

编辑:在SO上发现这个问题得出了同样的结论:

.net dropdownlist align text