只能选择GridView中的一个CheckBox(单选CheckBox)
方法一:
View Code
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox cbx = e.Row.FindControl("cbID") as CheckBox;
try
{
//绑定选中CheckBox 客户端ID
cbx.Attributes.Add("onclick", "Change(" + cbx.ClientID + ")");
}
catch
{
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor ='#6699ff'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor =currentcolor");
}
}
后台源中
<script type="text/javascript">
function Change(SCheckBox) {
//找到页面所有 input
var objs = document.getElementsByTagName("input");
for (var i = 0; i < objs.length; i++) {
//找到input中的checkbox
if (objs[i].type.toLowerCase() == "checkbox")
//所有checkbox为false
objs[i].checked = false;
}
//找到选中checkbox
var SelectCheckBoxID = SCheckBox.id;
//选中checkbox为true
document.getElementById(SelectCheckBoxID).checked = true;
}
</script>
方法二:
View Code
//识别不同的浏览器
function getTargetElement(evt) {
var elem
if (evt.target) {
elem = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target
}
else {
elem = evt.srcElement
}
return elem
}
var lastD = null;
function Change(evt) {
evt = (evt) ? evt : ((window.event) ? window.event : " ");
if (evt == " ") {
return;
}
var obj = getTargetElement(evt);
var hasTreeNode = false;
if (obj.tagName) {
if (obj.tagName == "INPUT" && obj.type == "checkbox") {
if (lastD) lastD.checked = false
lastD = obj;
obj.checked = true;
}
}
}
View Code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
GridView1.Attributes.Add("onclick", "Change(event)");
}
catch
{
}
}