GridView使用技巧小结

本文介绍ASP.NET中GridView控件的应用技巧,包括鼠标悬停效果、双击事件、键盘事件响应、条件背景色设置、全选功能实现、删除确认对话框、导出到Excel等功能的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

①添加鼠标移动事件
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"); } }
②添加双击事件
【客户端】 function ReKey(k) { alert("姓名"+k); } 【服务器端】 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+"')"); } }
 ③添加键盘事件
【客户端】 <mce:script language=javascript><!-- function GridViewItemKeyDownEvent(d) { window.alert("事件类型: GridViewItemKeyDownEvent 作用对象:" + d); } // --></mce:script> 【服务器端】 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if(e.Row.RowType==DataControlRowType.DataRow) { //键盘事件 e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')"); } }
 ④添加修改背景颜色事件(根据内容判断)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[8].Text == "USA") { //e.Row.BackColor = System.Drawing.Color.Red; e.Row.Cells[8].BackColor = System.Drawing.Color.Red; } } }
⑤添加全选效果
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; } } }
  ⑥添加删除确认效果
<asp:TemplateField HeaderText="删除" ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick='return confirm("确认要删除吗?")' Text="删除"></asp:LinkButton> </ItemTemplate> </asp:TemplateField>
⑦导出到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(); }
⑧不使用数据源控件的GridView。(只显示标题,不显示内容)
思路:添加一空行; private void AddDummyData(DataSet ds) { // Add a dummy row DataTable dt = ds.Tables[0]; DataRow newRow = dt.NewRow(); dt.Rows.Add(newRow); } 
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值