gridView中
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="cbId" checked="false" runat="server" />
</ItemTemplate>
</asp:TemplateField>
gridview的RowDataBound事件
/// <summary>
/// 列表数据行绑定事件处理程序
/// </summary>
/// <param name="sender">事件对象</param>
/// <param name="e">事件参数</param>
protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
//为每个checkbox绑定setRadio事件
System.Web.UI.WebControls.CheckBox cbx = e.Row.FindControl("cbId") as System.Web.UI.WebControls.CheckBox;
try
{
//绑定选中CheckBox客户端ID
cbx.Attributes.Add("onclick", "Change(" + cbx.ClientID + ")");
GridViewRow item = e.Row;
if (item.RowType.Equals(DataControlRowType.DataRow))
{
CustomerCardInfosInfo customerCardModel = (CustomerCardInfosInfo)item.DataItem;
//卡状态
item.Cells[4].Text = CommonOperation.GetStatus(customerCardModel.Status);
}
}
catch
{
}
}
前台js实现checkbox单选效果
<script type="text/javascript">
function Change(SCHeckBox) {
var objs = document.getElementsByTagName("input");
for (var i = 0; i < objs.length; i++) {
if (objs[i].type.toLowerCase() == "checkbox")
objs[i].checked = false;
}
var SelectCheckBoxID = SCHeckBox.id;
document.getElementById(SelectCheckBoxID).checked = true;
}
</script>
后台获取gridview列表值
foreach (GridViewRow gvr in gvList.Rows)
{
if (((CheckBox)gvr.FindControl("cbId")).Checked)
{
int index = gvr.RowIndex;
asn = gvList.Rows[index].Cells[1].Text;
driverName = gvList.Rows[index].Cells[2].Text;
vehicleNo = gvList.Rows[index].Cells[3].Text;
}
}