Monday, April 14, 2008

Highlight Gridview Row when checbox is checked

First thing create Database (Company) with Table (Employee)
which has fields (Id,Name)

Second enter data in Database

Now create new website


In Default.aspx add Gridview and in Gridview add Template field
and in ItemTemplate add checkbox(cbxId)

and in Default.aspx.cs

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.Text;

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

}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

//-- for Highlight
if ((e.Row.RowType == DataControlRowType.DataRow))
{
StringBuilder script = new StringBuilder();
script.Append("if (this.checked) {");
script.Append("document.getElementById('");
script.Append(e.Row.ClientID);
script.Append("').style.backgroundColor='Blue';");
script.Append("} else {");
script.Append("document.getElementById('");
script.Append(e.Row.ClientID);
script.Append("').style.backgroundColor='';");
script.Append("}");
((CheckBox)e.Row.FindControl("cbxId")).Attributes.Add("onclick", script.ToString());

}

}
}

No comments: