代码改变世界

Asp.net 2.0 GridView增加一列checkBox,并全选

2007-04-05 12:48  ruinet  阅读(2997)  评论(1编辑  收藏  举报
效果如图:

代码如下:
   <script language="javascript" type="text/javascript">
<!--

function SelectAll()
{
   
var e=document.getElementsByTagName("input");
   
var IsTrue;
   
if(document.getElementById("selall").value=="全选")
  
{
   IsTrue
=true;
   document.getElementById(
"selall").value="全否"
  }

  
else
  
{
  IsTrue
=false;
  document.getElementById(
"selall").value="全选"
  }

  
 
for(var i=0;i<e.length;i++)
 
{
  
if (e[i].type=="checkbox")
  
{
     e[i].checked
=IsTrue;
  }

 }

}

// -->
    </script>

<div>
            
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
                DataSourceID
="SqlDataSource1" ShowFooter="True">
                
<Columns>
                    
<asp:TemplateField>
                        
<FooterTemplate>
                            
<input id="selall" type="button" value="全选" language="javascript" onclick="return SelectAll()" />
                        
</FooterTemplate>
                        
<ItemTemplate>
                            
&nbsp;<input id="Checkbox1" type="checkbox" />
                        
</ItemTemplate>
                    
</asp:TemplateField>
                    
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                        SortExpression
="ID" />
                    
<asp:BoundField DataField="WorkerID" HeaderText="WorkerID" SortExpression="WorkerID" />
                    
<asp:BoundField DataField="Content" HeaderText="Content" SortExpression="Content" />
                    
<asp:BoundField DataField="WorkDate" HeaderText="WorkDate" SortExpression="WorkDate" />
                    
<asp:BoundField DataField="ApplyDate" HeaderText="ApplyDate" SortExpression="ApplyDate" />
                
</Columns>
            
</asp:GridView>
            
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                SelectCommand
="SELECT * FROM [SMCGZ_WorkInfo]"></asp:SqlDataSource>
        
</div>

free web counter