Click here to Skip to main content
15,891,372 members
Articles / Web Development / XHTML

Using AJAX with ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
3.64/5 (8 votes)
13 Jul 2008CPOL4 min read 67.9K   948   33  
This article demonstrates how to use AJAX with ASP.NET 2.0.
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
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.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

public partial class Names : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["q"] != null)
        {
            GetNames();
        }
    }
    
    private void GetNames()
    {
        string strQuery = Request.QueryString["q"];

        //Create the XML-like string to be sent back to the request 
        string strXmlNames = "";

        ArrayList al = new ArrayList();
        al = RetreiveNames();

        // Copies the elements of the ArrayList to a string array.
        String[] arrStrNames = (String[])al.ToArray(typeof(string));

        foreach (string strName in arrStrNames)
        {
            strXmlNames += "<user><name>" + strName + "</name></user>";
        }
        strXmlNames = "<users>" + strXmlNames + "</users>";

        //Create an XmlDocument object to store the XML string that we created 
        XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml(strXmlNames);

        //Create a navigator object to use in the XSL transformation 
        XPathNavigator xPathNav = xDoc.CreateNavigator();

        //Create the XSLTransform object 
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(Server.MapPath("Names.xslt"));

        //Do the transformation and send the results out to the Response object's output stream 
        xslt.Transform(xPathNav, null, Response.OutputStream);
        
    }

    public ArrayList RetreiveNames()
    {
        ArrayList al = new ArrayList();

        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=.;Integrated Security=True;Database=Northwind";

        SqlCommand cmd = new SqlCommand("SELECT ContactName from Customers WHERE ContactName LIKE UPPER('" + Request.QueryString["q"] + "%')", con);
        
        con.Open();

        SqlDataReader rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
            al.Add(rdr[0].ToString());
        }
        return al;
    }
}

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)


Written By
India India
Sudipta Chaudhari is having 14+ yrs of professional full stack software design & development experience with multiple onsite/client site visit experiences.

He has extensive experience working on – REACT, C#, .NET Core, Angular, AZURE, ASP.NET Web API, ASP.NET MVC, WPF, SQL Server, JQuery to name a few. He also has experience as a SCRUM Master.

For latest articles and updates, please visit by website/blog : http://sudiptachaudhari.com

Comments and Discussions