深入挖掘ASP.NET 2.0系列课程(1):随心所欲的使用GridView(笔记)

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;
10             }
11         }
12     }
13 


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>
10     </asp:TemplateField>
11 
12    
13     //后台代码
14     protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
15     {
16         int i;
17         if (((CheckBox)sender).Checked)
18         {
19             for (i = 0; i < GridView1.Rows.Count; i++)
20             {
21             ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked = true;
22             }
23         }
24         else
25         {
26             for (i = 0; i < GridView1.Rows.Count; i++)
27             {
28             ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked =false;
29             }
30         }
31     }


2.2.添加删除确认效果
    GridViewDelete.aspx
    GridViewDelete.aspx.cs

1     //前台代码
2     <asp:TemplateField HeaderText="删除" ShowHeader="False">
3         <ItemTemplate>
4         <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
5             OnClientClick='return confirm("确认要删除吗?")' Text="删除"></asp:LinkButton>
6         </ItemTemplate>
7     </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     //后台代码
10     <%@ WebHandler Language="C#" Class="GetImage" %>
11 
12     using System;
13     using System.Web;
14     using System.Data.SqlClient;
15     using System.Data.Sql;
16 
17     public class GetImage : IHttpHandler {
18          public void ProcessRequest (HttpContext context) {
19 
20 
21          using (SqlConnection sc = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
22          {
23              sc.Open();
24              String txtsql = "select photo from employees where employeeid=" + context.Request.QueryString["eid"];
25              SqlCommand scd = new SqlCommand(txtsql, sc);
26 
27              context.Response.Clear();
28              context.Response.ContentType = "image/bmp";
29 
30              byte[] bitmapBytes = (byte[])scd.ExecuteScalar();
31              int length = bitmapBytes.Length;
32 
33              context.Response.OutputStream.Write(bitmapBytes, 78, bitmapBytes.Length - 78);
34              context.Response.Cache.SetCacheability(HttpCacheability.Public);
35          }
36 
37          context.Response.End();
38         }
39         public bool IsReusable
40         {
41         get
42         {
43             return false;
44         }
45         }
46     }



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";
10         Response.AppendHeader("Content-Disposition""attachment;filename=FileName.xls");
11         // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
12         Response.ContentEncoding = System.Text.Encoding.UTF7;
13         Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
14         System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
15         System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
16         this.GridView1.RenderControl(oHtmlTextWriter);
17         Response.Output.Write(oStringWriter.ToString());
18         Response.Flush();
19         Response.End();
20 
21     }
22     public override void VerifyRenderingInServerForm( Control control )
23     { }



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 
10         dt.Rows.Add(newRow);
11 
12     }







5.总结

    摸态窗口在IE7中不能用了




posted on 2007-09-29 22:23  天涯人  阅读(762)  评论(0编辑  收藏  举报

导航