ASP.NET GridView 新手关于 TFOOT 和 TH 的问题 [英] ASP.NET GridView Newbie Questions About TFOOT and TH
本文介绍了ASP.NET GridView 新手关于 TFOOT 和 TH 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好!
我仍在学习 GridView 控件,并且我有一个绑定到 ObjectDataSource.我的 Web 表单如下所示:
<asp:GridView ID="ourGrid" runat="server" DataSourceID="ourDataSource" onrowdatabound="ourGrid_RowDataBound"HeaderStyle-CssClass="header_style" AlternatingRowStyle-CssClass="altrow_style"ShowFooter="true"><列><asp:BoundField DataField="名称" HeaderText="全名"/><asp:BoundField DataField="性别" HeaderText="性别"/><asp:BoundField DataField="BirthYear" HeaderText="出生年份"/><asp:BoundField DataField="JoinDate" HeaderText="加入日期" HtmlEncode="false" DataFormatString="{0:d}"/></列></asp:GridView><asp:ObjectDataSource ID="ourDataSource" runat="server" SelectMethod="GetTopUsers" TypeName="Acme.Model.OurNewObject"></asp:ObjectDataSource>
它目前生成以下标记:
<头><tr style="header_style"><th scope="col">全名</th><th scope="col">性别</th><th scope="col">出生年份</th><th scope="col">加入日期</th></tr></thead><脚><tr><td></td><td></td><td></td><td></td></tr></tfoot><tr><td>约翰·史密斯</td><td>男</td><td>1967</td><td>17-6-2007</td></tr><tr class="AspNet-GridView-Alternate altrow_style"><td>玫琳凯</td><td>女性</td><td>1972</td><td>15-11-2007</td></tr><tr><td>比尔·琼斯</td><td>男</td><td>1970</td><td>23-2-2007</td></tr></tbody>
还有一些 HTML 元素我想添加到此 GridView 控件将生成的表格标记中.首先,我需要 TFOOT 看起来像这样:
<tr><td colspan="4"><div><a class="footerlink_style" title="最新会员" href="#">最新会员</a><a class="footerlink_style" title="Top Posters" href="#">Top Posters</a></td></tr></tfoot>
链接将不包含数据绑定信息,但可能是超链接控件.有没有办法在设计时指定它?
另外,对于 THEAD,是否可以在 GridView 中为每个列标题指定单独的样式?
<tr style="header_style"><th scope="col" style="col1_style">全名</th><th scope="col" style="col2_style">性别</th><th scope="col" style="col3_style">出生年份</th><th scope="col" style="col4_style">加入日期</th></tr></thead>最后,是否可以像这样指定表的摘要属性?
提前致谢.
解决方案 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 = "最新成员";HtmlAnchor link2 = new HtmlAnchor();link2.HRef = "#";link2.InnerText = "热门海报";//添加一个不间断的空格...删除 & 之间的空格和 nbsp;//我似乎无法让它渲染LiteralControl space = new LiteralControl("& nbsp;");面板 p = 新面板();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" ...
THEAD 样式:
您可以为每个绑定字段的headerstyle-cssclass"中的每一列指定标题 css 类.例如:
<asp:BoundField headerstyle-cssclass="col1_style1" DataField="Name" HeaderText="全名"/><asp:BoundField headerstyle-cssclass="col1_style2" DataField="Gender" HeaderText="Gender"/>
表格摘要:
只需将摘要属性添加到 griview:
<asp:GridView ID="ourGrid" summary="blah" ...
综合起来:
<%@ Page Language="C#" %><%@导入命名空间="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){数据集 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(对象发送者,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 = "最新成员";HtmlAnchor link2 = new HtmlAnchor();link2.HRef = "#";link2.InnerText = "热门海报";LiteralControl l = new LiteralControl(" ");面板 p = 新面板();p.Controls.Add(link1);p.Controls.Add(l);p.Controls.Add(link2);e.Row.Cells[0].Controls.Add(p);}}私有数据集 CreateDataSet(){DataTable table = new DataTable("tblLinks");数据列列;数据行;col = 新数据列();col.DataType = Type.GetType("System.Int32");col.ColumnName = "ID";col.ReadOnly = true;col.Unique = true;table.Columns.Add(col);col = 新数据列();col.DataType = Type.GetType("System.DateTime");col.ColumnName = "日期";col.ReadOnly = true;col.Unique = false;table.Columns.Add(col);col = 新数据列();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;数据集 ds = 新数据集();ds.Tables.Add(table);row = table.NewRow();行[ID"] = 1;row["Date"] = new DateTime(2008, 11, 1);row["Url"] = "www.bbc.co.uk/newsitem1.html";table.Rows.Add(row);row = table.NewRow();行[ID"] = 2;row["Date"] = new DateTime(2008, 11, 1);row["Url"] = "www.bbc.co.uk/newsitem2.html";table.Rows.Add(row);返回 ds;}<html xmlns="http://www.w3.org/1999/xhtml"><head runat="服务器"><title></title><style type="text/css">.红色的{红色;}.橄榄{颜色:橄榄色;}.青色{颜色:青色;}</风格>头部><身体><form id="form1" runat="server"><div><asp:gridviewid="gv"自动生成列=假"显示头=真"显示脚注=真"摘要="这是新闻!标题=标题"标题对齐=顶部"alterrowstyle-cssclass="alt_row"useaccessibleheader="true"onrowcreated="OurGrid_RowCreated"runat="服务器"><列><asp:界域标题文本=ID"headerstyle-cssclass="橄榄"数据字段=id"/><asp:超链接字段标题文本=链接"headerstyle-cssclass="红色"datanavigateurlfields="网址"datanavigateurlformatstring="http://{0}"数据文本字段=网址"datatextformatstring="http://{0}"/><asp:界域标题文本=日期"headerstyle-cssclass="青色"数据字段=日期"/></列></asp:gridview></表单>