Initial knowledge of ASP---general processing program

Source: Internet
Author: User

Source of the problem:

Today in a small demo, using jquery to implement cascading drop-down boxes, the process of knocking found that no matter how compared to the source code can not display the desired function.

This is to think of the original is not written backstage code, asked a classmate, he told me to use the general process of ASP.

I was a rookie just started to learn, because I have not been exposed to the general processing procedures.

what is a generic handler:

Generic handler: is a special class that implements the System.Web.IHttpHandler interface. No matter what, a class that implements the IHttpHandler interface. Is the premise of the target program as an external request.

(Any class that does not implement this interface cannot be requested by the browser.) It is invoked and started by the server that supports ASP. A HttpHandler program handles requests for access to one or a set of URL addresses that it corresponds to, and receives incoming request information from the client (Request messages) and generates response content (response messages).

Advantages: Light weight, high efficiency

Creating a generic handler will generate a file with two suffix names. ashx and. Ashx.cs. There is only one instruction set in ashx. No matter what other code, Ashx.cs is the page processing code. Suppose there is a lot of HTML. The way to stitch strings or read templates through Ashx is complete.

The. Ashx.cs the corresponding content, for example, with the following code:

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Web;    Namespace Web  {      //<summary>///Handler//</summary> public class Handler: IHttpHandler      {public          void ProcessRequest (HttpContext context)          {              context. Response.ContentType = "Text/plain";  Represents the output result type            context. Response.Write ("Hello World");  Output        } public            bool isreusable          {              get              {                  return false;  }}}  

(1) HttpContext: Request context object, including: Request Message Object (HttpRequest), Response message object (HttpResponse), Server helper Class (server), session, etc.

(2) After receiving a request, the framework creates an object of the corresponding class of the request path through reflection technology and casts it into the IHttpHandler interface object (HttpHandler implements the IHttpHandler, after which the conversion is equivalent to strongly converting the subclass to the parent class). Unified call ProcessRequest method processing.


(3) The IsReusable property is used to indicate whether the handler can be pooled for reuse when the IHttpHandlerFactory object is created IHttpHandler.

(4) The Contenttyp property is the general handler output response content, the content of the output may be pictures, HTML and other pages, the browser based on the response to the style content does not know what is received, It is necessary to infer the contenttype of the response message head to determine the format of the report style content as the data to display. (image/jpeg----Indicates that the output is a picture, Text/plain--------represents the output text)

As you can see from the code above, the generic handler is actually a handler class that implements the IHttpHandler interface, which is directly mapped to the system configuration file by ASP. NET to the request of the ashx extension. Such We don't need to configure it in the config file.

details of Use:

The HttpHandler program is able to complete most of the tasks that a common class program can complete:

1 . get the data and URL parameters submitted by the client through the form form of HTML

2. Create a response message content to the client

3. Access to server-side file systems

4. Connect to the database and develop database- based applications

5. Calling other classes

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Web;    Namespace Web  {      //<summary>///Handler//</summary> public class Handler: IHttpHandler      {public          void ProcessRequest (HttpContext context)          {              context. Response.ContentType = "Text/plain";  Represents the output result type            context. Response.Write ("Hello World");  Output        } public            bool isreusable          {              get              {                  return false;  }}}  

Detailed application:

The following is the use of jquery to implement cascading drop-down effects. A generic handler is required to obtain the code of the general handler for the car type.

public class Handler1:ihttphandler    {public        void ProcessRequest (HttpContext context)        {            context. Response.ContentType = "Text/plain";            String Strcarname =context. request["Carnamevalue"]. ToString ();             SqlConnection conn = new SqlConnection ("server=.; database=car;uid=sa;pwd=123456; ");   Defines the connection string            Conn. Open ();     Open Connection            SqlDataAdapter SDR = new SqlDataAdapter ();            Sdr. SelectCommand =new SqlCommand ("Selectid,cartype from T_cartype where carnameid= '" + strcarname + "'", conn);             DataTable dt = new DataTable ();             Sdr. Fill (DT);            String str =datatabletojson ("1", DT);            Context. Response.Write (str);        }         public bool IsReusable        {            get            {                return false;        }}}

Summary:

in fact, the theoretical knowledge of the good will not be used and will only stay on the theoretical level. And programming is such a strange thing no matter how specific the other people speak, they can not be compared with their own practice.

The text mentioned in the demo source code, the article will be accompanied by a download link.

Demo Source code download: http://download.csdn.net/detail/senior_lee/7697243

Initial knowledge of ASP---general processing program

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.