Dynamically change item templet field in grid view
<p>I have a Grid view which contains checkboxex in item templet field. If check box is checked then send mail particular Id. Now When mail is sent the checkbox should replaced by image of sign correct. Now when ever any one have look at the grid view then there should be the image with symbol correct for those whom mail is sent and checkbox for those whom mail is not sent.</p>

<p>//This is the code for my .aspx  page</p>

<pre><code>&lt;%@ Page Title="Search candidates based on vacancy" Language="C#" MasterPageFile="~/HR Department/hrmasterpage.master"
    AutoEventWireup="true" CodeFile="searcAppForVac.aspx.cs" Inherits="HR_Department_searcAppForVac" %&gt;

&lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"&gt;
&lt;/asp:Content&gt;
&lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"&gt;
&lt;table width="100%"&gt;
    &lt;tr&gt;
        &lt;td&gt;
            &amp;nbsp;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td align="center" class="tdtitle"&gt;
            Search Candidates
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;
            &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt;
            &lt;/asp:ScriptManager&gt;
            &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
                &lt;ContentTemplate&gt;
                    &lt;table width="100%"&gt;
                        &lt;tr&gt;
                            &lt;td class="RowHeight" width="20%"&gt;
                                Select Company
                            &lt;/td&gt;
                            &lt;td width="30%"&gt;
                                &lt;asp:DropDownList ID="companyList" runat="server" AppendDataBoundItems="true" AutoPostBack="True"
                                    OnSelectedIndexChanged="companyList_SelectedIndexChanged" Width="150px"&gt;
                                    &lt;asp:ListItem Text="-Select Company-" Value="-1"&gt;&lt;/asp:ListItem&gt;
                                &lt;/asp:DropDownList&gt;
                            &lt;/td&gt;
                            &lt;td width="20%"&gt;
                                Select Department
                            &lt;/td&gt;
                            &lt;td width="30%"&gt;
                                &lt;asp:DropDownList ID="deptList" runat="server" AppendDataBoundItems="true" AutoPostBack="True"
                                    onclick="Validate();" OnSelectedIndexChanged="deptList_SelectedIndexChanged"
                                    Width="150px"&gt;
                                    &lt;asp:ListItem Value="-1"&gt;-Select Department-&lt;/asp:ListItem&gt;
                                &lt;/asp:DropDownList&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td class="RowHeight" width="20%"&gt;
                                Select Vacancy
                            &lt;/td&gt;
                            &lt;td colspan="3" width="*"&gt;
                                &lt;asp:DropDownList ID="vacanyList" runat="server" AppendDataBoundItems="true" 
                                    Width="200px" AutoPostBack="True" 
                                    onselectedindexchanged="vacanyList_SelectedIndexChanged"&gt;
                                    &lt;asp:ListItem Value="-1"&gt;-Select Vacancy-&lt;/asp:ListItem&gt;
                                &lt;/asp:DropDownList&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td colspan="4" align="center"&gt;
                                &amp;nbsp;
                                &lt;asp:Label ID="notifyLbl" runat="server" Font-Size="Large" ForeColor="Red" 
                                    Text="Label"&gt;&lt;/asp:Label&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td colspan="4"&gt;
                                &lt;asp:Label ID="titleLbl" runat="server" Font-Size="Large" ForeColor="Red" 
                                    Text="Label"&gt;&lt;/asp:Label&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td colspan="4"&gt;
                                &amp;nbsp;&lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td colspan="4"&gt;
                                &lt;asp:GridView ID="appForVacGrid" runat="server" AutoGenerateColumns="False" 
                                CellPadding="4" 
                                    onpageindexchanging="appForVacGrid_PageIndexChanging" GridLines="None" 
                                    CssClass="mGrid" DataKeyNames="AppId"&gt;
                                    &lt;RowStyle BackColor="#EFF3FB" /&gt;
                                    &lt;Columns&gt;
                                        &lt;asp:TemplateField&gt;
                                            &lt;HeaderTemplate&gt;
                                                App.ID
                                            &lt;/HeaderTemplate&gt;
                                            &lt;ItemTemplate&gt;
                                                &lt;asp:Label ID="appIdLbl" runat="server" Text='&lt;%# Eval("AppId") %&gt;'&gt;&lt;/asp:Label&gt;
                                            &lt;/ItemTemplate&gt;
                                        &lt;/asp:TemplateField&gt;
                                        &lt;asp:TemplateField&gt;
                                            &lt;HeaderTemplate&gt;
                                                First Name
                                            &lt;/HeaderTemplate&gt;
                                            &lt;ItemTemplate&gt;
                                                &lt;asp:Label ID="firstNameLbl" runat="server" Text='&lt;%# Eval("AppFirstName") %&gt;'&gt;&lt;/asp:Label&gt;
                                            &lt;/ItemTemplate&gt;
                                        &lt;/asp:TemplateField&gt;
                                        &lt;asp:TemplateField&gt;
                                            &lt;HeaderTemplate&gt;
                                                Last Name
                                            &lt;/HeaderTemplate&gt;
                                            &lt;ItemTemplate&gt;
                                                &lt;asp:Label ID="lastNameLbl" runat="server" Text='&lt;%# Eval("AppLastName") %&gt;'&gt;&lt;/asp:Label&gt;
                                            &lt;/ItemTemplate&gt;
                                        &lt;/asp:TemplateField&gt;
                                        &lt;asp:TemplateField&gt;
                                            &lt;HeaderTemplate&gt;
                                                Qualification
                                            &lt;/HeaderTemplate&gt;
                                            &lt;ItemTemplate&gt;
                                                &lt;asp:Label ID="qualiNameLbl" runat="server" Text='&lt;%# Eval("QualiName") %&gt;'&gt;&lt;/asp:Label&gt;
                                            &lt;/ItemTemplate&gt;
                                        &lt;/asp:TemplateField&gt;
                                        &lt;asp:TemplateField&gt;
                                            &lt;HeaderTemplate&gt;
                                                &amp;nbsp;Experience
                                            &lt;/HeaderTemplate&gt;
                                            &lt;ItemTemplate&gt;
                                                &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Eval("TotalExpYear") %&gt;'&gt;&lt;/asp:Label&gt;
                                            &lt;/ItemTemplate&gt;
                                        &lt;/asp:TemplateField&gt;
                                        &lt;asp:TemplateField&gt;
                                            &lt;HeaderTemplate&gt;
                                                EmailId
                                            &lt;/HeaderTemplate&gt;
                                            &lt;ItemTemplate&gt;
                                                &lt;asp:Label ID="emailLbl" runat="server" Text='&lt;%# Eval("AppEmailId1") %&gt;'&gt;&lt;/asp:Label&gt;
                                            &lt;/ItemTemplate&gt;
                                        &lt;/asp:TemplateField&gt;
                                        &lt;asp:TemplateField&gt;
                                            &lt;HeaderTemplate&gt;
                                                Send Mail
                                            &lt;/HeaderTemplate&gt;
                                            &lt;ItemTemplate&gt;
                                                &lt;asp:CheckBox ID="sendMailBox" runat="server" /&gt;
                                            &lt;/ItemTemplate&gt;
                                        &lt;/asp:TemplateField&gt;
                                    &lt;/Columns&gt;
                                    &lt;FooterStyle Font-Bold="True" ForeColor="White" 
                                        HorizontalAlign="Right" /&gt;
                                    &lt;PagerStyle ForeColor="White" HorizontalAlign="Center" 
                                        VerticalAlign="Top" CssClass="pgr" /&gt;
                                    &lt;SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /&gt;
                                    &lt;HeaderStyle Font-Bold="True" ForeColor="White" 
                                        Font-Size="Medium" HorizontalAlign="Left" /&gt;
                                    &lt;AlternatingRowStyle CssClass="alt" /&gt;
                                &lt;/asp:GridView&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td colspan="4" align="center"&gt;
                                &amp;nbsp;&lt;asp:Label ID="noSelectionLbl" runat="server" Font-Bold="True" 
                                    Font-Size="Large" ForeColor="Red" Text="Label"&gt;&lt;/asp:Label&gt;
