A summary of the usage of ASPX and ASCX,ASHX

Source: Internet
Author: User
Tags eval

  This article is mainly about the use of ASPX and ascx,ashx a detailed summary of the introduction, the need for friends can come to the reference, I hope to help you.

To do asp.net development of the aspx,.ascx and ashx are not unfamiliar. About them, there are many articles on the Internet. "Paper on the end feel shallow, know this matter to preach," the following summed up to make a note.   1,. aspx Web Forms design page. Web Forms pages are made up of two parts: visual elements (HTML, server controls, and static text) and programming logic for the page (Design view and code view in VS can see their corresponding files separately). Vs the two components are stored separately in a separate file. Visual elements are created in the. aspx file.   2,. ascx asp.net user controls are developed as a Web page that encapsulates specific features and behaviors, both of which are used on various pages of a Web application. A user control contains a combination of HTML, code, and other Web or user controls, and is saved in its own file format on the Web server with an extension of *.ascx. The default configuration in ASP.net does not allow Web clients to access these files through URLs, but other pages of the site can integrate the functionality contained in those files.   3, .ashx  front two are too familiar with, this is to talk about the point.   (1) Use example. ashx files are primarily used to write Web handler. Using. ASHX allows you to focus on programming without having to manage related web technologies. We know of. aspx is to be parsed by the HTML control tree,. aspx contains all the HTML is actually a class, all HTML is a class inside the member, this process in. ashx is not needed. Ashx must contain the IsReusable attribute (this property represents whether it is reusable, usually true), and the IRequiresSessionState interface must be implemented to use session in the Ashx file. A simple implementation to modify the login user Password example:   code is as follows: using System; Using System.Collections.Generic; Using System.Linq; Using System.Web; Using System.Web.Security; Using System.Web.UI; Using System.Web.UI.WebControls; Using System.Web.UI.WebControls.WebParts; Using System.Web.UI.HtmlControls; Using SYSTEM.WEB.SEssionstate;   namespace Test {      public class Handlertest:ihttphandler, IRequiresSessionState     {          public void ProcessRequest (HttpContext context)         {            context. Response.clearcontent ();             context. Response.ContentType = "Text/plain";             context. Response.Cache.SetCacheability (Httpcacheability.nocache); No cache               string action = context. Request.params["Action"]; External request             if (action = "modifypwd")/user Change password           &NB Sp {                String oldpwd = context. request.params["PWD"];                  //in ASHX file use session must implement IRequiresSessionState interface   &NBS P            //session["Logeduser"] is a login user's session, username and password are test                 if (Oldpwd.toupper ()!= (context. session["Logeduser"]) as Customer. Password.toupper ())//user input old password and current login user is not the same                 {      & nbsp             context. Response.Write ("Old password input error!");                                 Else &NB Sp               {                    C Ontext. Response.Write ("Old password input correct!");                            }                 context. Response.End ();        }           public bool isreusable         {&nbs P           Get             {              &NB Sp return true; (JS and page sections)            }        }    }}     client   Code as follows: <%@ Page language= "C #" autoeventwireup= "true" codebehind= "ASHXTest.aspx.cs" inherits= "Ashxtest"%> & nbsp <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <html xmlns=" http://www.w3.org/1999/xhtml "> <head runat=" Server ">     <title>mytest </title>     <script type= "Text/javascript" >         function $ (s) {if (documen T.getelementbyid) {return eval (' document.getElementById + S + ') ');} else {return eval (' document.all. ' + s);}}           function createxmlhttp () {            var xmlHttp = Fals E   &nbsp         var arrsignatures = [MSXML2. xmlhttp.5.0 "," MSXML2. xmlhttp.4.0 ",                           MSXML2. xmlhttp.3.0 "," MSXML2. XMLHTTP "                         " Microsoft.XMLHTTP "];             for (var i = 0; i < arrsignatures.length; i++) {                try {                    xmlHttp = new ACTI Vexobject (Arrsignatures[i]);                     return xmlHttp;                                 catch (o Error) {                    XmlHttp = False//ignore     &NB Sp          }     &nbsp      }            //throw new Error ("MSXML isn't installed on your system" .");               if (!xmlhttp && typeof xmlhttprequest!= ' undefined ') {  &N Bsp             xmlHttp = new XMLHttpRequest ();                         return xmlHttp;        }           var xmlreq = Createxmlhttp ();          //Send AJAX processing request (here simply verify the validity of old password)         function validateoldpwd (otxt) {            var url = "/handlertest.ashx?action=modifypwd&pwd=" + Escape (otxt.value); . ashx files             Xmlreq.open ("Get", url, True);             Xmlreq.setrequestheader ("If-modified-since", "0");             XMLREQ.ONreadystatechange = CallBack;             xmlreq.send (URL); Send text        }           function CallBack () {   ,         if (xmlreq.readystate = 4) {                if (xmlreq.status = = {       ,             alert (xmlreq.responsetext);//Receive text   &N Bsp                             else if (xmlreq.status = 404) {                    alert ("Requested URL is not found.") &NB Sp              } else if (Xmlreq.status = 403) {          &NB Sp         alert ("Access denied.");                } else                    alert ("status is" + Xmlreq.status);                     {      </script>   < /head> <body>     <form id= "Form1" runat= "Server" >     <div>         <input id= "txtoldpwd" type= "text" onblur= "validateoldpwd (This)"/>     </div>     </form> </body> </html>     Analysis: A, previously we are usually through a simple ASPX file to achieve the function, in fact, through the ashx can also. I have written an Ajax: Data transmission method Introduction, by contrast, we found that aspx to the front and back display and processing logic separate, so it became two files, in fact, in the final compilation, ASPX and CS are still compiled into the same class. This is the middle of the design of some of the logic of HTML processing; unlike ASHX, it is simply a simple web Direct return of HTTP requests the result you want to return. The process of processing HTML less than ASPX (but ashx can also handle some of the logic of HTML, but usually not). Theoretically ashx faster than ASPX.   B, or in the same old text, we know several ways of data transmission, in fact, ASHX can be implemented (modify ASHX file context. Response.ContentType can), here no longer repeat.   (2) ashx is particularly suitable for generating dynamic images, generating dynamic text (plain text, json,xml,javascript, etc.).     (3). ashx file has a disadvantage: it handles postback events for controls very cumbersome. Processing the postback of the data usually requires some. aspx page functionality, and only manually handle these features (rather than building an ASPX file to placeRationale). So, generally use. ashx output some items that do not require postback processing.     4, summary aspx-->p (Page) ascx-->c (Control) ashx-->h (HttpHandler)   when the browser accesses the Web server, The last we received was HTML text. The browser interprets the tags through the rendering engine, showing the visible effect on the screen. And ASP.net is just one of the platform technologies we use to "disguise" the HTML, which is to increase productivity, and its technical terms are more It's essentially HTML--if you don't use HTML and browsers (and of course, JS) to achieve dynamic page effects without using dynamic page technology, Then you will find that the effect has a considerable amount of code. So the bottom of web development is a bunch of HTML tags, whether asp.net or JSP is a way to HTML packaging, is the product of HTML.    

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.