CSS 对齐 GridViewTemplate 字段 TextBox 中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9326182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Align text in a GridViewTemplate field TextBox
提问by Nalaka526
I need to align text to right in a TextBox
which is in a GridView
TemplateField
我需要将文本与 aTextBox
中的 a对齐GridView
TemplateField
This is TemplateField
HTML:
这是TemplateField
HTML:
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<div style="text-align: right;">
<asp:TextBox ID="txtDeductAmount" runat="server" Text="" BorderWidth="1px"></asp:TextBox>
</div>
</ItemTemplate>
<ItemStyle Width="80px" HorizontalAlign="Right" />
</asp:TemplateField>
This still aligned to left side. How to force text to align to right?
这仍然与左侧对齐。如何强制文本向右对齐?
NOTE :<ItemStyle Width="80px" HorizontalAlign="Right" />
aligns bound field text to right side properly.
注意:<ItemStyle Width="80px" HorizontalAlign="Right" />
将绑定字段文本正确对齐到右侧。
采纳答案by Aristos
This parameters do not change the input text box, but the table tags aligns.
此参数不会更改输入文本框,但会对齐表格标签。
To change how the input align the text add a class to it like.
要更改输入对齐文本的方式,请为其添加一个类。
.AlgRgh
{
text-align:right;
font-family:Verdana, Arial, Helvetica, sans-serif;
}
and use CssClass="AlgRgh"
on control
并CssClass="AlgRgh"
在控制上使用
<asp:TextBox ID="txtDeductAmount" runat="server" Text="" BorderWidth="1px" CssClass="AlgRgh" />
回答by Hannington Mambo
Or this:
或这个:
<asp:TemplateField HeaderText = "Bank" SortExpression="BankID">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
<ItemTemplate>
<asp:Label ID="BankIDLabel" runat="server" Text='<%# Bind("BankID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
回答by Carlos Ceballos
The Hannington Mambo answers work perfect, the faster one is:
汉宁顿曼波答案完美无缺,速度越快:
<asp:TemplateField HeaderText = "Bank" SortExpression="BankID">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
<ItemTemplate>
<asp:Label ID="BankIDLabel" runat="server" Text='<%# Bind("BankID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
回答by Jerr
When setting the values of the text box inside the GridView
you can use this code in the .cs
:
在 中设置文本框的值时,GridView
您可以在以下代码中使用此代码.cs
:
((TextBox)GridView1.Rows[Index of the grid].FindControl("txtName")).Style.Add("text-align", "right");
((TextBox)GridView1.Rows[Index of the grid].FindControl("txtName")).Style.Add("text-align", "right");