<tr style='<%# (Container.DisplayIndex % 2 == 0) ? "background-color:white;" : "background-color:#EEEEEE;" %>'>
Container.DisplayIndex 為資料項索引 (0-based),奇偶判斷套用不同背景色。
<script>
function tr_bgcolor(chk) {
var tr = chk.parentNode.parentNode;
tr.rowIndex % 2 === 0
? tr.style.backgroundColor = chk.checked ? '#add6a6' : '#eee'
: tr.style.backgroundColor = chk.checked ? '#add6d6' : '';
}
function selall(chk) {
var inputs = chk.form.elements;
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type === 'checkbox' && inputs[i] !== chk) {
inputs[i].checked = chk.checked;
tr_bgcolor(inputs[i]);
}
}
}
</script>
<style>
#grid td { border:1px solid #ccc; padding:4px; text-align:center; }
.tr1 { background:#eee; color:red; }
.tr2 { background:#ccc; color:blue; }
</style>
<script>
function colorRows(tableId) {
var rows = document.getElementById(tableId).rows;
for (var i = 0; i < rows.length; i++)
rows[i].className = i % 2 ? "tr1" : "tr2";
}
</script>
<body onload="colorRows('grid')">
<table id="grid">...</table>
style.backgroundColor 更易維護。