Click here to Skip to main content
11,412,159 members (65,484 online)
Click here to Skip to main content
Articles » Web Development » Ajax » Samples » Downloads
Add your own
alternative version

A Working Example of Five Different Ways to Make jQuery AJAX Calls

, 2 Feb 2010 CPOL
This article introduces a working example of five different ways to make AJAX calls using jQuery.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace jQueryFiveAjaxCalls.StudentRecordArchive
{
    public partial class StudentRecordsHTMLFragment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String ReturnJson = Request.QueryString["RETURNJSON"];
            String NoOfRecordsToRetrieve = Request.QueryString["NRECORD"];

            if (NoOfRecordsToRetrieve == null)
            {
                NoOfRecordsToRetrieve = Request.Form["NRECORD"];
            }

            StudentRecords Records = new StudentRecords(System.Convert.ToInt16(NoOfRecordsToRetrieve));

            DataGrid DG = new DataGrid();
            DG.CellPadding = 14;
            DG.Style.Add("font-size", "10px");
            DG.HeaderStyle.BackColor = System.Drawing.Color.Brown;
            DG.HeaderStyle.ForeColor = System.Drawing.Color.White;
            DG.HeaderStyle.Font.Bold = true;

            DG.Attributes.Add("bordercolor", "black");
            DG.DataSource = Records;
            DG.DataBind();

            StringBuilder SB = new StringBuilder();
            HtmlTextWriter TW = new HtmlTextWriter(new System.IO.StringWriter(SB));
            DG.RenderControl(TW);

            string ContentType = "text/html";
            string Content = SB.ToString();
            if (ReturnJson == "True")
            {
                ContentType = "text/x-json";
                StringBuilder JsonSB = new StringBuilder();
                JsonSB.Append("{\"TB\": \"");
                JsonSB.Append(SB.Replace("\"", "'").Replace("\t", "").Replace("\r", "").Replace("\n", ""));
                JsonSB.Append("\"}");
                Content = JsonSB.ToString();
            }

            Response.ContentType = ContentType;
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            Response.OutputStream.Write(encoding.GetBytes(Content), 0, Content.Length);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dr. Song Li

United States United States
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.5 | Last Updated 2 Feb 2010
Article Copyright 2010 by Dr. Song Li
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid