在前两篇文章中,我们已经创建了一个HTTP处理程序,并实现了页面到HTTP处理程序之间的字符串参数的传递。下面让我们来作个比较实用的东西--一个类似于 Google Analytics 的GridView,实现后的效果见下图。

实现方法
Step1:实现动态生成图片。首先,为类库 mylib.system.web 添加对 System.Drawing 的引用。然后,编写如下代码根据传给 MyHandler.jxd 的参数动态生成一个图片,并写到 Response 的 OutputsStream 里面。MyHandler.cs的代码如下
MyHandler.cs
MyHandler.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.IO;
5
using System.Drawing;
6
using System.Drawing.Imaging;
7
namespace mylib.system.web
8
{
9
public class MyHandler : System.Web.IHttpHandler
10
{
11
#region IHttpHandler 成员
12
public bool IsReusable
13
{
14
get { return false; }
15
}
16
public void ProcessRequest(System.Web.HttpContext context)
17
{
18
// 获取参数n,并将其转换为Int型。
19
string n = context.Request.QueryString["n"];
20
int count = 0;
21
int.TryParse(n, out count);
22
// 使用GDI+函数画一个长方形。
23
Bitmap bmp = new Bitmap(count, 20);
24
Graphics graphic = Graphics.FromImage(bmp);
25
MemoryStream stream = new MemoryStream();
26
SolidBrush brush = new SolidBrush(Color.SteelBlue);
27
try
28
{
29
graphic.FillRectangle(brush, 0, 0, count, 20);
30
// 将画好的图形保存到内存中。
31
bmp.Save(stream, ImageFormat.Png);
32
// 将内存中的图片发送到客户端
33
context.Response.ContentType = "image/png";
34
context.Response.OutputStream.Write(stream.ToArray(),0,(int)stream.Length);
35
}
36
finally
37
{
38
// 释放资源
39
stream.Close();
40
brush.Dispose();
41
brush = null;
42
graphic.Dispose();
43
graphic = null;
44
}
45
}
46
#endregion
47
}
48
}
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.IO;5
using System.Drawing;6
using System.Drawing.Imaging;7
namespace mylib.system.web8
{9
public class MyHandler : System.Web.IHttpHandler10
{11
#region IHttpHandler 成员12
public bool IsReusable13
{14
get { return false; }15
}16
public void ProcessRequest(System.Web.HttpContext context)17
{18
// 获取参数n,并将其转换为Int型。19
string n = context.Request.QueryString["n"];20
int count = 0;21
int.TryParse(n, out count);22
// 使用GDI+函数画一个长方形。23
Bitmap bmp = new Bitmap(count, 20);24
Graphics graphic = Graphics.FromImage(bmp);25
MemoryStream stream = new MemoryStream();26
SolidBrush brush = new SolidBrush(Color.SteelBlue);27
try28
{ 29
graphic.FillRectangle(brush, 0, 0, count, 20);30
// 将画好的图形保存到内存中。31
bmp.Save(stream, ImageFormat.Png);32
// 将内存中的图片发送到客户端33
context.Response.ContentType = "image/png";34
context.Response.OutputStream.Write(stream.ToArray(),0,(int)stream.Length);35
}36
finally37
{38
// 释放资源39
stream.Close();40
brush.Dispose();41
brush = null;42
graphic.Dispose();43
graphic = null;44
}45
}46
#endregion47
}48
}Step2:在Web程序的 Default.aspx 中放置一个 GridView 控件。并为其添加两个模板列。一列用于显示“网址”;另一列中放一个 Image 控件和一个 Label 控件,用于显示访问量。Label 控件直接绑定到数组下标为1的字段,就不用多说了。Image 控件通过“ImageUrl= '<%# "~/MyHandler.jxd?n=" + Eval("[1]") %>'”这样的绑定表达式向 MyHandler.jxd 发送带参数的请求,Myhandler.cs 将根据参数 n 的值生成相应大小的图片并发送给 Image 控件。
Default.aspx
Step3:作一些测试数据,并绑定到 GridView 中。
Default.aspx.cs
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml" >
4
<head runat="server">
5
<title>无标题页</title>
6
</head>
7
<body>
8
<form id="form1" runat="server">
9
<div>
10
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" GridLines="Vertical" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px">
11
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
12
<Columns>
13
<asp:TemplateField HeaderText="网址">
14
<ItemTemplate>
15
<asp:Label ID="Label1" runat="server" Text='<%# Eval("[0]") %>'></asp:Label>
16
</ItemTemplate>
17
</asp:TemplateField>
18
<asp:TemplateField HeaderText="访问量">
19
<ItemTemplate>
20
<asp:Image ID="Image1" runat="server" ImageUrl= '<%# "~/MyHandler.jxd?n=" + Eval("[1]") %>' />
21
<asp:Label ID="Label1" runat="server" Text='<%# Eval("[1]") %>'></asp:Label>
22
</ItemTemplate>
23
</asp:TemplateField>
24
</Columns>
25
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
26
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
27
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
28
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
29
<AlternatingRowStyle BackColor="#DCDCDC" />
30
</asp:GridView>
31
32
</div>
33
</form>
34
</body>
35
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">3
<html xmlns="http://www.w3.org/1999/xhtml" >4
<head runat="server">5
<title>无标题页</title>6
</head>7
<body>8
<form id="form1" runat="server">9
<div>10
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" GridLines="Vertical" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px">11
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />12
<Columns>13
<asp:TemplateField HeaderText="网址">14
<ItemTemplate>15
<asp:Label ID="Label1" runat="server" Text='<%# Eval("[0]") %>'></asp:Label>16
</ItemTemplate>17
</asp:TemplateField>18
<asp:TemplateField HeaderText="访问量">19
<ItemTemplate>20
<asp:Image ID="Image1" runat="server" ImageUrl= '<%# "~/MyHandler.jxd?n=" + Eval("[1]") %>' />21
<asp:Label ID="Label1" runat="server" Text='<%# Eval("[1]") %>'></asp:Label>22
</ItemTemplate>23
</asp:TemplateField>24
</Columns>25
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />26
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />27
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />28
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />29
<AlternatingRowStyle BackColor="#DCDCDC" />30
</asp:GridView>31
32
</div>33
</form>34
</body>35
</html>Step3:作一些测试数据,并绑定到 GridView 中。
Default.aspx.cs
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Collections.Generic;
11
public partial class _Default : System.Web.UI.Page
12
{
13
protected void Page_Load(object sender, EventArgs e)
14
{
15
List<string[]> data = new List<string[]>();
16
data.Add(new string[] { "1. 使用分治法实现的全排列算法", "50" });
17
data.Add(new string[] { "2. 让Ruby的数组支持任意起始下标", "16" });
18
data.Add(new string[] { "3. 输出二叉树的方法", "14" });
19
data.Add(new string[] { "4. 让ruby以矩阵的样式输出二维数组", "9" });
20
data.Add(new string[] { "5. 算法分析涉及到的一些函数图像", "8" });
21
GridView1.DataSource = data;
22
GridView1.DataBind();
23
}
24
}
using System;2
using System.Data;3
using System.Configuration;4
using System.Web;5
using System.Web.Security;6
using System.Web.UI;7
using System.Web.UI.WebControls;8
using System.Web.UI.WebControls.WebParts;9
using System.Web.UI.HtmlControls;10
using System.Collections.Generic;11
public partial class _Default : System.Web.UI.Page 12
{13
protected void Page_Load(object sender, EventArgs e)14
{15
List<string[]> data = new List<string[]>();16
data.Add(new string[] { "1. 使用分治法实现的全排列算法", "50" });17
data.Add(new string[] { "2. 让Ruby的数组支持任意起始下标", "16" });18
data.Add(new string[] { "3. 输出二叉树的方法", "14" });19
data.Add(new string[] { "4. 让ruby以矩阵的样式输出二维数组", "9" });20
data.Add(new string[] { "5. 算法分析涉及到的一些函数图像", "8" });21
GridView1.DataSource = data;22
GridView1.DataBind();23
}24
}本篇到此结束,下一篇将介绍让Web程序与HTTP处理程序共享Session的方法。

