HttpHandler应用一例

目的:在DridView中的每一行显示任务进度条!
HttpHandler实现如下:

namespace THOA.Web.Utility
{
classHandlerTaskbar:IHttpHandler
{
IHttpHandler成员#regionIHttpHandler成员

publicboolIsReusable
{
get{returnfalse;}
}


publicvoidProcessRequest(HttpContextcontext)
{
Tasktask
=newTask();
ICtrlctrTask
=ICtrl.FactoryCreate(CtrType.CtrTask);
task.TaskID
=Convert.ToInt32(context.Request.QueryString["task_id"]);

Bitmapmap
=newBitmap(m_Width,m_Height);
Graphicsg
=Graphics.FromImage(map);

//--此处进度条绘图代码略

context.Response.Clear();
map.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
//写回客户端输出流
g.Dispose();
map.Dispose();
context.Response.End();
}

}

}

请注意回写代码!

编译为dll,在Web app中引用生成的dll,假设名称为THOA.Web.Utility.dll,在web.config文件添加

< httpHandlers >
< addverb = " GET " path = " Taskbar.aspx " type = " THOA.Web.Utility.HandlerTaskbar,THOA.Web.Utility " />
</ httpHandlers >

Web Form代码如下:
< asp:GridViewID = " gvTask " runat = " server " >
< Columns >
< asp:TemplateFieldHeaderText = " 进度 " >
< HeaderStyleHorizontalAlign = " center " Width = " 45px " />
< ItemTemplate >
< asp:Imagerunat = " server " ID = " imgBar " />
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
</ asp:GridView >

Web Form cs文件代码:

void gridview_RowDataBound( object sender,GridViewRowEventArgse)
{
ImageimgBar
=e.Row.FindControl("imgBar")asImage;
if(imgBar!=null)
imgBar.ImageUrl
="Taskbar.aspx?task_id="+e.Row.Cells[0].Text;
}

到此处大功告成,看看实际效果
HttpHandler应用一例

另外,建议将出现频率高的进度缓存,以提高速度!

你可能感兴趣的:(handler)