熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

編程ASP.NET的幾個技巧

2013-11-13 10:03:33  來源: .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文件中定義默認的頁面繼承類型


From:http://tw.wingwit.com/Article/program/net/201311/12428.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.