ASP.NET GridView 新手关于 TFOOT 和 TH 的问题

2023-12-15

问候!

我仍在学习 GridView 控件,并且我有一个绑定到 ObjectDataSource 的控件。我的网络表单如下所示:

<asp:GridView ID="ourGrid" runat="server" DataSourceID="ourDataSource" onrowdatabound="ourGrid_RowDataBound"
              HeaderStyle-CssClass="header_style" AlternatingRowStyle-CssClass="altrow_style"
              ShowFooter="true">
    <columns>
    <asp:BoundField DataField="Name" HeaderText="Full Name" />
    <asp:BoundField DataField="Gender" HeaderText="Gender" />
    <asp:BoundField DataField="BirthYear" HeaderText="Year of Birth" />
    <asp:BoundField DataField="JoinDate" HeaderText="Date Joined" HtmlEncode="false" DataFormatString="{0:d}" />
  </columns>
</asp:GridView>
<asp:ObjectDataSource ID="ourDataSource" runat="server" SelectMethod="GetTopUsers" TypeName="Acme.Model.OurNewObject">
</asp:ObjectDataSource>

它当前生成以下标记:

<table cellpadding="0" cellspacing="0" summary="">
    <thead>
        <tr style="header_style">
            <th scope="col">Full Name</th>
            <th scope="col">Gender</th>
            <th scope="col">Year of Birth</th>
            <th scope="col">Date Joined</th>
        </tr>
    </thead>

    <tfoot>
        <tr>
            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
        </tr>
    </tfoot>

    <tbody>
        <tr>
            <td>John Smith</td>
            <td>Male</td>
            <td>1967</td>
            <td>17-6-2007</td>
        </tr>
        <tr class="AspNet-GridView-Alternate altrow_style">
            <td>Mary Kay</td>
            <td>Female</td>
            <td>1972</td>
            <td>15-11-2007</td>
        </tr>
        <tr>
            <td>Bill Jones</td>
            <td>Male</td>
            <td>1970</td>
            <td>23-2-2007</td>
        </tr>
    </tbody>
</table>

我还想添加一些 HTML 元素到此 GridView 控件将生成的表标记中。首先,我需要 TFOOT 如下所示:

<tfoot>
    <tr>
        <td colspan="4">
            <div>
                <a class="footerlink_style" title="Newest Members" href="#">Newest Members</a>
                <a class="footerlink_style" title="Top Posters" href="#">Top Posters</a>
            </div>
        </td>
    </tr>
</tfoot>

这些链接将不包含数据绑定信息,但可能是超链接控件。有没有办法可以在设计时指定这一点?

另外,对于 THEAD,是否可以为每个列标题指定单独的样式,就像在 GridView 中一样?

<thead>
    <tr style="header_style">
        <th scope="col" style="col1_style">Full Name</th>
        <th scope="col" style="col2_style">Gender</th>
        <th scope="col" style="col3_style">Year of Birth</th>
        <th scope="col" style="col4_style">Date Joined</th>
    </tr>
</thead>

最后,是否可以像这样指定表的汇总属性?

<table cellpadding="0" cellspacing="0" summary="Here is a list of users">

提前致谢。


TFOOT定制:

页脚将始终默认创建与网格视图其余部分相同数量的单元格。您可以通过添加以下内容在代码中覆盖它:

protected void OurGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.Footer)
    {
        int colSpan = e.Row.Cells.Count;

        for(int i = (e.Row.Cells.Count - 1); i >= 1; i -= 1)
        {
            e.Row.Cells.RemoveAt(i);
            e.Row.Cells[0].ColumnSpan = colSpan;
        }  

        HtmlAnchor link1 = new HtmlAnchor();
        link1.HRef = "#";
        link1.InnerText = "Newest Members";  

        HtmlAnchor link2 = new HtmlAnchor();  
        link2.HRef = "#";
        link2.InnerText = "Top Posters";  

        // Add a non-breaking space...remove the space between & and nbsp;
        // I just can't seem to get it to render in
        LiteralControl space = new LiteralControl("& nbsp;");

        Panel p = new Panel();
        p.Controls.Add(link1);
        p.Controls.Add(space);
        p.Controls.Add(link2);

        e.Row.Cells[0].Controls.Add(p);
    }
}

