Home > ASP.Net > Highlight Gridview Row when checbox is checked

Highlight Gridview Row when checbox is checked


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

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: