ASP.NET2005 设置GridView表头的背景图片

来源:互联网 发布:ubuntu apt安装jdk1.8 编辑:程序博客网 时间:2024/05/20 06:26

最近在做一个网站,显示数据时为了方便使用了GridView。虽然GridView显示数据的功能很强也很方便,但它的样式却并不美观。为了使GridView的显示样式美观一些,经常需要设置表头的背景,我总结出的方法如下:
方法一:
在GridView的OnRowDataBound事件中设置背景,代码如下:

 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Attributes.Add("style", "background-image:url('images/title.gif')");
}
}

 


方法二:
和上面的方法一样,只是代码不一样:

 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Style.Add("background-image", "images/title.gif");
}
}

 

方法三:
使用CSS,设置GridView每一列的HeaderStyle的CssClass习性,代码如下:

 

<style type="text/css">
.headbackground
{
background-image:url(images/title.gif);
}
</style>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="id" HeaderText="编号">
<HeaderStyle CssClass="headbackground" />
</asp:BoundField>
<asp:HyperLinkField DataTextField="title" HeaderText="标题">
<HeaderStyle CssClass="headbackground" />
</asp:HyperLinkField>
</Columns>
</asp:GridView>