如何将 CSS 类添加到 BoundField,以便我可以使用 jQuery 找到它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3467722/
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
How do I add a CSS class to a BoundField, so I can find it with jQuery?
提问by pencilCake
I want to add a class name to some of my BoundFields in the GridView control; so that once the GridView is data-bound and rendered I can obtain something like:
我想为 GridView 控件中的一些 BoundField 添加一个类名;这样一旦 GridView 被数据绑定并呈现,我就可以获得如下内容:
<td class="Tag1">Some data came from data source</td>
The purpose of doing such a thing is to be able to find all the elements that are "Tag1" in this way:
做这样的事情的目的是为了能够通过这种方式找到所有属于“Tag1”的元素:
var allTag1td = $('td.Tag1');
So, how can I add this class to the BoundField so that it is rendered in this way?
那么,如何将这个类添加到 BoundField 以便以这种方式呈现?
采纳答案by Tobiasopdenbrouw
Can you not directly set the itemstyle property of your boundfield in the aspx?
不能直接在aspx中设置你的boundfield的itemstyle属性吗?
(TableItemstyle has a CssClass property)
(TableItemstyle 有一个 CssClass 属性)
<asp:BoundField ItemStyle-CssClass="Tag1"/>
See:
看:
回答by Ralph Willgoss
Add the ItemStyle property to your field:
将 ItemStyle 属性添加到您的字段:
<asp:BoundField DataField="Count" HeaderText="Count">
<ItemStyle CssClass="yourclass"></ItemStyle>
</asp:BoundField>
回答by Giuseppe Accaputo
You can set a row's cell CssClass
property to Tag1
when the row's created (RowCreated
event).
您可以将行的单元格CssClass
属性设置Tag1
为该行的创建时间(RowCreated
事件)。
Page.aspx:
页面.aspx:
<asp:GridView OnRowCreated="grid_RowCreated" AutoGenerateColumns="true" runat="server" ID="grid"></asp:GridView>
Code-behind file, Page.aspx.cs:
代码隐藏文件Page.aspx.cs:
protected void grid_RowCreated(object sender, GridViewRowEventArgs e) {
foreach (TableCell cell in e.Row.Cells)
cell.CssClass = "Tag1";
}
The code will set the class
attribute of each td
in your table to Tag1
; the markup of the rendered page will look like the one you're looking for:
该代码会将表class
中每个的属性设置td
为Tag1
; 呈现页面的标记将与您要查找的标记类似:
<td class="Tag1"></td>
<td class="Tag1"></td>
...
回答by user3473248
You can convert to TemplateField then use a Label and Add any style you want.
您可以转换为 TemplateField,然后使用标签并添加您想要的任何样式。
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Field") %>' CssClass="YourStyle" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Field") %>' CssClass="YourStyle" />
</ItemTemplate>
</asp:TemplateField>
OR
或者
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Field") %>' Style="line-height: 1.4" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Field") %>' Style="line-height: 1.4" />
</ItemTemplate>
</asp:TemplateField>
It works for me.
这个对我有用。
回答by mabel
my answer
我的答案
<asp:BoundField DataField="id" HeaderText="" SortExpression="id">
<ItemStyle Width="10%" CssClass="hide"/>
<headerstyle CssClass="hide"> </headerstyle>
</asp:BoundField>
<asp:BoundField DataField="id" HeaderText="" SortExpression="id">
<ItemStyle Width="10%" CssClass="hide"/>
<headerstyle CssClass="hide"> </headerstyle>
</asp:BoundField>
回答by Gordon Smith
For adding a boundfield in code behind (this is VB, but similar for C#) try:
要在后面的代码中添加边界域(这是 VB,但与 C# 类似),请尝试:
bf = New BoundField()
bf.DataField = "FieldName"
bf.HeaderText = "Header"
bf.SortExpression = "FieldName(could be different)"
bf.ItemStyle.CssClass = "NoWrap"
MyGrid.Columns.Add(bf)
If you want to make CssClass data dependent you would need a templatefield Eg:
如果你想让 CssClass 数据依赖你需要一个模板字段 例如:
tf = New WebControls.TemplateField()
tf.HeaderText = "Whatever"
tf.SortExpression = "Whatever"
tf.ItemTemplate = New MyItemTemplate("DataField", "CssDataField")
AssessmentGrid.Columns.Add(tf)
MyItemTemplate implements ITemplate in the App_Code folder E.g:
MyItemTemplate 在 App_Code 文件夹中实现 ITemplate 例如:
Imports Microsoft.VisualBasic
Public Class MyItemTemplate
Implements System.Web.UI.ITemplate
'Normally Template type would be in here but we are only do Item
'(no edit, delete or header etc)
Dim DataField1 As String 'Displayed data
Dim DataField2 As String 'CssClass
Sub New(ByVal Field1 As String, ByVal Field2 As String)
DataField1 = Field1
DataField2 = Field2
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
Implements System.Web.UI.ITemplate.InstantiateIn
Dim ml As New Label()
ml.ID = DataField1
ml.Text = ""
ml.CssClass = ""
AddHandler ml.DataBinding, New EventHandler(AddressOf Item_DataBinding)
container.Controls.Add(l)
End Sub
Protected Sub Item_DataBinding(ByVal sender As Control, ByVal e As System.EventArgs)
Dim bound_value_object As Object
Dim data_item_container As IDataItemContainer = sender.NamingContainer
Dim Parent As TableCell = sender.Parent
Dim l As Label = sender
bound_value_object = DataBinder.Eval(data_item_container.DataItem, DataField1)
l.Text = bound_value_object.ToString
bound_value_object = DataBinder.Eval(data_item_container.DataItem, DataField2)
Parent.CssClass = bound_value_object.ToString
End Sub
End Class
You could apply CssClass to the label direct, but original question had it on the cell
您可以将 CssClass 直接应用于标签,但原始问题在单元格上有它
回答by Yves M.
I've done someting like this in the RowCreated_Event. I had to style the cells according to they values.
我在 RowCreated_Event 中做过这样的事情。我必须根据它们的值对单元格进行样式设置。
http://msdn.microsoft.com/de-de/library/system.web.ui.webcontrols.gridview.rowcreated.aspx
http://msdn.microsoft.com/de-de/library/system.web.ui.webcontrols.gridview.rowcreated.aspx
回答by Zarepheth
Make sure to set the ItemStyle
CssClass property rather than one of the others. I made the mistake of setting the ControlStyle
CssClass property and it wasn't until I read this post that I realized my mistake.
确保设置ItemStyle
CssClass 属性而不是其他属性之一。我犯了设置ControlStyle
CssClass 属性的错误,直到我阅读了这篇文章,我才意识到我的错误。