页面是ASP.NET 2.0的现在页面有一个GridView1控件GridView1控件里有一TemplateField(模板)列,里面放着CheckBox1选择控件
//现在是这样,在页面的底部,有一个Button1控件,这个Button1控件不在GridView1控件里,是在页面里的. 我想实现这样一种功能:我点击Button1控件时,用JavaScript来全部选择GridView1控件里的TemplateField(模板)列里的CheckBox1选择控件.也就是常见的"全部选中"功能.请各位朋友有类似代码的,给一下,谢谢
//现在是这样,在页面的底部,有一个Button1控件,这个Button1控件不在GridView1控件里,是在页面里的. 我想实现这样一种功能:我点击Button1控件时,用JavaScript来全部选择GridView1控件里的TemplateField(模板)列里的CheckBox1选择控件.也就是常见的"全部选中"功能.请各位朋友有类似代码的,给一下,谢谢
CheckBox与GridView结合:
http://www.cnblogs.com/insus/articles/1411613.html做选择:
http://www.cnblogs.com/insus/articles/1413740.html
{
for (var i=0;i<frm.elements.length;i++)
frm.elements[i].checked = frm.selectall.checked;
}
function CheckSelect(frm)
{
var col=frm.elements;
var obj=frm.selectall;
var intEls=0,intChks=0;
for(var i=0;i<col.length;i++)
{
if(col[i].type=="checkbox"&&col[i].name!="selectall")
{
intEls++;
if(col[i].checked)
intChks++;
}
}
obj.checked=(intEls==intChks);
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="15" DataKeyNames="XTBH"
AllowPaging="true" Width="100%" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
<AlternatingRowStyle CssClass="Alternatingback"></AlternatingRowStyle>
<RowStyle CssClass="griditem"></RowStyle>
<HeaderStyle CssClass="gridheader" Font-Size="10"></HeaderStyle>
<Columns>
<asp:TemplateField HeaderText="<input type=checkbox name='selectall' onClick='SelectAll(document.form1)'>全选">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" Width="8%"></HeaderStyle>
<ItemTemplate>
<asp:CheckBox id="CheckBox1" runat="server" ToolTip='<%#DataBinder.Eval(Container.DataItem,"BH")%>'>
</asp:CheckBox>
<asp:HiddenField ID="Hd_SH" Value='<%#DataBinder.Eval(Container.DataItem,"SH")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="XTBH" HeaderText="编 号" />
<asp:ButtonField Text="修改" HeaderText="修改" CommandName="Modify" >
<HeaderStyle Width="5%"></HeaderStyle>
</asp:ButtonField>
</Columns>
<PagerSettings Visible ="false" />
</asp:GridView>
function CheckAll()
{
var objs=document.getElementsById("<%#GridView1.ClientId%>").getElementsByTagName("input");
for(var i=0;i< objs.length;i++)
{
if(objs[i].type=="check")
{
objs[i].checked=true;
}
}
}
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat ="server">
<script type="text/javascript">
function changecolor(cbo,o)
{
var theBox=cbo;
var tr=document.getElementById(o);
if(theBox.checked)
{
tr.style.backgroundColor="#B9D3FF";
}
else
{
tr.style.backgroundColor="#FFFFFF";
}
}
function SelectAll(tempControl)
{
var theBox=tempControl;
var xState=theBox.checked;
var elem=theBox.form.elements;
for(i=0;i<elem.length;i++)
{
if(elem[i].type=="checkbox" && elem[i].id!=theBox.id)
{
if(elem[i].checked!=xState)
{
elem[i].click();
}
}
} }
</script>
<table border="0" width="85%" align="left" cellpadding="2" cellspacing="1" style="margin-left:30px; margin-top:30px;" >
<tr>
<td align="center" colspan="6" style="height: 23px" background="../images/bj.bmp">
<asp:Label ID="Label1" runat="server" Text="新闻管理"></asp:Label></td>
</tr>
<tr>
<td style="height: 12px;" colspan="6" align="center">
<asp:GridView ID="GridNewsInfo" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="NewsID" ForeColor="#333333" GridLines="None"
OnRowDataBound="GridNewsInfo_RowDataBound1" Width="100%" OnRowDeleting="GridNewsInfo_RowDeleting">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="CheckAll" type="checkbox" onclick="SelectAll(this)"; />
<%-- <asp:CheckBox ID="CheckAll" runat="server" onclick="SelectAll(this)"; />--%>
</HeaderTemplate>
<ItemTemplate>
<%-- <input id="CheckSingle" type="checkbox" />--%>
<asp:CheckBox ID="CheckSingle" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NewsID" HeaderText="编号" InsertVisible="False" ReadOnly="True"
SortExpression="NewsID" >
<ControlStyle Font-Size="12px" />
<HeaderStyle Font-Size="9pt" />
</asp:BoundField>
<asp:BoundField DataField="NewsTitle" HeaderText="新闻标题" SortExpression="NewsTitle">
<ControlStyle Width="150px" Font-Size="12px" />
<HeaderStyle Font-Size="9pt" />
</asp:BoundField>
<asp:BoundField DataField="CreateTime" HeaderText="创建时间" SortExpression="CreateTime" DataFormatString="{0:yyyy年M月d日}" HtmlEncode="False">
<ControlStyle Width="100px" />
<HeaderStyle Font-Size="9pt" />
</asp:BoundField>
<asp:BoundField DataField="Author" HeaderText="作者" SortExpression="Author">
<ControlStyle Width="80px" />
<HeaderStyle Font-Size="9pt" />
</asp:BoundField>
<asp:HyperLinkField DataNavigateUrlFields="NewsID" DataNavigateUrlFormatString="EditNewsInfo.aspx?NID={0}"
HeaderText="修改新闻信息" Text="修改新闻信息" >
<HeaderStyle Font-Size="9pt" />
</asp:HyperLinkField>
<asp:CommandField ShowDeleteButton="True" >
<HeaderStyle Font-Size="9pt" />
</asp:CommandField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<%-- <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:BJSYSSConnectionString %>" DeleteCommand="DELETE FROM [NewsInfo] WHERE [NewsID] = @original_NewsID AND [NewsTitle] = @original_NewsTitle AND [Author] = @original_Author"
InsertCommand="INSERT INTO [NewsInfo] ([NewsTitle], [CreateTime], [Author]) VALUES (@NewsTitle, @CreateTime, @Author)"
OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [NewsID], [NewsTitle], [CreateTime], [Author] FROM [NewsInfo]"
UpdateCommand="UPDATE [NewsInfo] SET [NewsTitle] = @NewsTitle, [CreateTime] = @CreateTime, [Author] = @Author WHERE [NewsID] = @original_NewsID AND [NewsTitle] = @original_NewsTitle AND [CreateTime] = @original_CreateTime AND [Author] = @original_Author">
<DeleteParameters>
<asp:Parameter Name="original_NewsID" Type="Int32" />
<asp:Parameter Name="original_NewsTitle" Type="String" />
<asp:Parameter Name="original_CreateTime" Type="DateTime" />
<asp:Parameter Name="original_Author" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="NewsTitle" Type="String" />
<asp:Parameter Name="CreateTime" Type="DateTime" />
<asp:Parameter Name="Author" Type="String" />
<asp:Parameter Name="original_NewsID" Type="Int32" />
<asp:Parameter Name="original_NewsTitle" Type="String" />
<asp:Parameter Name="original_CreateTime" Type="DateTime" />
<asp:Parameter Name="original_Author" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="NewsTitle" Type="String" />
<asp:Parameter Name="CreateTime" Type="DateTime" />
<asp:Parameter Name="Author" Type="String" />
</InsertParameters>
</asp:SqlDataSource>--%>
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="删除" /></td>
<td colspan="4">
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" OnPageChanging="AspNetPager1_PageChanging">
</webdiyer:AspNetPager>
</td>
<td style="width: 100px">
</td>
</tr>
</table>
</asp:Content>