深入挖掘ASP.NET 2.0系列课程(1):随心所欲的使用GridView(笔记)
2007-09-29 22:23
666 查看
1.GridView列实现内容交互效果
修改背景颜色与添加交互效果
RowCreated方法添加列元素属性(对行处理时)
RowDataBound(对数据处理时)
1.1.添加鼠标移动事件
GridViewClient.aspx
GridViewClient.aspx.cs
//后台代码
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");
}
}
1.2.添加单击事件
GetInfo.htm 打开窗口
GridViewClientClick.aspx 双击事件返回值/按下键时
GridViewClientClick.aspx.cs
1.3.添加键盘事件
同上
1.4.添加修改背景颜色事件
ChangeBackColor.aspx
ChangeBackColor.aspx.cs
1 //后台代码
2 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
3 {
4 if (e.Row.RowType == DataControlRowType.DataRow)
5 {
6 if (e.Row.Cells[8].Text == "USA")
7 {
8 //e.Row.BackColor = System.Drawing.Color.Red;
9 e.Row.Cells[8].BackColor = System.Drawing.Color.Red;
}
}
}
2.使用模版列
2.1.添加全选效果
ChoseAll.aspx
ChoseAll.aspx.cs
1
2 //前台代码
3 <asp:TemplateField HeaderText="选取">
4 <HeaderTemplate>
5 选择全部<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged" />
6 </HeaderTemplate>
7 <ItemTemplate>
8 <asp:CheckBox ID="CheckBox1" runat="server" />
9 </ItemTemplate>
</asp:TemplateField>
//后台代码
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;
}
}
}
2.2.添加删除确认效果
GridViewDelete.aspx
GridViewDelete.aspx.cs
//前台代码
<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>
2.3.添加图片显示效果
GetImage.ashx(一般处理程序)
GridViewImage.aspx
GridViewImage.aspx.cs
1 //前台代码
2 <asp:TemplateField HeaderText="Photo">
3 <ItemTemplate>
4 <img src='GetImage.ashx?eid=<%#Eval("EmployeeID")%>' />
5 </ItemTemplate>
6 </asp:TemplateField>
7
8
9 //后台代码
<%@ WebHandler Language="C#" Class="GetImage" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data.Sql;
public class GetImage : IHttpHandler {
public void ProcessRequest (HttpContext context) {
using (SqlConnection sc = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
sc.Open();
String txtsql = "select photo from employees where employeeid=" + context.Request.QueryString["eid"];
SqlCommand scd = new SqlCommand(txtsql, sc);
context.Response.Clear();
context.Response.ContentType = "image/bmp";
byte[] bitmapBytes = (byte[])scd.ExecuteScalar();
int length = bitmapBytes.Length;
context.Response.OutputStream.Write(bitmapBytes, 78, bitmapBytes.Length - 78);
context.Response.Cache.SetCacheability(HttpCacheability.Public);
}
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
3.GridView的导出数据(Excel)
ExportToExcel.aspx
ExportToExcel.aspx.cs
1 //前台代码
2 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />
3
4 //后台代码
5 protected void Button1_Click(object sender, EventArgs e)
6 {
7 Response.Clear();
8 Response.Buffer = true;
9 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();
}
public override void VerifyRenderingInServerForm( Control control )
{ }
4.不使用数据源控件的GridView
分页等
空行处理
1 private void AddDummyData(DataSet ds)
2 {
3
4 // Add a dummy row
5
6 DataTable dt = ds.Tables[0];
7
8 DataRow newRow = dt.NewRow();
9
dt.Rows.Add(newRow);
}
5.总结
摸态窗口在IE7中不能用了
修改背景颜色与添加交互效果
RowCreated方法添加列元素属性(对行处理时)
RowDataBound(对数据处理时)
1.1.添加鼠标移动事件
GridViewClient.aspx
GridViewClient.aspx.cs
//后台代码
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");
}
}
1.2.添加单击事件
GetInfo.htm 打开窗口
GridViewClientClick.aspx 双击事件返回值/按下键时
GridViewClientClick.aspx.cs
1.3.添加键盘事件
同上
1.4.添加修改背景颜色事件
ChangeBackColor.aspx
ChangeBackColor.aspx.cs
1 //后台代码
2 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
3 {
4 if (e.Row.RowType == DataControlRowType.DataRow)
5 {
6 if (e.Row.Cells[8].Text == "USA")
7 {
8 //e.Row.BackColor = System.Drawing.Color.Red;
9 e.Row.Cells[8].BackColor = System.Drawing.Color.Red;
}
}
}
2.使用模版列
2.1.添加全选效果
ChoseAll.aspx
ChoseAll.aspx.cs
1
2 //前台代码
3 <asp:TemplateField HeaderText="选取">
4 <HeaderTemplate>
5 选择全部<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged" />
6 </HeaderTemplate>
7 <ItemTemplate>
8 <asp:CheckBox ID="CheckBox1" runat="server" />
9 </ItemTemplate>
</asp:TemplateField>
//后台代码
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;
}
}
}
2.2.添加删除确认效果
GridViewDelete.aspx
GridViewDelete.aspx.cs
//前台代码
<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>
2.3.添加图片显示效果
GetImage.ashx(一般处理程序)
GridViewImage.aspx
GridViewImage.aspx.cs
1 //前台代码
2 <asp:TemplateField HeaderText="Photo">
3 <ItemTemplate>
4 <img src='GetImage.ashx?eid=<%#Eval("EmployeeID")%>' />
5 </ItemTemplate>
6 </asp:TemplateField>
7
8
9 //后台代码
<%@ WebHandler Language="C#" Class="GetImage" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data.Sql;
public class GetImage : IHttpHandler {
public void ProcessRequest (HttpContext context) {
using (SqlConnection sc = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
sc.Open();
String txtsql = "select photo from employees where employeeid=" + context.Request.QueryString["eid"];
SqlCommand scd = new SqlCommand(txtsql, sc);
context.Response.Clear();
context.Response.ContentType = "image/bmp";
byte[] bitmapBytes = (byte[])scd.ExecuteScalar();
int length = bitmapBytes.Length;
context.Response.OutputStream.Write(bitmapBytes, 78, bitmapBytes.Length - 78);
context.Response.Cache.SetCacheability(HttpCacheability.Public);
}
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
3.GridView的导出数据(Excel)
ExportToExcel.aspx
ExportToExcel.aspx.cs
1 //前台代码
2 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />
3
4 //后台代码
5 protected void Button1_Click(object sender, EventArgs e)
6 {
7 Response.Clear();
8 Response.Buffer = true;
9 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();
}
public override void VerifyRenderingInServerForm( Control control )
{ }
4.不使用数据源控件的GridView
分页等
空行处理
1 private void AddDummyData(DataSet ds)
2 {
3
4 // Add a dummy row
5
6 DataTable dt = ds.Tables[0];
7
8 DataRow newRow = dt.NewRow();
9
dt.Rows.Add(newRow);
}
5.总结
摸态窗口在IE7中不能用了
相关文章推荐
- (笔记)深入挖掘ASP.NET 2.0系列课程
- 【PHP TO ASP.NET 2.0开发系列课程(1):PHP与ASP.NET2.0之比较分析】笔记
- ASP.NET AJAX深入浅出系列课程(2):UpdatePanel的使用(上) 笔记 备忘
- ASP.NET 2.0中使用webpart系列控件
- 使用ASP.NET 2.0中的GridView控件
- ASP.NET 2.0入门与提高系列课程(视频课程讲师:徐栋)
- ASP.NET 2.0中GridView与DetailsView的联合使用
- 使用ASP.NET 2.0 GridView轻松操作数据
- ASP.NET Core 2.0系列学习笔记-ADO.NET-MSSQLServerHelper
- Scott Mitchell 的ASP.NET 2.0数据教程之十二:在GridView控件中使用TemplateField
- 使用ASP.NET 2.0 GridView轻松操作数据
- 小tips:asp.net 2.0中在gridview中使用DataFromatString
- ASP.NET Core 2.0系列学习笔记-启动类Startup
- ASP.NET 2.0 中的数据操作:: 使用 GridView 和DetailView实现的主/从报表
- 在ASP.NET 2.0中操作数据之十:使用 GridView和DetailView实现的主/从报表
- Scott Mitchell 的ASP.NET 2.0数据教程之十:使用GridView 和DetailView实现的主/从报表
- ASP.NET 2.0中使用Gridview控件的高级技巧
- ASP.NET 2.0中使用webpart系列控件
- 小tips:asp.net 2.0中在gridview中使用DataFromatString
- ASP.NET 2.0中使用webpart系列控件