GridView小技巧

1,给GridView加上鼠标移动改变背景色效果:

   增加GridView的RowCreated(很明显是当GridView行创建)事件代码
这里this.style.backgroundColor='#C0C0FF' 的值可以是这样的十六进制也可以直接是颜色名称如='red'

  protected   void  GridView1_RowCreated( object  sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow ) //判断当前行是不是数据行
        {
            e.Row.Attributes.Add(
"onmouseover""currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
            
//当鼠标移走时还原该行的背景色
            e.Row.Attributes.Add("onmouseout""this.style.backgroundColor=currentcolor");
        }

    }

 

2: 判断特定条件,更改单元格背景颜色:

  protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
    
{    //数据加载时发生
        if (e.Row.RowType == DataControlRowType.DataRow)
        
{    //判断是否是数据行
            if (e.Row.Cells[8].Text == "USA")
            
{   //判断第9列值是否为"USA"
                
//e.Row.BackColor = System.Drawing.Color.Red;
                e.Row.Cells[8].BackColor = System.Drawing.Color.Red;
            }

        }

    }

3:在父窗体中调用子窗体的gridview值(简单的页面交互):

父窗体代码:

注:单机事件用window.open打开新窗体并获得焦点
< head >
 
< script  language =javascript >
    
function openpage(htmlurl) 
    
{
        
var newwin=window.open(htmlurl,"newWin","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes,top=100,left=200,width=650,height=300");
        newwin.focus();
        
return false;
    }

    
</ script >
</ head >
< body >
< input  type =text  id ="name"   />
    在按钮中调用:
< input  type =button  value ="调用"  onclick ="return openpage('GridViewClientClick.aspx');"   />
</ body >

子窗体代码:

注:girdview中e.Row.Attributes增加单击属性,ReKey并将此行第三列的值传过去
     protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
    
{
        
if(e.Row.RowType==DataControlRowType.DataRow)
        
{
            e.Row.Attributes.Add(
"ondblclick""ReKey('" + e.Row.Cells[2].Text+"')");
            
//e.Row.Attributes["style"] = "Cursor:hand"; 
            
// //键盘事件
            
//e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')");

        }


    }

其中rekey的前台的JS脚本:

注: window.opener是获得该子窗体的父窗体对象getElementById( ' name ' ).value = k;是把参数值传给父窗体的name对象
< script language = javascript >
function  GridViewItemKeyDownEvent(d)
{
    window.alert(
"事件类型: GridViewItemKeyDownEvent  作用对象: " + d);       
}

function  ReKey(k)
{
    window.opener.document.getElementById(
'name').value=k;
    window.close();
}

</ script >

 4:GirdView增加全选列,单击checkbox全选此列

   首先添加一个模版列,在header和item模版中添加checkbox.它的代码如下:

     protected   void  CheckBox2_CheckedChanged( object  sender, EventArgs e)
    
{
        
int i;
        
if (((CheckBox)sender).Checked)
        
{
            
for (i = 0; i < GridView1.Rows.Count; i++)
            
{
                ((CheckBox)GridView1.Rows[i].FindControl(
"CheckBox1")).Checked = true;
            }

        }

        
else
        
{
            
for (i = 0; i < GridView1.Rows.Count; i++)
            
{
                ((CheckBox)GridView1.Rows[i].FindControl(
"CheckBox1")).Checked =false;
            }

        }

    }

 

 5:为删除增加提示效果:
    
     很简单,发个图片就可以明白了----前提是把"删除"列转化为模版列,代码就一句:  return confirm("确定删除吗?")
   

 6:将表格倒出为Excel: 

    protected   void  Button1_Click( object  sender, EventArgs e)
    
{
        Response.Clear();
        Response.Buffer 
= true;
        Response.Charset 
= "GB2312";
        Response.AppendHeader(
"Content-Disposition""attachment;filename=FileName.xls");
        
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType 
= "application/ms-excel";//设置输出文件类型为excel文件。 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter 
= new System.Web.UI.HtmlTextWriter(oStringWriter);
        
this.GridView1.RenderControl(oHtmlTextWriter); 
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();

    }

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值