Yasserzaid’s Weblog

December 25, 2008

Highlight Gridview Row when checbox is checked

Filed under: ASP.Net — yasserzaid @ 9:41 pm

Hi

try this example:

add Gridview and in Gridview add Template field
and in ItemTemplate add checkbox(Id = 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());

}

}
}

Good Luck

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.