如何将CSS类添加到BoundField,以便我可以使用jQuery找到它?

时间:2010-08-12 12:42:35

标签: jquery asp.net css gridview

我想在GridView控件中为我的一些BoundFields添加一个类名;因此,一旦GridView被数据绑定和渲染,我就可以获得类似的东西:

<td class="Tag1">Some data came from data source</td>

这样做的目的是能够以这种方式找到所有“Tag1”的元素:

var allTag1td = $('td.Tag1');

那么,如何将此类添加到BoundField以便以这种方式呈现?

8 个答案:

答案 0 :(得分:40)

将ItemStyle属性添加到您的字段:

<asp:BoundField DataField="Count" HeaderText="Count">
    <ItemStyle CssClass="yourclass"></ItemStyle>
</asp:BoundField>

答案 1 :(得分:15)

你能否直接在aspx中设置boundfield的itemstyle属性?

(TableItemstyle有一个CssClass属性)

<asp:BoundField ItemStyle-CssClass="Tag1"/>

请参阅:

答案 2 :(得分:5)

您可以在创建行(CssClass事件)时将行的单元格Tag1属性设置为RowCreated

Page.aspx

<asp:GridView OnRowCreated="grid_RowCreated" AutoGenerateColumns="true" runat="server" ID="grid"></asp:GridView>

代码隐藏文件, Page.aspx.cs

protected void grid_RowCreated(object sender, GridViewRowEventArgs e) {
    foreach (TableCell cell in e.Row.Cells)
        cell.CssClass = "Tag1";
}

代码会将表格中每个class的{​​{1}}属性设置为td;呈现页面的标记看起来就像您正在寻找的标记:

Tag1

答案 3 :(得分:3)

您可以转换为TemplateField,然后使用Label并添加您想要的任何样式。

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

它对我有用。

答案 4 :(得分:1)

我的回答

<asp:BoundField DataField="id" HeaderText="" SortExpression="id"> <ItemStyle Width="10%" CssClass="hide"/> <headerstyle CssClass="hide"> </headerstyle> </asp:BoundField>

答案 5 :(得分:0)

我在RowCreated事件中做过类似的事情。我不得不根据他们的价值来塑造细胞。

http://msdn.microsoft.com/de-de/library/system.web.ui.webcontrols.gridview.rowcreated.aspx

答案 6 :(得分:0)

确保设置ItemStyle CssClass属性而不是其他属性。我犯了设置ControlStyle CssClass属性的错误,直到我读到这篇文章才意识到我的错误。

答案 7 :(得分:0)

为了在代码后面添加一个boundfield(这是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)

如果你想使CssClass数据依赖,你需要一个模板字段例如:

tf = New WebControls.TemplateField()
tf.HeaderText = "Whatever"
tf.SortExpression = "Whatever"
tf.ItemTemplate = New MyItemTemplate("DataField", "CssDataField")
AssessmentGrid.Columns.Add(tf)

MyItemTemplate在App_Code文件夹E.g中实现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

你可以直接将CssClass应用于标签,但原始问题是在单元格上有它