Home Backend Development C#.Net Tutorial Detailed case summary of learning .net and ajax

Detailed case summary of learning .net and ajax

Dec 16, 2016 pm 04:32 PM

First understand what ajax is.

AJAX is not a new programming language, but a technology for creating better, faster and more interactive web applications.
With AJAX, your JavaScript can communicate directly with the server using JavaScript's XMLHttpRequest object. Through this object, your JavaScript can exchange data with the web server without reloading the page.
AJAX uses asynchronous data transfer (HTTP requests) between the browser and the web server, which allows a web page to request a small amount of information from the server instead of the entire page.

When it comes to javascript, everyone will think of browser compatibility issues. In fact, ajax also needs to consider this issue. Because the web javascript classes used by different browsers are different, the effects produced will not be the same. No more nonsense below, just introduce the code directly for your reference.
  {
    var xmlhttp;//Non-IE browser creates XmlHttpRequest object
   if(window.                                                                                                                                                                                                        // IE browser creates
                  try {
                                                                                  try {
                                                                                                                                                     }
                                                                                          // xmlhttp.open("POST", "GetDate.ashx?nd=" + new Date( ), false); //Send a request to a page on the server
                                         {
              xxxxx = xmlhttp.responseText;//Here Here we can return the parameters we passed to our html tags, or other variable processing issues
                                                                                                                                                                                       //responseText                                                                                    
}
                                                                                                                                                                                               It depends, so I don’t need to introduce it here.

How do you feel when you see these codes? For students who have just learned javascript or ajax, it is really difficult to remember these codes. How do you understand these things? I basically have comments above. Everyone can generally understand it. But if each page needs to be partially refreshed, it will feel like it is very troublesome to write such code for each page. jQuery has helped us complete a lot of things, which can reduce the difficulty of developing our projects. The following is the use jQuery code:

$.ajax({

type: "POST",

url: "some.php",

data: "name=John&location=Boston",
success: function(msg){
alert( " Data Saved: " + msg );
}
});Another way

$.post("test.php", { name: "John", time: "2pm" },
function(data){

alert("Data Loaded: " + data);

}); Isn't it very simple? Here we are calling the function he wrote, and we can implement our no-refresh code. Now it feels like it is useless. Isn’t it very simple to refresh the page, but we are just passing small data. It will be very troublesome to extract data from the database. Here is part of my code to let everyone think about it

The following is my implementation of no refresh Comment and display front-end and back-end code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxComment.aspx.cs" Inherits="ajax learning.No refresh comment.AjaxComment1" %>



   
   
   


   


   

                    DeleteMethod="Delete" InsertMethod="Insert"
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
            TypeName="ajax学习.DataSetCommentTableAdapters.T_CommentTableAdapter"
            UpdateMethod="Update">
           
               
           

           
               
               
               
           

           
               
               
               
               
           

       

       

           
               
                   
  • 评论日期:<%#Eval("PostDate")%>,IP:<%#Eval("IP")%>,内容:<%#Eval("Msg")%>

  •            

           

           

   

    value="提交" />
   



Backend code (AjaxComment.ashx)

using System;
using System.Collections.Generic;
using System.Linq;
using System .Web;
using ajax learning.DataSetCommentTableAdapters;
using System.Web.Services;
namespace ajax learning.No refresh comments
{
///


/// Summary description of AjaxComment
/// < /summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AjaxComment: IHttpHandler
{

public void ProcessRequest(Http Context context)
{
                                                                                                                            { ;
                              return false; Is there something wrong with the front-end code? What should I do if I pass data with many fields and attributes? If every data is split()ed like this, it will definitely be tiring if I do a big project. Below, jQuery has done a good thing for us, which is the use of json. Below I introduce my json code for using non-refresh comments

front-end code:




   
   
   


   


       


    后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using ajax学习.DataSetCommentTableAdapters;
    using System.Web.Script.Serialization;
    namespace ajax学习.无刷新分页
    {
        ///


        /// PagedServices 的摘要说明
        ///

        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        public class PagedServices : IHttpHandler
        {

            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                 // context.Response.Write("Hello World");
                string action=context.Request["action"];
                var adapter = new T_CommentTableAdapter();
                if (action == "getpagecount")
                {
                   
                    int count=adapter.SelectCount().Value;
                    int pagecount=count/10;
                    if(count%10!=0)
                    {
                        pagecount++;
                    }
                    context.Response.Write(pagecount);
                }
                else if(action=="getpagedata")
                {
                    string pagenum=context.Request["pagenum"];
                    int iPageNum = Convert.ToInt32(pagenum);
                    var data = adapter.GetPagedData((iPageNum - 1) * 10 + 1, iPageNum * 10);
                    List list=new List ();
                    foreach(var row in data)
                    {
                        list.Add(new Comments() { Id = (int)row.Id, IP = row.IP, Msg = row.Msg, PostDate = row.PostDate, });
                    }
                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    context.Response.Write(jss.Serialize(list));
                } 
            }

            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }

        public class Comments
        {
            public int Id { get; set; }
            public string IP{get;set;}
            public string Msg{get;set;}
            public string PostDate{get;set;}
        }
    }这样我们真的是省了很多力气啊,是不是jQuery很强大啊。。。。。。。。。。。。。

    然后吧,微软感觉我必须封装自己的ajax,这样使得开发者做项目变得容易一些,微软真的是帮我们封装好了一个,但是对于高手来说,大家都不想用,感觉这样的代码太呆板,一点不灵活,我再下面引入代码,供新手参考:(不了解ajax运行原理的新手一样可以使用ajax快速开发新的项目)

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UpdatPanel.aspx.cs" Inherits="ajax学习.UpdatePanel.UpdatPanel" %>

    <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>



       


       


       
       

       

           
            onclick="Button1_Click" />
       

       
      
           
               
                    Text="Ajax无刷新显示当前时间" />
                   

               

           

       

       


    但是微软后来真的是开发一个很好地工具,那就是ajax与WCF的结合,废话少说,引入代码,供大家思考

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WCF_Ajax.aspx.cs" Inherits="ajax学习.WCF之Ajax.WCF_Ajax" %>



       
       
       


       


       

       
           
               
                    //注意路径处理问题,
               

           

           
           
           

       
       



    Backend code: (PersonService.svc)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System. Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.ServiceModel.Web;
    using System.Text;

    namespace ajax learning.WCF’s Ajax
    {
                                                                                                                                           ")]
     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
     public class PersonService
     {
                                                                                                                                                                          ​(The default ResponseFormat is WebMessageFormat.Json)
                                                                                                                                                                                                                                       ​WebOperationContext .Current.outgoingResponse.ContentType = "Text/XML"; Ontract] u Public Person Getperson (int id)
                                                                                                                                            . {
                    public string Name { get; set; }                                             use using using .net using . Chinese website!



    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1670
    14
    PHP Tutorial
    1276
    29
    C# Tutorial
    1256
    24
    The Continued Relevance of C# .NET: A Look at Current Usage The Continued Relevance of C# .NET: A Look at Current Usage Apr 16, 2025 am 12:07 AM

    C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

    From Web to Desktop: The Versatility of C# .NET From Web to Desktop: The Versatility of C# .NET Apr 15, 2025 am 12:07 AM

    C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

    C# as a Versatile .NET Language: Applications and Examples C# as a Versatile .NET Language: Applications and Examples Apr 26, 2025 am 12:26 AM

    C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

    C# .NET and the Future: Adapting to New Technologies C# .NET and the Future: Adapting to New Technologies Apr 14, 2025 am 12:06 AM

    C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

    Deploying C# .NET Applications to Azure/AWS: A Step-by-Step Guide Deploying C# .NET Applications to Azure/AWS: A Step-by-Step Guide Apr 23, 2025 am 12:06 AM

    How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

    C# and the .NET Runtime: How They Work Together C# and the .NET Runtime: How They Work Together Apr 19, 2025 am 12:04 AM

    C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

    C# and .NET: Understanding the Relationship Between the Two C# and .NET: Understanding the Relationship Between the Two Apr 17, 2025 am 12:07 AM

    The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

    C# .NET Development: A Beginner's Guide to Getting Started C# .NET Development: A Beginner's Guide to Getting Started Apr 18, 2025 am 12:17 AM

    To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

    See all articles