简体   繁体   English

如何居中对齐 TemplateField 的标题文本?

[英]How to center align the header text of a TemplateField?

I've a GridView with TemplateField s.我有一个带有TemplateFieldGridView

I have tried HeaderStyle-HorizontalAlign="Center" to align the header text of the TemplateField to center but it's not working.我试过HeaderStyle-HorizontalAlign="Center"将 TemplateField 的标题文本对齐到中心,但它不起作用。

<asp:TemplateField HeaderText="Events" HeaderStyle-HorizontalAlign="Center" 
    ItemStyle-HorizontalAlign="Center">
</asp:TemplateField>

How I can align center the header text of a TemplateField?如何将 TemplateField 的标题文本居中对齐?

While ItemStyle-HorizontalAlign="Center" aligning center the items of TemplateField is working correctly.ItemStyle-HorizontalAlign="Center"对齐中心时,TemplateField 的项目工作正常。

Any help will be really appreciated!任何帮助将不胜感激!

The <center> tag is deprecated in HTML 4.01 and not supported in HTML5 - the working code you posted could be "CSS-ified" as follows: <center>标记在 HTML 4.01 中已弃用,在 HTML5 中不受支持 - 您发布的工作代码可能是“CSS-ified”,如下所示:

<asp:TemplateField ItemStyle-HorizontalAlign="Center">
    <HeaderTemplate>
        <asp:Panel style="margin-left: auto; margin-right: auto; text-align: center;">
            Events
        <asp:Panel>
    </HeaderTemplate>
<asp:TemplateField>

(Note:Panel is the ASP.Net equivalent of a <div> .) (注意:Panel<div>的 ASP.Net 等价物。)

A slight improvement here is to define a CSS class for the style so it can be reused elsewhere:这里的一个小改进是为样式定义一个 CSS 类,以便它可以在其他地方重用:

.center {
  margin-left: auto;
  margin-right: auto;
  text-align: center;
}

...and reference it from the panel instead of using the inline style: ...并从面板中引用它而不是使用内联样式:

<asp:Panel CssClass="center">

I have just created a new WebForms solution and removed bootstrap just to be sure that no css styles interfere with my code.我刚刚创建了一个新的 WebForms 解决方案并删除了引导程序,以确保没有 css 样式干扰我的代码。 This is what I've done to reproduce your problem.这就是我为重现您的问题所做的工作。

aspx: ASP:

<asp:GridView runat="server" ID="grid" Style="width: 500px;">
    <Columns>
        <asp:TemplateField HeaderText="FirstName - TemplateField">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

</asp:GridView>

As you can see, I have defined one TemplateField without any additional css styles.如您所见,我定义了一个 TemplateField,没有任何额外的 css 样式。

CodeBehind:代码隐藏:

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        grid.DataSource = GetPersons();
        grid.DataBind();
    }

    public IEnumerable<Person> GetPersons()
    {
        for(int i = 0; i< 10; i++)
        {
            yield return new Person { FirstName = $"John{i}", LastName = "Doe", Age = i };
        }
    }
}

I am just returning 10 dummy items to create a demo grid.我只是返回 10 个虚拟项目来创建一个演示网格。 Nothing out of the ordinary.没有什么异常。

This is the result in Chrome and Internet Explorer:这是 Chrome 和 Internet Explorer 中的结果:

在此处输入图片说明

As you can see, the headers are centered by default.如您所见,标题默认居中。 And this is for both -BoundFields and TemaplateFields.这适用于 -BoundFields 和 TemaplateFields。

If this is not the case for you, I recommend checking if any other stylesheets are interfering with your styles.如果您不是这种情况,我建议您检查是否有任何其他样式表干扰了您的样式。 I know that bootstrap 3 defaults to text-align: center for th elements (Because I just checked)我知道 bootstrap 3 默认为text-align: center for th元素(因为我刚刚检查过)

By right, temStyle-HorizontalAlign="Center" should be working.没错, temStyle-Horizo​​ntalAlign="Center" 应该可以工作。 Just be aware that your gridview styles are inherited from parent stylesheet.请注意,您的 gridview 样式是从父样式表继承的。 Meaning, your gridview has at least one parent style which is not certer align.意思是,您的 gridview 至少有一个不对齐的父样式。 That should be where your problem comes.那应该是你的问题出现的地方。

I have found a way that solved the problem in which I used a <center> tag in HeaderTemplate:我找到了一种方法来解决我在 HeaderTemplate 中使用<center>标签的问题:

<asp:TemplateField ItemStyle-HorizontalAlign="Center">
    <HeaderTemplate>
        <center>
            Events
        </center>
    </HeaderTemplate>
<asp:TemplateField>

Try this to center the text in a column:试试这个将文本放在一列中:

.centeralign {
   text-align: center;
}

<ItemStyle CssClass="centeralign" />


<asp:TemplateColumn HeaderStyle-VerticalAlign="Top">

                              <HeaderTemplate>
                                 <asp:LinkButton runat="Server" ID="linkbuttonSortType" Text="Type" />
                                 <br>
                                 <asp:DropDownList runat="Server" ID="dropdownlistTypeFilter" AutoPostBack="true"
                                    Style="width: 100%;" OnSelectedIndexChanged="FilterChanged" />
                              </HeaderTemplate>
                              <ItemStyle CssClass="centeralign" />
                              <ItemTemplate>
                                 <asp:Literal runat="server" ID="litType" Text='<%# Eval("call_type").ToString() == "2" ? "Fax" : "Voice" %>' />
                              </ItemTemplate>
                           </asp:TemplateColumn>
<asp:TemplateField >
                            <HeaderTemplate>
                               <label style="text-align:center;display:block;"> Date</label>                                        
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label DataField="Created_Date" Style="font-size: medium; font-`enter code here`weight: bold;" ID="Created_Date_Id" runat="server" Text='<%# Bind("Created_Date") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM