Lightweight ASP.net Ajax Solutions Detail _ Practical Tips

Source: Internet
Author: User

This example describes a lightweight asp.net Ajax solution. Share to everyone for your reference, specific as follows:

Study with Shotdog to explore Ajax solutions in the next asp.net, in addition to the official huge asp.net ajax. Our idea is to output it in different server-side ways, and then use the AJAX implementation of jquery on the page to invoke several solutions on the server side:

1, using the general WebForm, in the page with jquery Ajax call, and then from the obtained HTML data to get the contents of <body>, write Dom

Advantages: Do not change the existing ASP.net development model, you can use ready-made pages; Ajax gets content that is HTML text and writes directly to the DOM

Disadvantage: Content wasted,<body> is not necessary, and if the use of masterpage then ...

2, the use of general WebForm, but with the Response.Write () control output HTML, in the page with jquery Ajax call, will get the content written to the DOM

Advantages: Content is clean, do not waste; Ajax gets content that is HTML text that can be written directly to the DOM

Disadvantage: Need to construct HTML text in string form on server side, programming inconvenient, not easy to debug and maintain

3, using the general WebForm, with Response.Write () control output JSON data, in the page with jquery Ajax call, the JSON data processed into HTML after the client to write DOM

Benefits: Simply exchanging JSON data, extremely clean, and highly efficient web design concepts

Disadvantage: You need to process the JSON data on the client and create an intrusion on the DOM

4. Use ASMX to encapsulate the Web service, invoke the content of ASMX with jquery Ajax, and write JSON or XML data to the DOM after processing the client into HTML

Advantages: Only Exchange JSON or/xml data, very clean; Web service is easy to cross platform

Disadvantage: You need to process the JSON data on the client and create an intrusion on the DOM

5, use the custom control ascx, and then use the Special WebForm page to do wrapper (packaging) in the page with jquery Ajax call wrapper WebForm, write HTML data to the DOM

Advantage: WebForm is used only as a wrapper, depending on the request parameters can dynamically use the custom control in wrapper; The custom control outputs HTML text that can be written directly to the DOM; Easy programming, VS2008 code-aware support, easy to debug and maintain

Disadvantages: Unlike traditional webform programming ideas, weakening the role of WebForm

These are some of the possible options to discuss-whether it's asp.net webform or asp.net mvc approach.

Another scenario was found last night: using Ashx+jquery. ASHX is a specialized file type for handling HttpHandler, which handles custom HTTP requests and can define how HTTP requests are handled for ASHX at run time in Web.config.

Copy Code code as follows:
<add verb= "*" path= "*.ashx" type= "System.Web.UI.SimpleHandlerFactory" validate= "false"/>

This allows us to use simplehandlerfactory to handle ASHX HTTP requests. Implement the IRequiresSessionState interface in the Ashx class, using the next System.Web.SessionState can use the session, very convenient

Using System.Web.SessionState;
public class Checkcookie:ihttphandler, IRequiresSessionState
{
  ...//todo somthing
}

Example: Verifying the existence of an email using ashx+jquery

. ashx file

<%@ WebHandler language= "C #" class= "CheckUser"%>
using System;
Using System.Web;
public class Checkuser:ihttphandler
{public
  void ProcessRequest (HttpContext context)
  {
      context . Response.ContentType = "Text/plain";
      Context. Response.Write (Userrule.getinstance (). Isuserexist (context. request["Email"]);
  }
  public bool IsReusable
  {get
    {return
      false;
}}}

Html:

<input type= "text" id= "email"/>
<input type= "button" value= "Test" onclick= "Check_email ()"/>

Js:

function Check_email ()
{
  var email = $ ("#email"). attr ("value");
  $.get (".. /ajax/checkuser.ashx ",
  {email:email},
   function (data)
   {
    window.alert (data);}
   );
}

Simple, obviously the efficiency will be relatively high. But simple can only do something simple. If you want to output HTML, it is still not convenient. If you want to output HTML, I still prefer to use ascx processing content, WebForm do packaging so ashx+jquery should be a asp.net lightweight solution

I hope this article will help you to ASP.net program design.

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.