Skip to main content

How To change the background image of html table placed in gridview's template field.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicDiv2.aspx.cs" Inherits="DynamicDiv2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
      <div>
          <asp:GridView ID="GridView1" AutoGenerateColumns="true" runat="Server" OnRowDataBound="GridView1_RowDataBound1">
              <Columns>
                  <asp:TemplateField>
                      <ItemTemplate>
                          <table id="myTable" runat="server" width="200px" height="150px">
                              <tr>
                                  <td>
                                      <asp:Label ID="lblName" runat="Server" Text='<%#Eval("Name")%>'></asp:Label>
                                  </td>
                              </tr>
                          </table>
                      </ItemTemplate>
                  </asp:TemplateField>
              </Columns>
          </asp:GridView>
      </div>
  </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class DynamicDiv2 : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {

       if (!IsPostBack)
       {
           List<dataStruct> mylist = new List<dataStruct>();

           mylist.Add(new dataStruct(new DateTime(1982, 01, 01), "Bill", 'm'));
           mylist.Add(new dataStruct(new DateTime(1981, 01, 01), "Philip", 'm'));
           mylist.Add(new dataStruct(new DateTime(1980, 01, 01), "Susan", 'f'));

           GridView1.DataSource = mylist;
           GridView1.DataBind();
       }

   }

   public struct dataStruct
   {

       private DateTime birthdate;
       private string name;
       private char sex;
       public DateTime BirthDate
       {
           get { return birthdate; }
           set { birthdate = value; }
       }

       public string Name
       {
           get { return name; }
           set { name = value; }
       }

       public char Sex
       {
           get { return sex; }
           set { sex = value; }
       }


       public dataStruct(DateTime _birthdate, string _name, char _sex)
       {

           this.birthdate = _birthdate;
           this.name = _name;
           this.sex = _sex;
       }
   }

   protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           HtmlTable tbl = (HtmlTable)e.Row.FindControl("myTable");
           if (tbl != null)
           {
               if (((Label)tbl.FindControl("lblName")).Text == "Bill")
               {
                   tbl.Attributes.Add("style", "color:red;background-color:green;height:50px);");
               }
               else if (((Label)tbl.FindControl("lblName")).Text == "Philip")
               {
                   tbl.Attributes.Add("style", "background-image:url(images/pdf-icon.jpg);");
               }
               else
               {
                   tbl.Attributes.Add("style", "background-image:url(images/mime_text.png);");
               }

           }
       }

   }
}

Comments

Popular posts from this blog