zoukankan      html  css  js  c++  java
  • GridView固定表头 动态创建模板列 、构造模板选择列触发js方法

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class GridVisdf : System.Web.UI.Page
    {
        
    protected void Page_Load(object sender, EventArgs e)
        {

        }

        
    int i = 0;
        
    public string state(object Month)
        {

          
            
    if (Month != System.DBNull.Value)
            {

                
    return "<a href=DyReportList.aspx? target=_self>浏览报表</a>";

            }
            
    else
            {
                
    return "&nbsp;&nbsp;暂无报表";
            }
        }

        
    public string state33(object Month)
        {

            i
    ++;
            
    if (i %2==1)
            {

                
    return "<a href='#' onclick=" + "PopWindows('" +  i.ToString() + "')" +">【<font color=#ff0000>预定</font>】</a> ";

            }
            
    else
            {
                
    return "";
            }
        }
    }
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridVisdf.aspx.cs" Inherits="GridVisdf" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
      
    // 计算数据,完全可以从数据看取得
      ICollection CreateDataSource( )
      {
        System.Data.DataTable dt 
    = new System.Data.DataTable();
        System.Data.DataRow dr;
        dt.Columns.Add(
    new System.Data.DataColumn("学生班级"typeof(System.String)));
        dt.Columns.Add(
    new System.Data.DataColumn("学生姓名"typeof(System.String)));
        dt.Columns.Add(
    new System.Data.DataColumn("语文"typeof(System.Decimal)));
        dt.Columns.Add(
    new System.Data.DataColumn("数学"typeof(System.Decimal)));
        dt.Columns.Add(
    new System.Data.DataColumn("英语"typeof(System.Decimal)));
        dt.Columns.Add(
    new System.Data.DataColumn("计算机"typeof(System.Decimal)));
        dt.Columns.Add(
    new System.Data.DataColumn("详细"typeof(System.String)));

        
    for (int i = 0; i < 100; i++)
        {
            System.Random rd 
    = new System.Random(Environment.TickCount * i); ;
            dr 
    = dt.NewRow();
            dr[
    0= "班级" + i.ToString();
            dr[
    1= "中南海" + i.ToString();
            dr[
    2= System.Math.Round(rd.NextDouble() * 1002);
            dr[
    3= System.Math.Round(rd.NextDouble() * 1002);
            dr[
    4= System.Math.Round(rd.NextDouble() * 1002);
            dr[
    5= System.Math.Round(rd.NextDouble() * 1002);

            dt.Rows.Add(dr);
        }
        System.Data.DataView dv 
    = new System.Data.DataView(dt);
        
    return dv;
      }

      
    protected void Page_Load( object sender, EventArgs e )
      {
        
    if (!IsPostBack)
        {


            GridView1.Attributes.Add(
    "style""table-layout:fixed");
            GridView1.DataSource 
    = CreateDataSource();
            GridView1.DataBind();
        }
      }
      
    </script>

    <script type="text/javascript">
    function s()
    {
     var t 
    = document.getElementById("<%=GridView1.ClientID%>");
     var t2 
    = t.cloneNode(true)
     
    for(i = t2.rows.length -1;i > 0;i--)
     t2.deleteRow(i)  
     t.deleteRow(
    0)  
     Div1.appendChild(t2) 


    }

    </script>
    <script type="text/javascript"> 

    window.onload 
    = function(){ 
    GridViewColor(
    "<%=GridView1.ClientID%>","#fff","#eee","#6df","#fd6"); 

    //参数依次为(后两个如果指定为空值,则不会发生相应的事件): 
    //GridView ID, 正常行背景色,交替行背景色,鼠标指向行背景色,鼠标点击后背景色 
    function GridViewColor(GridViewId, NormalColor, AlterColor, HoverColor, SelectColor){ 
    //获取所有要控制的行 
    var AllRows = document.getElementById(GridViewId).getElementsByTagName("tr"); 

    //设置每一行的背景色和事件,循环从1开始而非0,可以避开表头那一行 
    for(var i=1; i<AllRows.length; i++){ 
    //设定本行默认的背景色 
    AllRows[i].style.background = i%2==1?NormalColor:AlterColor; 

    //如果指定了鼠标指向的背景色,则添加onmouseover/onmouseout事件 
    //处于选中状态的行发生这两个事件时不改变颜色 
    if(HoverColor != ""){ 
    AllRows[i].onmouseover 
    = function(){if(!this.selected)this.style.background = HoverColor;} 
    if(i%2 == 0){ 
    AllRows[i].onmouseout 
    = function(){if(!this.selected)this.style.background =AlterColor ;} 

    else
    AllRows[i].onmouseout 
    = function(){if(!this.selected)this.style.background = NormalColor;} 



    //如果指定了鼠标点击的背景色,则添加onclick事件 
    //在事件响应中修改被点击行的选中状态 
    if(SelectColor != ""){ 
    AllRows[i].onclick 
    = function()

    //this.style.background = this.style.background==SelectColor?HoverColor:SelectColor; 
    //this.selected = !this.selected; 



    s();

    </script> 
     
    <script  type="text/jscript"  >
              function PopWindows(id)
                     { 
                        
                       alert(id);
                         
                     }

            
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      
    <title>创建表头固定,表体可滚动的GridView</title>
    </head>
    <body>
      
    <form id="Form1" runat="server">
        
    <table>
          
    <tr>
            
            
    <td>
              
    <div id="Div1">
              
    </div>
              
    <div style="overflow-y: scroll; height: 500px">
                
    <asp:GridView ID="GridView1" runat="server" Font-Size="12px" BackColor="White" CellPadding="5" Width="560px" AutoGenerateColumns="False" >
                  
    <HeaderStyle BackColor="#EDEDED" Height="15px" />
                    
    <Columns>
                  
                        
    <asp:TemplateField HeaderText="用户操作项">
                                        
    <ItemTemplate>                                          
                                        
    <%#Eval("学生姓名")%>
                                        
    </ItemTemplate>
                       
    </asp:TemplateField>  <asp:TemplateField HeaderText="用户操作项">
                                        
    <ItemTemplate>                                          
                                        
    <%# state(Eval("详细"))%>
                                        
    </ItemTemplate>
                       
    </asp:TemplateField>  <asp:TemplateField HeaderText="用户操作项">
                                        
    <ItemTemplate>                                          
                                        
    <%# state(Eval("详细"))%>
                                        
    </ItemTemplate>
                       
    </asp:TemplateField>
                        
    <asp:TemplateField HeaderText="用户操作项">
                                        
    <ItemTemplate>                                          
                                         
    <a href="#" onclick="PopWindows('<%#Eval("英语")%>')"><font color=#ff0000>选择</font></a> 
                                        
    </ItemTemplate>
                       
    </asp:TemplateField>
                       
                       
    <asp:TemplateField ShowHeader="False">
                                    
    <ItemTemplate>
                                         
    <%#state33(2)%> 
                                    
    </ItemTemplate> 
                                    
    <ItemStyle  HorizontalAlign="Center" VerticalAlign="Middle" />
                                
    </asp:TemplateField> 
                    
    </Columns>
                
    </asp:GridView>
              
    </div>
            
    </td>
          
    </tr>
        
    </table>      

         
      
    </form>
    </body>
    </html>

  • 相关阅读:
    性能测试_LR11_数据向导
    python django部署linux服务器
    pip升级问题
    解决win10命令提示行下cnpm无反应
    adb常用命令连接设备/查看包名/查看activity
    android虚拟机sdcard操作出现mkdir failed for , Read-only file system的解决办法
    Django项目和应用创建
    mac下配置python虚拟环境
    这个是我的标题_2020_01_01_18_49_21
    这个是我的标题_2020_01_01_18_47_58
  • 原文地址:https://www.cnblogs.com/Fooo/p/1497237.html
Copyright © 2011-2022 走看看