【问题标题】:How to add to each cell in a asp GridView a div wrapper?如何将 div 包装器添加到 asp GridView 中的每个单元格?
【发布时间】:2017-02-12 20:32:29
【问题描述】:

你能告诉我如何在每个 td 生成的每个单元格中添加一个 div 吗?

<asp:GridView ID="XDataGridView"
        CssClass="XDataGridView"
        ClientIDMode="Static"
        AllowPaging="True"
        AllowSorting="True"
        OnSorting="XDataGridView_Sorting"
        OnPageIndexChanging="XDataGridView_PageIndexChanging"
        Font-Size="Smaller"
        runat="server"
        EnablePersistedSelection="True"
        OnRowDataBound="XDataGridView_RowDataBound"
        OnRowCreated="XDataGridView_RowCreated"
        SelectedRowStyle-BackColor="#FF7900"
        meta:resourcekey="XDataGridViewResource1">
        <HeaderStyle CssClass="XDataGridViewHeader" BackColor="DimGray"
            BorderColor="#FF7900" HorizontalAlign="Center" VerticalAlign="Middle" Font-Size="10" Height="34" />
        <PagerSettings Visible="false" />
        <RowStyle BorderColor="#FF7900" Height="34" Font-Size="10" />
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <div id="insideDiv" runat="server"></div>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

我需要每次网格在 html 中生成它的表格时,每个 td 内容都将被包装在一个 div 元素中。乍一看应该很容易,虽然我不知道该怎么做。我试过使用 ItemTemplate 但似乎没有任何区别。

【问题讨论】:

    标签: c# html asp.net gridview


    【解决方案1】:

    您可以在ItemTemplate 中执行此操作。

    <ItemTemplate>
        <div><%# Eval("columnA") %></div>
    </ItemTemplate>
    

    div 元素不需要runat="server" 来工作。

    更新

    如果您使用自动生成的列,您可以使用 RowDataBound 事件添加&lt;div&gt;。

    protected void XDataGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //check if the row is a datarow
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //loop all the cells in the row
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                e.Row.Cells[i].Text = "<div>" + e.Row.Cells[i].Text + "</div>";
            }
        }
    }
    

    【讨论】:

    • 重点是 GridView 是动态使用的 - 所以我不知道将被绑定的列。我所做的是附加到 gridView_RowDataBound 事件,并在该事件中迭代 e.Row.Cells 并更改其 Text 属性。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 2017-12-15
    • 2013-05-27
    • 2016-11-07
    • 2017-04-04
    相关资源
    最近更新 更多