...并将 onrowcreated 属性添加到服务器控件:

<asp:GridView ID="ourGrid" onrowcreated="OurGrid_RowCreated" ...

西德风格:

您可以为每个绑定字段的“headerstyle-cssclass”中的每列指定标题 css 类。例如:

<asp:BoundField headerstyle-cssclass="col1_style1" DataField="Name" HeaderText="Full Name" />    
<asp:BoundField headerstyle-cssclass="col1_style2" DataField="Gender" HeaderText="Gender" />

表摘要:

只需将 Summary 属性添加到 gridview 即可:

<asp:GridView ID="ourGrid" summary="blah" ...

把它们放在一起:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = CreateDataSet();
        this.gv.DataSource = ds.Tables[0];

        this.gv.DataBind();
        this.gv.HeaderRow.TableSection = TableRowSection.TableHeader;
        this.gv.FooterRow.TableSection = TableRowSection.TableFooter;
    }

    protected void OurGrid_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            int colSpan = e.Row.Cells.Count;

            for (int i = (e.Row.Cells.Count - 1); i >= 1; i -= 1)
            {
                e.Row.Cells.RemoveAt(i);
                e.Row.Cells[0].ColumnSpan = colSpan;
            }

            HtmlAnchor link1 = new HtmlAnchor();
            link1.HRef = "#";
            link1.InnerText = "Newest Members";

            HtmlAnchor link2 = new HtmlAnchor();
            link2.HRef = "#";
            link2.InnerText = "Top Posters";

            LiteralControl l = new LiteralControl("&nbsp;");

            Panel p = new Panel();
            p.Controls.Add(link1);
            p.Controls.Add(l);
            p.Controls.Add(link2);

            e.Row.Cells[0].Controls.Add(p);

        }
    }

    private DataSet CreateDataSet()
    {
        DataTable table = new DataTable("tblLinks");
        DataColumn col;
        DataRow row;

        col = new DataColumn();
        col.DataType = Type.GetType("System.Int32");
        col.ColumnName = "ID";
        col.ReadOnly = true;
        col.Unique = true;
        table.Columns.Add(col);

        col = new DataColumn();
        col.DataType = Type.GetType("System.DateTime");
        col.ColumnName = "Date";
        col.ReadOnly = true;
        col.Unique = false;
        table.Columns.Add(col);

        col = new DataColumn();
        col.DataType = Type.GetType("System.String");
        col.ColumnName = "Url";
        col.ReadOnly = true;
        col.Unique = false;
        table.Columns.Add(col);

        DataColumn[] primaryKeysColumns = new DataColumn[1];
        primaryKeysColumns[0] = table.Columns["ID"];
        table.PrimaryKey = primaryKeysColumns;

        DataSet ds = new DataSet();
        ds.Tables.Add(table);

        row = table.NewRow();
        row["ID"] = 1;
        row["Date"] = new DateTime(2008, 11, 1);
        row["Url"] = "www.bbc.co.uk/newsitem1.html";
        table.Rows.Add(row);

        row = table.NewRow();
        row["ID"] = 2;
        row["Date"] = new DateTime(2008, 11, 1);
        row["Url"] = "www.bbc.co.uk/newsitem2.html";
        table.Rows.Add(row);

        return ds;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
    .red
    {
         color: red;
    }
    .olive
    {
        color:Olive;
    }
    .teal
    {
        color:Teal;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:gridview 
        id="gv" 
        autogeneratecolumns="false"
        showheader="true" 
        showfooter="true"
        summary="Here is the news!"
        caption="The Caption"
        captionalign="Top"
        alternatingrowstyle-cssclass="alt_row" 
        useaccessibleheader="true"
        onrowcreated="OurGrid_RowCreated" 
        runat="server">
        <columns>
            <asp:boundfield 
                headertext="ID" 
                headerstyle-cssclass="olive"  
                datafield="id" />
            <asp:hyperlinkfield 
                headertext="Link" 
                headerstyle-cssclass="red" 
                datanavigateurlfields="Url" 
                datanavigateurlformatstring="http://{0}" 
                datatextfield="Url" 
                datatextformatstring="http://{0}" />
            <asp:boundfield 
                headertext="Date"  
                headerstyle-cssclass="teal" 
                datafield="Date"/>
        </columns>
    </asp:gridview>
    </div>
    </form>
</body>
</html>

上面生成了以下 HTML:

<table cellspacing="0" 
        rules="all" 
        summary="Here is the news!" 
        border="1" 
        id="gv" 
        style="border-collapse:collapse;">

    <caption align="Top">
        The Caption
    </caption>
    <thead>
        <tr>
            <th class="olive" scope="col">ID</th>
            <th class="red" scope="col">Link</th>
            <th class="teal" scope="col">Date</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>
                <a href="http://www.bbc.co.uk/newsitem1.html">
                    http://www.bbc.co.uk/newsitem1.html
                </a>
            </td>
            <td>01/11/2008 00:00:00</td>
        </tr>
        <tr class="alt_row">
            <td>2</td>
            <td>
                <a href="http://www.bbc.co.uk/newsitem2.html">
                    http://www.bbc.co.uk/newsitem2.html
                </a>
            </td>
            <td>01/11/2008 00:00:00</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="3">
                <div>
                    <a href="#">Newest Members</a>&nbsp;<a href="#">Top Posters</a>
                </div>
            </td>
        </tr>
    </tfoot>
</table>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ASP.NET GridView 新手关于 TFOOT 和 TH 的问题 的相关文章

  • WPF ListView/Gridview 允许用户选择多个项目并将它们分组在一起

    我在 MVVM 应用程序中有一个 WPF ListView GridViwe GridView 绑定到 ViewModel 中的列表 要求是用户应该能够选择网格视图的多行 右键单击它并看到上下文菜单 将这些行组合在一起 选择后 所有这些项目
  • 如何从重定向的查询字符串中删除项目?

    在我的基页中 我需要从查询字符串中删除一个项目并重定向 我不能使用 Request QueryString Remove foo 因为该集合是只读的 有没有什么方法可以获取查询字符串 除了该一项 而无需迭代集合并重新构建它 您可以通过处理原
  • 将 .MDF SQL Server 数据库与 ASP.NET 结合使用与使用 SQL Server

    我目前正在 ASP NET MVC 中编写一个网站 我的数据库 其中还没有任何数据 只有正确的表 使用 SQL Server 2008 我已将其安装在我的开发计算机上 我使用服务器资源管理器从应用程序连接到数据库 然后使用 LINQ to
  • sitecore 站点内嵌套虚拟目录或应用程序是否可能

    我想将 ASP NET Web 应用程序嵌套在 sitecore 站点中 如下所示
  • 自定义WebApi授权数据库调用

    我正在尝试确定我编写的自定义授权属性是否确实是一个好主意 Scenario假设我们有一系列商店 每个商店Store有一个主人 只有商店的所有者才能对商店进行CRUD操作 除了具有Claim这基本上超越了所有权要求 并表示他们可以在任何商店上
  • 将 url 参数获取到 asp.net 标签中

    我试图自动将 asp label 的文本设置为 url 参数 但它似乎不起作用 这是我的代码
  • 对 HTTP 处理程序的同时请求不起作用

    我的 ASP Net 应用程序中有一个通用 HTTP 处理程序 ashx 它执行一些基本但耗时的计算 将进度语句打印到输出 以便让用户了解情况 执行这些计算涉及读取一些在使用处理程序时锁定的数据文件 因此对处理程序的两次调用不要立即开始处理
  • 获取 SignalR hub 内的完整 URL

    我正在使用 SignalR 开发一个用户跟踪解决方案 作为学习 SignalR 的有趣项目 用于 ASP NET MVC 应用程序 目前我可以跟踪登录的用户以及他们在特定页面上停留的时间 如果他们移动到另一个页面 我也会跟踪该页面 并且 S
  • 使用 AJAX 或多线程加速页面加载

    我的页面有 5 个部分 每个部分大约需要 1 秒来渲染 Page Load RenderSection1 1 sec RenderSection2 1 sec RenderSection3 1 sec RenderSection4 1 se
  • 导出到 CSV 时 Gridview 出现空行

    这个问题是由进一步讨论引发的这个问题 https stackoverflow com questions 6674555 export gridview data into csv file 6674589 noredirect 1 com
  • 从呈现的控件 ID 中删除 ctl00$ContentBody$

    我对现有的应用程序进行了一些更改 该应用程序以前只是简单的 HTML 和 Javascript 为了添加服务器端功能 我选择了 ASP NET 并利用了母版页概念 不幸的是 在一个巨大的 Web 表单上 控件 ID 全部被 ctl00 Co
  • 向特定客户端发送消息以及消息发送用户

    我是 SignalR 的初学者 我创建了一个基于 SignalR 的基本聊天应用程序 我面临的问题是我想向特定客户端以及发送消息的用户发送消息 这个怎么做 我知道要向特定客户端发送消息 我们可以这样做 Clients Client Cont
  • 如何使用 ASP.NET Web 表单从代码隐藏中访问更新面板内的文本框、标签

    我在更新面板中定义了一些控件 它们绑定到中继器控件 我需要根据匿名字段隐藏和显示用户名和国家 地区 但问题是我无法以编程方式访问更新面板中定义的控件 我如何访问这些控件 我也在网上查找但找不到很多参考资料 下面是来自aspx页面和 cs页面
  • 如何在aspx页面中的repeater ItemDataBound函数中传递Control.ClientID?

    我想调用 JavaScript 函数来折叠 展开 我在 asp repeater 中使用此代码ItemTemplate在跨度上 onclick javascript funCollExp this 我该如何通过Control ClientI
  • 在 ASP.Net 网站中使用 VBScript 中的变量

    我花了一天的大部分时间来研究这个问题 但找不到答案 我对 stackoverflow 比较陌生 询问多个问题是否有一定的礼仪 过去几天我问了三个问题 Anyways 这是代码隐藏文件中的代码 它执行脚本 systeminfo vbs 并且工
  • jQuery UI 对话框 + 验证

    我在单击 保存 后使用 Jquery Validate 验证 jQuery UI 对话框时遇到问题 这是我创建 Jquery 对话框的代码 它从目标 href URL 加载对话框 document ready dialogForms fun
  • ASP.NET预编译的优点是什么?

    使用 Aspnet compiler exe 代替通过 Visual Studio 进行的传统发布有多有用 那么资源 resx 文件又如何呢 与简单的 xcopy 相比 预编译有两个主要优点 文件系统不会包含所有代码 aspx文件和后面的所
  • VSTS/TFS设置环境变量ASP.NET core

    我正在尝试使用 VSTS 将 ASP NET Core 应用程序部署到 IIS 并执行以下任务 然而 经过多次谷歌搜索和浏览 MS 文档后 我找不到为部署设置环境变量的方法 我在环境范围的发布定义中设置的变量未设置为环境变量 知道如何实现这
  • 如何使用 ViewBag 创建 BaseController

    我需要执行以下操作 我已经准备好一些控制器并正在运行 但现在我想创建一个BaseController 我的每一个Controllers应该像这样继承它 public class MySecondController BaseControll
  • ASP.NET Click() 事件在第二次回发时不会触发

    我有一个 ASP NET Web 表单 我第一次提交表单时 会引发 提交按钮单击 事件 表单返回到浏览器时可能会出现验证错误 或者可以选择使用新值再次提交表单 当再次提交表单时 提交按钮单击 事件永远不会触发 Page Load 触发 但按

随机推荐