把Girdview鼠标经过时行变色应用到所有页面,以及客户端全选

写一个公用的JS函数

代码
   
     
function GridViewColor(GridViewId) {
// 获取所有要控制的行
var AllRows = document.getElementById(GridViewId);
if (AllRows != null ) {
AllRows
= AllRows.getElementsByTagName( " tr " );

// 设置每一行的背景色和事件,循环从1开始而非0,可以避开表头那一行
for ( var i = 1 ; i < AllRows.length; i ++ ) {
AllRows[i].onmouseout
= function () { this .style.background = " #FFFFFF " ; }
}
}
}

引用这段JS,只要在每个页面的Page_Load中注册即可,ctl00_PageBody_gvItems为GirdView的客户端ID 

  
    
ScriptManager.RegisterStartupScript( this .Page, this .GetType(), " js " , " GridViewColor('ctl00_PageBody_gvItems'); " , true );

全选的代码:

  
    
<script type="text/javascript">
 
function CheckAll(oCheckbox)
  {
   
var gvItems = document.getElementById("<%=gvItems.ClientID %>");
   
for (i = 1; i < gvItems.rows.length; i++)
    {
        gvItems.rows[i].cells[
0].getElementsByTagName("INPUT")[0].checked = oCheckbox.checked;
    }
  }
 
</script>
<asp:GridView ID="gvItems" runat="server" >                           
    < Columns >
    < asp:TemplateField HeaderText ="选项" >
      < HeaderTemplate >
        < asp:CheckBox ID ="CheckBox1" runat ="server" Text ="全选" AutoPostBack ="true" onclick ="CheckAll(this)" />
      </ HeaderTemplate >
      <ItemTemplate>
        <asp:CheckBox ID="rbtnName"  runat="server" cid='<%# Eval("CandidatesID") %>'
                                            oncheckedchanged="rbtnName_CheckedChanged"/>
      </ItemTemplate>
    
</ asp:TemplateField >                                
  
</ Columns >
  </ asp:GridView >

 

你可能感兴趣的:(view)