</code></pre>

<p>&nbsp;
                            
                            
                                
                                    
                                
                            
                            
                                &lt;%--
                                    &nbsp;
                                --%>
                            
                        
                    
                    
                    
                    
                    
                    
                  </p>

<pre><code>        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;
            &amp;nbsp;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;
            &amp;nbsp;
        &lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;

&lt;script type="text/javascript"&gt;

    function alertOnBadSelection() {
        var select = document.getElementById('companyList');
        if (select.options[select.selectedIndex].value == "-Select Company-") {
            alert('Please Select Company!');
            return false;
        }
    }

&lt;/script&gt;

&lt;/asp:Content&gt;
</code></pre>

<p>//This is the code for my .aspx.cs page</p>

<pre><code> using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Data;
 using System.Collections;

public partial class HR_Department_searcAppForVac : System.Web.UI.Page
{
DataOperation oDo = new DataOperation();
AppVacancyDetailClass objAppVacDetail = new AppVacancyDetailClass();
protected void Page_Load(object sender, EventArgs e)
{
    //SET LABLE VISIBILITY TO FALSE.
    notifyLbl.Visible = false;
    titleLbl.Visible = false;
    sendMailBtn.Visible = false;
    noSelectionLbl.Visible = false;

    //SET GRIDVIEW'S PAGGING PROPERTY.
    appForVacGrid.AllowPaging = true;
    appForVacGrid.PageSize = 3;

    try
    {
        if (!IsPostBack)
        {
            Session.Clear();
            //DISABLE DEPARTMENT DROPDOWN LIST AND VACANCY DROPDOWN LIST TILL COMPANY IS NOT SELECTED.
            deptList.Enabled = false;
            vacanyList.Enabled = false;
            //FILL COMPANY DROPDOWN LIST HERE.
            DataTable objCmpnyTable = oDo.DropDownList("select * from tblCompanyMaster");
            if (objCmpnyTable.Rows.Count &gt; 0)
            {
                companyList.DataSource = objCmpnyTable;
                companyList.DataValueField = "CompId";
                companyList.DataTextField = "CompName";
                companyList.DataBind();
            }
            else
            {
                notifyLbl.Visible = true;
                notifyLbl.Text = "There is no company in the list.";
            }
        }
        else
        {
            //DISABLE ALL DROPDOWN LISTS IF COMPANY DROPDOWN LIST IS SET TO ITS DEFAULT VALUE.
            if (companyList.SelectedIndex &lt;= 0)
            {
                deptList.Enabled = false;
                vacanyList.Enabled = false;
            }
        }
     }
     catch (Exception)
     {
          throw;
     }
 }
protected void companyList_SelectedIndexChanged(object sender, EventArgs e)
{
    //DISABLE VACANCY LIST IF DEPARTMENT IS NOT SELECTED.
    vacanyList.Enabled = false;
    //CLEAR GRIDVIEW WHEN NEW COMPANY IS SELECTED.
    appForVacGrid.DataSource = null;
    appForVacGrid.DataBind();
    try
    {
        if (companyList.SelectedIndex &gt; 0)
        {
            deptList.Enabled = true;
            deptList.Items.Clear();
            string str = "select * from vwCompWiseList where CompId=" + companyList.SelectedValue;
            DataTable objDeptTable = oDo.DropDownList("select DeptId,DeptName from vwCompWiseDept where CompId= "+companyList.SelectedValue);
            if (objDeptTable.Rows.Count &gt; 0)
            {
                deptList.DataSource = objDeptTable;
                deptList.DataTextField = "DeptName";
                deptList.DataValueField = "deptId";
                deptList.DataBind();
                //SET DEPARTMENT DROPDOWN LIST TO ITS BEFORE FIRST VALUE
                deptList.Items.Insert(0, new ListItem("--Select Department--", "-1"));
            }
            else 
            {
                deptList.Items.Insert(0, new ListItem("--No Departments--", "-1"));
                notifyLbl.Visible = true;
                notifyLbl.Text = "No Departments Available in " + companyList.SelectedItem.Text;
            }
        }
        else
        {
            notifyLbl.Visible = true;
            notifyLbl.Text = "Select Company....";
            //CLEAR GRIDVIEW
            appForVacGrid.DataSource = null;
            appForVacGrid.DataBind();
        }
    }
    catch (Exception)
    {

        throw;
    }    
}
protected void deptList_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        if (deptList.SelectedIndex &gt; 0)
        {
            //ENABLE VACANCY DROPDOWN LIST WHEN DEPARTMENT IS SELECTED.
            vacanyList.Enabled = true;
            //CLEAR OLD VALUE AND REFILL VACANCY DROPDOWN LIST.
            vacanyList.Items.Clear();

            //GET VACANCIES.
            DataTable objVacancytbl = oDo.DropDownList("select VacId,VacTitle from tblVacancyMaster where DeptId =" + deptList.SelectedValue + " and CompId=" + companyList.SelectedValue);
            if (objVacancytbl.Rows.Count &gt; 0)
            {
                vacanyList.DataSource = objVacancytbl;
                vacanyList.DataValueField = "VacId";
                vacanyList.DataTextField = "VacTitle";
                vacanyList.DataBind();
                //SET VACANCY DROPDOWN LIST BEFORE FIRST VALUE.
                vacanyList.Items.Insert(0, new ListItem("--Select Vacancy--", "-1"));
                appForVacGrid.DataSource = null;
                appForVacGrid.DataBind();
            }
            else
            {
                notifyLbl.Visible = true;
                notifyLbl.Text = "ALL VACANCIES ARE CLOSED IN "+" "+deptList.SelectedItem.Text.ToUpper();
                vacanyList.Enabled = false;
                appForVacGrid.DataSource = null;
                appForVacGrid.DataBind();
            }
        }
        else
        {
            notifyLbl.Visible = true;
            notifyLbl.Text = "Select Department...";
            //CLEAR GRIDVIEW.
            appForVacGrid.DataSource = null;
            appForVacGrid.DataBind();
            vacanyList.Enabled = false;
        }
    }
    catch (Exception)
    {

        throw;
    }
}
protected void vacanyList_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        //FILTER APPLICANTS FOR PERTICULAR VACANCY IN SELECTED DEPARTMETN OF SELECTED COMPANY.
        DataTable AppListTbl = objAppVacDetail.GetValue("CompId=" + companyList.SelectedValue + " and DeptId=" + deptList.SelectedValue + " and VacId=" + vacanyList.SelectedValue);
        if (AppListTbl.Rows.Count &gt; 0)
        {
            appForVacGrid.DataSource = AppListTbl;
            appForVacGrid.DataBind();
            appForVacGrid.Columns[5].Visible = false;
            appForVacGrid.Columns[0].Visible = false;
            Session.Add("snAppListTbl", AppListTbl);
            titleLbl.Visible = true;
            titleLbl.Text = AppListTbl.Rows.Count.ToString() + " " + "CANDIDATE(S) ARE ELIGIBLE FOR THE POST OF" + " " + vacanyList.SelectedItem.Text.ToUpper() + ".";
            sendMailBtn.Visible = true;
        }
    }
    catch (Exception)
    {

        throw;
    }
}
protected void appForVacGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    try
    {
        //FUNCTION TO PERSIST CHECKBOX STATE IN GRIDVIEW DURING PAGGINATION(POSTBACK).
        RememberOldValues();
        titleLbl.Visible = true;
        titleLbl.Text = ((DataTable)Session["snAppListTbl"]).Rows.Count.ToString() + " " + "CANDIDATE(S) ARE ELIGIBLE FOR THE POST OF" + " " + vacanyList.SelectedItem.Text.ToUpper() + ".";
        appForVacGrid.PageIndex = e.NewPageIndex;
        appForVacGrid.DataSource = (DataTable)Session["snAppListTbl"];
        appForVacGrid.DataBind();
        //FUNCTION TO SET CHECKBOX TO ITS PREVIOUS VALUE DURING PAGGINATION.
        RepopulateValues();
        sendMailBtn.Visible = true;
    }
    catch (Exception)
    {

        throw;
    }
}
protected void sendMailBtn_Click(object sender, EventArgs e)
{
    DataTable AppListTable = ((DataTable)Session["snAppListTbl"]);
    //FUNCTION TO PERSIST CHECKBOX STATE IN GRIDVIEW DURING POSTBACK
    RememberOldValues();
    if (Session["CheckBoxValue"] != null)
    {
        //RESET PAGGING PROPERTY AND REBIND GRIDVIEW .
        appForVacGrid.AllowPaging = false;
        appForVacGrid.PageSize = AppListTable.Rows.Count;
        appForVacGrid.DataSource = AppListTable;
        appForVacGrid.DataBind();

        //SET VARIABLES
        ArrayList AppIdList = (ArrayList)Session["CheckBoxValue"];
        string strToId = "", strMailBody = "", strCcId = "", strBccId = "";
        string strFromId = "chetan.patel@sahmed.com";
        string strVacTitle = vacanyList.SelectedItem.Text;
        string strCompName = companyList.SelectedItem.Text;
        string strSubject = "Regarding Selection of Your Resume";

        //GET APPLICANT'S EMAILID IF CHECKBOX IS CHECKED.
        foreach (GridViewRow Row in appForVacGrid.Rows)
        {
            int intIndex = (int)appForVacGrid.DataKeys[Row.RowIndex].Value;
            if (AppIdList.Contains(intIndex))
            {
                if (strToId == "")
                    strToId = ((Label)Row.FindControl("emailLbl")).Text;
                else
                    strToId += "," + ((Label)Row.FindControl("emailLbl")).Text;
            }
        }
        //CREATE MAILBODY.
        strMailBody = CommonProcedures.GetMailBody(strVacTitle, strCompName);
        //SEND MAIL.
        bool isMailSent = true;// CommonProcedures.SendMail(strFromId, strToId, strCcId, strBccId, strSubject, null, strMailBody, false);
        if (isMailSent)
        {
            titleLbl.Visible = true;
            titleLbl.Text = "MAIL HAS BEEN SENT TO THE SELECTED APPLICANTS";
            sendMailBtn.Visible = true;
        }
        else
        {
            titleLbl.Visible = true;
            titleLbl.Text = "MAIL SENDING FAIL.... TRY AGAIN LATER..";
            sendMailBtn.Visible = true;
        }

        //RESET PAGGING PROERTY AND REBIND GRIDVIEW.
        appForVacGrid.AllowPaging = true;
        appForVacGrid.PageSize = 3;
        appForVacGrid.DataSource = AppListTable;
        appForVacGrid.DataBind();
        sendMailBtn.Visible = true;
        Session.Clear();
    }
    else
    {
        noSelectionLbl.Visible = true;
        noSelectionLbl.Text = "NO APPLICANT IS SELECTED...";
        sendMailBtn.Visible = true;
    }

}

