js實(shí)現(xiàn)GridView單選效果自動(dòng)設(shè)置交替行、選中行、鼠標(biāo)移動(dòng)行背景色
更新時(shí)間:2010年05月27日 03:14:44 作者:
使用js實(shí)現(xiàn)GridView單選效果自動(dòng)設(shè)置交替行、選中行、鼠標(biāo)移動(dòng)行背景色
后臺(tái)代碼
/// <summary>
/// 數(shù)據(jù)行綁定事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvProduct_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
GridViewRow row = e.Row;
CheckBox ckb = row.Cells[0].FindControl("ckb") as CheckBox;
Label ProductID = row.Cells[0].FindControl("lblProductID") as Label;
//當(dāng)鼠標(biāo)停留時(shí)更改背景色
row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00A9FF'");
//當(dāng)鼠標(biāo)移開時(shí)還原背景色
row.Attributes.Add("onmouseout", "gvProducts_onmouseout('" + gvProducts.ClientID + "','" + ckb.ClientID + "',this) ");
//當(dāng)鼠標(biāo)移開時(shí)還原背景色
row.Attributes.Add("onclick", "SelectRadio('" + gvProducts.ClientID + "','" + ckb.ClientID + "','" + ProductID.Text + "',this) ");
ckb.Attributes.Add("onclick", "SelectRadio('" + gvProducts.ClientID + "','" + ckb.ClientID + "','" + ProductID.Text + "',document.getElementById('" + row.ClientID + "')) ");
}
catch (Exception ex)
{
}
}
前臺(tái)代碼
/****************************************************/
//功能:鼠標(biāo)移出時(shí)設(shè)置行顏色
//說明:onmouseout事件時(shí)使用
//作者:XXXXX
//日期:2010年5月26日
/****************************************************/
function gvUsers_onmouseout(listId, SelectRadioID, row) {
var SelectRadio = document.getElementById(SelectRadioID);
//找到控制范圍
var GridViewtableSearchList = document.getElementById(listId);
//找到控制范圍下所有input
var objs = GridViewtableSearchList.getElementsByTagName("input");
//找到控制范圍下所有checkbox并都變?yōu)閒alse
for (var i = 0; i < objs.length; i++) {
if (objs[i].type.toLowerCase() == "checkbox" && objs[i] == SelectRadio) {
if (SelectRadio.checked) {
//設(shè)置選中行的顏色
row.style.backgroundColor = '#33A922'
}
else {
//設(shè)置交替行的顏色
if (i % 2 == 0) {
row.style.backgroundColor = '#FFFFFF'
}
else {
row.style.backgroundColor = '#F4FAFD'
}
}
}
}
}
/****************************************************/
//功能:鼠標(biāo)單擊時(shí)使用
//說明:onmouseout事件時(shí)使用
//作者:XXXXXX
//日期:2010年5月26日
/****************************************************/
function SelectRadio(listId, SelectRadioID, rv, row) {
var SelectRadio = document.getElementById(SelectRadioID);
//找到控制范圍
var GridViewtableSearchList = document.getElementById(listId);
//找到控制范圍下所有input
var objs = GridViewtableSearchList.getElementsByTagName("input");
//找到控制范圍下所有checkbox并都變?yōu)閒alse
for (var i = 0; i < objs.length; i++) {
//設(shè)置除當(dāng)前選擇行外其它行的背景色
if (objs[i].type.toLowerCase() == "checkbox" && objs[i] != SelectRadio) {
objs[i].checked = false;
//設(shè)置交替行的背景色
if (i % 2 == 0) {
objs[i].parentElement.parentElement.style.backgroundColor = '#FFFFFF'
}
else {
objs[i].parentElement.parentElement.style.backgroundColor = '#F4FAFD'
}
}
}
var SelectRadioSelectRadioID = SelectRadio.id;
SelectRadio.checked = !SelectRadio.checked
//設(shè)置當(dāng)前選擇行的背景色和返回選擇行的主鍵
if (SelectRadio.checked) {
row.style.backgroundColor = '#33A922'
window.returnValue = rv;
}
else {
window.returnValue = ""
}
}
復(fù)制代碼 代碼如下:
/// <summary>
/// 數(shù)據(jù)行綁定事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvProduct_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
GridViewRow row = e.Row;
CheckBox ckb = row.Cells[0].FindControl("ckb") as CheckBox;
Label ProductID = row.Cells[0].FindControl("lblProductID") as Label;
//當(dāng)鼠標(biāo)停留時(shí)更改背景色
row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00A9FF'");
//當(dāng)鼠標(biāo)移開時(shí)還原背景色
row.Attributes.Add("onmouseout", "gvProducts_onmouseout('" + gvProducts.ClientID + "','" + ckb.ClientID + "',this) ");
//當(dāng)鼠標(biāo)移開時(shí)還原背景色
row.Attributes.Add("onclick", "SelectRadio('" + gvProducts.ClientID + "','" + ckb.ClientID + "','" + ProductID.Text + "',this) ");
ckb.Attributes.Add("onclick", "SelectRadio('" + gvProducts.ClientID + "','" + ckb.ClientID + "','" + ProductID.Text + "',document.getElementById('" + row.ClientID + "')) ");
}
catch (Exception ex)
{
}
}
前臺(tái)代碼
復(fù)制代碼 代碼如下:
/****************************************************/
//功能:鼠標(biāo)移出時(shí)設(shè)置行顏色
//說明:onmouseout事件時(shí)使用
//作者:XXXXX
//日期:2010年5月26日
/****************************************************/
function gvUsers_onmouseout(listId, SelectRadioID, row) {
var SelectRadio = document.getElementById(SelectRadioID);
//找到控制范圍
var GridViewtableSearchList = document.getElementById(listId);
//找到控制范圍下所有input
var objs = GridViewtableSearchList.getElementsByTagName("input");
//找到控制范圍下所有checkbox并都變?yōu)閒alse
for (var i = 0; i < objs.length; i++) {
if (objs[i].type.toLowerCase() == "checkbox" && objs[i] == SelectRadio) {
if (SelectRadio.checked) {
//設(shè)置選中行的顏色
row.style.backgroundColor = '#33A922'
}
else {
//設(shè)置交替行的顏色
if (i % 2 == 0) {
row.style.backgroundColor = '#FFFFFF'
}
else {
row.style.backgroundColor = '#F4FAFD'
}
}
}
}
}
/****************************************************/
//功能:鼠標(biāo)單擊時(shí)使用
//說明:onmouseout事件時(shí)使用
//作者:XXXXXX
//日期:2010年5月26日
/****************************************************/
function SelectRadio(listId, SelectRadioID, rv, row) {
var SelectRadio = document.getElementById(SelectRadioID);
//找到控制范圍
var GridViewtableSearchList = document.getElementById(listId);
//找到控制范圍下所有input
var objs = GridViewtableSearchList.getElementsByTagName("input");
//找到控制范圍下所有checkbox并都變?yōu)閒alse
for (var i = 0; i < objs.length; i++) {
//設(shè)置除當(dāng)前選擇行外其它行的背景色
if (objs[i].type.toLowerCase() == "checkbox" && objs[i] != SelectRadio) {
objs[i].checked = false;
//設(shè)置交替行的背景色
if (i % 2 == 0) {
objs[i].parentElement.parentElement.style.backgroundColor = '#FFFFFF'
}
else {
objs[i].parentElement.parentElement.style.backgroundColor = '#F4FAFD'
}
}
}
var SelectRadioSelectRadioID = SelectRadio.id;
SelectRadio.checked = !SelectRadio.checked
//設(shè)置當(dāng)前選擇行的背景色和返回選擇行的主鍵
if (SelectRadio.checked) {
row.style.backgroundColor = '#33A922'
window.returnValue = rv;
}
else {
window.returnValue = ""
}
}
您可能感興趣的文章:
- Android利用GridView實(shí)現(xiàn)單選功能
- asp.net GridView中使用RadioButton單選按鈕的方法
- ASP.NET GridView中加入RadioButton不能單選的解決方案
- gridview中實(shí)現(xiàn)radiobutton的單選示例
- DataGridView中CheckBox實(shí)現(xiàn)某一列單選
- Gridview使用CheckBox全選與單選采用js實(shí)現(xiàn)同時(shí)高亮顯示選擇行
- asp.net 擴(kuò)展GridView 增加單選按鈕列的代碼
- Android利用GridView實(shí)現(xiàn)單選效果
相關(guān)文章
JavaScript 選中文字并響應(yīng)獲取的實(shí)現(xiàn)代碼
當(dāng)鼠標(biāo)選擇一段文字時(shí),對(duì)這個(gè)事件產(chǎn)生響應(yīng),并且將選中的文字傳遞出去。2011-08-08
JS中的六種繼承方式以及優(yōu)缺點(diǎn)總結(jié)
JS作為面向?qū)ο蟮娜躅愋驼Z言,繼承也是其非常強(qiáng)大的特性之一,那么如何在JS中實(shí)現(xiàn)繼承呢?下面這篇文章主要給大家介紹了關(guān)于JS中六種繼承方式以及優(yōu)缺點(diǎn)的相關(guān)資料,需要的朋友可以參考下2021-10-10
解決layui數(shù)據(jù)表格Date日期格式的回顯Object的問題
今天小編就為大家分享一篇解決layui數(shù)據(jù)表格Date日期格式的回顯Object的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
js實(shí)現(xiàn)鼠標(biāo)觸發(fā)圖片抖動(dòng)效果的方法
這篇文章主要介紹了js實(shí)現(xiàn)鼠標(biāo)觸發(fā)圖片抖動(dòng)效果的方法,通過定時(shí)器定時(shí)遞歸調(diào)用rattleimage函數(shù)實(shí)現(xiàn)抖動(dòng)效果,非常實(shí)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02
Ajax+FormData+javascript實(shí)現(xiàn)無刷新表單信息提交
在前端開發(fā)中ajax,formdata和js實(shí)現(xiàn)無刷新表單信息提交非常棒,接下來通過本文給大家介紹Ajax+FormData+javascript實(shí)現(xiàn)無刷新表單信息提交的相關(guān)資料,需要的朋友可以參考下2016-10-10

