开发者

Execute server side code without full postback

开发者 https://www.devze.com 2023-01-02 14:25 出处:网络
When a user clicks a button I need to create a .bmp file on the server. After th开发者_如何学Ce .bmp file is created I will load it into the html page. Would Ajax be the best way to accomplish this?Ye

When a user clicks a button I need to create a .bmp file on the server. After th开发者_如何学Ce .bmp file is created I will load it into the html page. Would Ajax be the best way to accomplish this?


Yes AJAX is the best way. It can be done through ASP.NET's AJAX mechanisms, jQuery itself or another of your choice.

If you're just generating an image for return to the browser then instead of invoking an ASP.NET page (.aspx) lifecycle for it, use the lighter HTTP handler (.ashx). Here's an example on the server-side.

(The HTTP Handlers work by inheriting your class from something like DefaultHttpHandler or implementing IHttpHandler instead of the Page class.)

Additional Samples and notes about AJAX communication:

  • If you choose an ASPX Page to generate your image, then use WebMethod attributes on page methods to facilitate AJAX communication with them; or an explicit .ASMX Web Service to do the same using the WebMethod attribute.
    or,
  • ASP.NET AJAX and HTTP Handlers
  • You can pick up many tidbits from Stackoverflow for example, you don't need an ASPX or ASHX page, just a raw class that inherits IHttpHandler and web.config httphandler settings in place to make it work.

To debug this kind of stuff it's very useful to have an HTTP Monitoring utility like Fiddler to watch the out-of-band HTTP requests and responses.


Short answer: Yes.

ajax would be the best way to do this. Look at the jquery docs to start with. Its not really that simple of a thing to do and doesn't lend itself to posting a snippet to illustrate. You're going to have to get cozy with several possibly new concepts here.

http://docs.jquery.com/Main_Page

Here's the direct link to jQuery's ajax documentation.

http://api.jquery.com/category/ajax/

If you've never done jQuery, its kinda weird, but definitely worth taking the time to get used to.


One side-note. Do you really need to output a BMP file to display it in the HTML?

BMPs are quite heavy and uncompressed, if you must store it in that format serer-side, maybe you can also save a JPG "thumbnail" and output that. It will be much faster, whenever you decide to use AJAX or normal postbacks.

As everyone else said, AJAX is the best approach because response will be much smaller (just the contents) than returning the full page.

Also for debugging, apart from Fiddler I like to use Firefox with Firebug, it is an excellent web development tool.


if you don't want a full post back, yes.


if you want to provide an useful feature on this then you should go Ajax if not then a postback is OK


You don't have to use Ajax. You can set your page to AutoEventWireUp="false" and handle all events by hand. Circumvent the Page_Load event on the Button click and voila.

0

精彩评论

暂无评论...
验证码 换一张
取 消