62,254
社区成员
发帖
与我相关
我的任务
分享<script>
function Checkall(XelementName)
{
var n=0;
var checkboxes=document.getElementsByName(XelementName);
for(var i=0;i<checkboxes.length;i++)
{
if (checkboxes[i].checked)
n++;
}
if (n>1)
alert("只能选1个");
}
</script>
<head runat="server">
<title>Tim 演示</title>
<script>
function Check() {
var flag = 0;
var inputs = document.getElementsByTagName("INPUT")
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox" && inputs[i].id.indexOf("GridView1") >= 0 && inputs[i].id.indexOf("chkSelect") >= 0 && inputs[i].checked) {
flag ++;
}
}
if (flag >= 2) {
alert('选择了多于 2个' +flag);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField HeaderText="Id" DataField="ID"></asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<input type="checkbox" id="chkAll" name="chkAll" onclick="Check()" />
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
for (int i = 0; i < 10; i++)
{
dt.Rows.Add(i.ToString());
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}