03/01/2012
Jquery Ajax
PLease note that you need to download a jquery.js file
-----------------------------------Ajax.aspx page
$(document).ready(function () {
$(" ").click(function ()
{
$.ajax
(
{
url: "ajax1.aspx?function=getrecords&details=" + $(" ").val() + "&rand=" + Math.random(),
success: function (data) {
$(" ").html(data);
}
}
);
});
})
-----------------------------------------------------
Ajax.cs file
----------
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 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
-----------------
------- Ajax1.aspx page
--------------------------------------
------------------------Ajax1.cs -----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
public partial class ajax1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
details();
}
public void details()
{
string function = Request.QueryString["function"];
switch (function.ToLower())
{
case "getrecords":
recorddetails();
break;
}
}
public void recorddetails()
{
string customerId = Request.QueryString["details"];
SqlConnection con = new SqlConnection("Data Source=Localhost;Initial Catalog=ganesh;User Id=sa;Password=master");
con.Open();
SqlCommand com = new SqlCommand("Select * from employee Where id=" + customerId, con);
SqlDataReader oReader = com.ExecuteReader();
bool blnFound = false;
StringBuilder Data = new StringBuilder();
if (oReader.HasRows)
{
while (oReader.Read())
{
blnFound = true;
Data.Append("");
Data.Append("");
Data.Append("Name");
Data.Append("" + Convert.ToString(oReader["Fname"]) + "");
Data.Append("");
Data.Append("");
Data.Append("Address");
Data.Append("" + Convert.ToString(oReader["City"]) + "");
Data.Append("");
Data.Append("");
}
}
else
{
Data.Append("");
Data.Append("");
Data.Append("Customer not found");
Data.Append("");
Data.Append("");
Data.Append("");
}
Response.Write(Data.ToString());
}
}
----------------------------------