asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

编程ASP.NET的几个技巧


发布日期:2024年08月02日
 
编程ASP.NET的几个技巧

把生成图片的代码放在一个aspx页面中在PageLoad事件中把图片写入输出流

private void Page_Load(object sender SystemEventArgs e) {

string code = RequestParams[code];

Bitmap image = DrawImage(code);

ResponseContentType = image/gif;

imageSave(ResponseOutputStreamSystemDrawingImagingImageFormatGif);

ResponseEnd();

}

在需要引用图片的地方设定图片URL为生成图片的aspx页面

ImageCodeImageUrl = CodeImageaspx?code=+code;

使用SystemWebHttpContextCurrent来实现一些页面中常用的方法比如

public class WebApp{

public static void ShowMessage(string message){

HttpContextCurrentResponseWrite (<script language=javascript>alert(+message+)</script>);

}

public static string CurrentUser{

get{

return HttpContextCurrentSession[UserID]+;

}

}

}

在自定义的Web控件中把Javascript脚本文件编译为内嵌的资源然后从资源中读取脚本并注册

public class Res {

public static StreamReader GetStream(Type typestring name){

//Assembly assembly = AssemblyGetAssembly(type);

Assembly assembly = typeAssembly;

Stream stream = assemblyGetManifestResourceStream(typename);

return new StreamReader(stream);

}

}

public class ScriptControl : Control {

/// <summary>

/// Register Client Script Block

/// </summary>

/// <param name=control>custom web control</param>

/// <param name=scriptfile>resource script file name</param>

public void RegisterScript(string scriptfile){

if (!thisPageIsClientScriptBlockRegistered(scriptfile)) {

StreamReader reader = ResGetStream(thisGetType()scriptfile);

using(reader){

string script

= <script language=\javascript\ type=\text/javascript\> <!

+ readerReadToEnd()

+ //> </script>;

thisPageRegisterClientScriptBlock(scriptfile script);

}

}

}

}

[DefaultProperty(Text)

ToolboxData(<{}:ShowDialogListBox runat=server></{}:ShowDialogListBox>)]

public class ShowDialogListBox : ScriptControl {

protected override void OnInit(EventArgs e) {

thisRegisterScript(EnDeListBoxjs);

}

}

在nfig文件中定义默认的页面继承类型

               

上一篇:ASP.NET CheckBoxList组件编程浅析

下一篇:ASP.NET MVC自定义过滤属性实现log功能