我在gridview中有2列复选框,其中标题也包含复选框。 如果我选择标题复选框,它将检查其colomn中的所有对应复选框。 现在,我想要发生的是如果我选择第1列复选框,第2列复选框应该取消选中,反之亦然。我的代码如下帮助将不胜感激:从2列复选框中选择一个复选框
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
#form1
{
height: 138px;
width: 509px;
}
</style>
<script type="text/javascript" >
function SelectAll(id)
{
// alert(id);
// GridView_ctl01_cbSelectAll
//get reference of GridView control
var cell;
var cellnext;
if(id =='GridView_ctl01_cbSelectAll')
{
var grid = document.getElementById("<%= GridView.ClientID %>");
//variable to contain the cell of the grid
if (grid.rows.length > 0)
{
//loop starts from 1. rows[0] points to the header.
for (i=1; i<grid.rows.length; i++)
{
//get the reference of first column
cell = grid.rows[i].cells[1];
//loop according to the number of childNodes in the cell
for (j=0; j<cell.childNodes.length; j++)
{
//if childNode type is CheckBox
if (cell.childNodes[j].type =="checkbox")
{
//assign the status of the Select All checkbox to the cell checkbox within the
cell.childNodes[j].checked = document.getElementById(id).checked;
}
}
}
}
}
else
{
//alert("yes");
var grid = document.getElementById("<%= GridView.ClientID %>");
//variable to contain the cell of the grid
if (grid.rows.length > 0)
{
//loop starts from 1. rows[0] points to the header.
for (i=1; i<grid.rows.length; i++)
{
//get the reference of first column
cellnext = grid.rows[i].cells[2];
//loop according to the number of childNodes in the cell
for (j=0; j<cellnext.childNodes.length; j++)
{
//if childNode type is CheckBox
if (cellnext.childNodes[j].type =="checkbox")
{
//assign the status of the Select All checkbox to the cell checkbox within the grid
cellnext.childNodes[j].checked = document.getElementById(id).checked;
}
}
}
}
}
}
</script>
</head>
<body>
<form runat ="server" id="form1">
<asp:GridView ID="GridView" runat="server" BorderWidth="1px" AutoGenerateColumns="False"
Width="100%" Visible="True" AutoGenerateEditButton="False" GridLines="none" ShowHeader="true"
SelectedRowStyle-Wrap="false">
<Columns>
<asp:TemplateField>
<ItemStyle Width="20%" />
<HeaderStyle HorizontalAlign="Left" />
<HeaderTemplate>
<strong>Name</strong>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" class="ms-vb" ID="lblemployees" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle Width="35%" />
<HeaderStyle HorizontalAlign="Left" />
<HeaderTemplate>
<strong>
<asp:CheckBox ID="cbSelectAll" runat="server" onclick="return SelectAll(this.id);" AutoPostBack="false" />
Sleep (6 Hours)
</strong>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle Width="50%" />
<HeaderStyle HorizontalAlign="Left" />
<HeaderTemplate>
<strong>
<asp:CheckBox ID="cbSelectAll1" runat="server" onclick="return SelectAll(this.id);" AutoPostBack="false" />
Wake
</strong>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbSelect1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
<p>
</p>
</body>
</html>
而且它的源代码是:
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class _Default
Inherits System.Web.UI.Page
Dim sqlconn As SqlConnection
Dim sqlCmd As SqlCommand
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strsql As String
Dim s As String = "whatever it may be "
sqlconn = New SqlConnection(s)
Dim sqlCmd As New SqlCommand()
Dim sqlAdp As SqlDataAdapter
sqlconn.Open()
strsql = "select name from tab_name"
sqlCmd.Connection = sqlconn
sqlCmd.CommandText = strsql
sqlAdp = New SqlDataAdapter()
sqlAdp.SelectCommand = sqlCmd
Dim DS As New DataSet()
sqlAdp.Fill(DS, "Return")
Dim dt As New DataTable()
dt = DS.Tables(0)
sqlCmd.Dispose()
sqlconn.Close()
GridView.DataSource = dt
GridView.DataBind()
End Sub
'Protected Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView.RowDataBound
' If e.Row.RowType = DataControlRowType.Header Then
' 'Find the checkbox control in header and add an attribute
' DirectCast(e.Row.FindControl("cbSelectAll"), CheckBox).Attributes.Add("onclick", "javascript:SelectAll('" + DirectCast(e.Row.FindControl("cbSelectAll"), CheckBox).ClientID & "')")
' End If
'End Sub
End Class
任何帮助吗?将不胜感激 – 2011-12-16 13:27:52