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      }
posted @ 2008-05-22 14:33  acme  阅读(610)  评论(0编辑  收藏  举报