1. 程式人生 > >設定GridView表頭的背景圖片 設定GridView表頭的背景圖片

設定GridView表頭的背景圖片 設定GridView表頭的背景圖片

設定GridView表頭的背景圖片

  方法一: protected void Page_Load(object sender, EventArgs e)
    {
       //UserGrid.Attributes.Add("bordercolor", "#a7b8d9");
        UserGrid. HeaderRow.Attributes.Add("style", "background-image:url('images/bj1.png')");    }    

最近在做一個網站,顯示資料時為了方便使用了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>
方法一: protected void Page_Load(object sender, EventArgs e)
    {
       //UserGrid.Attributes.Add("bordercolor", "#a7b8d9");
        UserGrid. HeaderRow.Attributes.Add("style", "background-image:url('images/bj1.png')");    }    

最近在做一個網站,顯示資料時為了方便使用了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>