ASP.NET的网络编程中几个技巧
1.显示通过代码生成的图片
把生成图片的代码放在一个aspx页面中,在pageload事件中把图片写入输出流:
private void page_load(object sender, system.eventargs e) {
string code = request.params["code"];
bitmap image = drawimage(code);
response.contenttype = "image/gif";
image.save(response.outputstream,system.drawing.imaging.imageformat.gif);
response.end();
}
在需要引用图片的地方,设定图片url为生成图片的aspx页面:
imagecode.imageurl = "codeimage.aspx?code="+code;
2.使用system.web.httpcontext.current来实现一些页面中常用的方法,比如:
public class webapp{
public static void showmessage(string message){
httpcontext.current.response.write ("<script language=javascript>alert('"+message+"')</script>");
}
public static string currentuser{
get{[iocblog.net 来源]
return httpcontext.current.session["userid"]+"";
}
}
}
3.在自定义的web控件中,把javascript脚本文件编译为内嵌的资源,然后从资源中读取脚本并注册。
public class res {
public static streamreader getstream(type type,string name){
//assembly assembly = assembly.getassembly(type);
assembly assembly = type.assembly;
stream stream = assembly.getmanifestresourcestream(type,name);
return new streamreader(stream);
}
}
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。