//FUNCTION TO PERSIST STATE OF CHECKBOX IN GRIDVIEW
private void RememberOldValues()
{
    ArrayList AppIdList = new ArrayList();
    int intIndex = -1;
    foreach (GridViewRow Rows in appForVacGrid.Rows)
    {
        intIndex = (int)appForVacGrid.DataKeys[Rows.RowIndex].Value;
        CheckBox sendMailBox = ((CheckBox)Rows.FindControl("sendMailBox"));
        if (Session["CheckBoxValue"] != null)
        {
            AppIdList = (ArrayList)Session["CheckBoxValue"];
        }
        if (sendMailBox.Checked)
        {
            if (!AppIdList.Contains(intIndex))
                AppIdList.Add(intIndex);
         }
         else
             AppIdList.Remove(intIndex);
     }
    if (AppIdList.Count &gt; 0 &amp;&amp; AppIdList!=null)
    {
        Session["CheckBoxValue"] = AppIdList;
    }
}
//FUNCTION TO SET CHECKBOX VALUE AFTER POSTBACK.
private void RepopulateValues()
{
    if (Session["CheckBoxValue"] != null)
    {
       ArrayList AppIdList = (ArrayList)Session["CheckBoxValue"];
       if (AppIdList.Count &gt; 0 &amp;&amp; AppIdList != null)
       {
           foreach (GridViewRow Row in appForVacGrid.Rows)
           {
              int intIndex = (int)appForVacGrid.DataKeys[Row.RowIndex].Value;
              if (AppIdList.Contains(intIndex))
              {
                  CheckBox sendMailBox = (CheckBox)Row.FindControl("sendMailBox");
                  sendMailBox.Checked = true;
               }
           }
        }
    }
}
</code></pre>

<p>}</p>

<p>Please Guide me how can I achieve my goal?</p>
