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

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    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)

    How to add next-level C compiler How to add next-level C compiler Mar 03, 2025 pm 05:44 PM

    This article explains how to create newline characters in C using the \n escape sequence within printf and puts functions. It details the functionality and provides code examples demonstrating its use for line breaks in output.

    What are the alternatives to NULL in C language What are the alternatives to NULL in C language Mar 03, 2025 pm 05:37 PM

    This article explores the challenges of NULL pointer dereferences in C. It argues that the problem isn't NULL itself, but its misuse. The article details best practices for preventing dereferences, including pre-dereference checks, pointer initiali

    Which C language compiler is better? Which C language compiler is better? Mar 03, 2025 pm 05:39 PM

    This article guides beginners on choosing a C compiler. It argues that GCC, due to its ease of use, wide availability, and extensive resources, is best for beginners. However, it also compares GCC, Clang, MSVC, and TCC, highlighting their differenc

    Is NULL still important in modern programming in C language? Is NULL still important in modern programming in C language? Mar 03, 2025 pm 05:35 PM

    This article emphasizes the continued importance of NULL in modern C programming. Despite advancements, NULL remains crucial for explicit pointer management, preventing segmentation faults by marking the absence of a valid memory address. Best prac

    What are the web versions of C language compilers? What are the web versions of C language compilers? Mar 03, 2025 pm 05:42 PM

    This article reviews online C compilers for beginners, focusing on ease of use and debugging capabilities. OnlineGDB and Repl.it are highlighted for their user-friendly interfaces and helpful debugging tools. Other options like Programiz and Compil

    Method of copying code by C language compiler Method of copying code by C language compiler Mar 03, 2025 pm 05:43 PM

    This article discusses efficient code copying in C IDEs. It emphasizes that copying is an IDE function, not a compiler feature, and details strategies for improved efficiency, including using IDE selection tools, code folding, search/replace, templa

    C language online programming website C language compiler official website summary C language online programming website C language compiler official website summary Mar 03, 2025 pm 05:41 PM

    This article compares online C programming platforms, highlighting differences in features like debugging tools, IDE functionality, standard compliance, and memory/execution limits. It argues that the "best" platform depends on user needs,

    C language compiler installation tutorial (computer version) C language compiler installation tutorial (computer version) Mar 03, 2025 pm 05:41 PM

    This tutorial guides users through installing C compilers on Windows, macOS, and Linux. It details installation for popular compilers (MinGW, Visual Studio, Xcode, GCC), explains environment variable configuration, and offers troubleshooting steps

    See all articles