Pages

Wednesday, July 8, 2009

Using Jquery with ASP.NET simple example.

download source code for this example download

Prerequisites

Download jquery java script library from http://jquery.com/

Steps

1. Create new web site

1. Add jquery java script file(s) to solution.

2. Create new page (ajax_page.aspx) and add following code.

C# ajax_page.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class ajax_page : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string sampleJSONObject =

"{\"FullName\" : \"Dilhan Jayathilaka\", \"Email\" : \"dilhan.jayathilaka@live.com\"}";

Response.Write(sampleJSONObject);

Response.End();

}

}

3. Add javascript to call ajax page using jquery.

default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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></title>

<script src="jQuery/jquery-1.3.2.js" type="text/javascript"></script>

<script language="javascript" type="text/javascript">

// JQuery ajax call.

$.getJSON("ajax_page.aspx", function(json) {

$("#txtFullName").html(json.FullName);

$("#txtEmail").html(json.Email);

});

</script>

</head>

<body>

<form id="form1" runat="server">

<div>

<span id="txtFullName" ></span>

<br />

<span id="txtEmail" ></span>

</div>

</form>

</body>

</html>




Open Source JQuery ASP.NET - (DJ - jQuery Web Contorls for ASP.NET)http://dj.codeplex.com/

No comments: