Several Skills in network programming ASP. NET

Source: Internet
Author: User
1. display images generated by code

Put the generated image code in An ASPX page and write the image to the output stream in the pageload event:

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 ();

}

To reference an image, set the image URL to the ASPX page of the generated image:

Imagecode. imageurl = "codeimage. aspx? Code = "+ code;

2. Use System. Web. httpcontext. Current to implement common page methods, such:

Public class webapp {

Public static void showmessage (string message ){

Httpcontext. Current. response. Write ("<script language = JavaScript> alert ('" + message + "') </SCRIPT> ");

}

Public static string currentuser {

Get {

Return httpcontext. Current. session ["userid"] + "";

}

}

}

3. Compile the Javascript script file into an embedded resource in a custom web control, read the script from the resource, and register it.

Public class res {

Public static streamreader getstream (type, string name ){

// Assembly = assembly. getassembly (type );

Assembly = type. assembly;

Stream stream = assembly. getmanifestresourcestream (type, name );

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 (! This. Page. isclientscriptblockregistered (scriptfile )){

Streamreader reader = res. getstream (this. GetType (), scriptfile );

Using (Reader ){

String script

= "<Script language =/" javascript/"type =/" text/JavaScript/">/R/n <! --/R/N"

+ Reader. readtoend ()

+ "/R/N // -->/R/n </SCRIPT> ";

This. Page. registerclientscriptblock (scriptfile, script );

}

}

}

}

[Defaultproperty ("text "),

Toolboxdata ("<{0}: showdialoglistbox runat = Server> </{0}: showdialoglistbox>")]

Public class showdialoglistbox: scriptcontrol {

......

Protected override void oninit (eventargs e ){

This. registerscript ("endelistbox. js ");

}

......

}

4. Define the default page inheritance type in the web. config file.

<Pages pagebasetype = "msdn. Page"/>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.