Tabiki. Kod aynen soyle:
Customer.htm
<!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>
<title></title>
<script type="text/javascript">
var xmlHttp;
function showCustomer(str) {
var xmlHttp;
if (str == "-1") {
document.getElementById("txtHint").innerHTML = "";
return;
}
xmlHttp = GetXMLHttpObject();
if (xmlHttp == null) {
alert("browseriniz AJAX desteklemiyor");
return;
}
var url = "getcustomer.aspx";
url += "?q=" + str;
xmlHttp.onreadystatechange = stateChanged;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChanged() {
if (xmlHttp.readyState == 4) {
document.getElementById("txtHint").innerHTML = xmlHttp.responseText;
}
else {
document.getElementById("txtHint").innerHTML = "loading...";
}
}
function GetXMLHttpObject() {
var xmlHttp = null;
try {
xmlHttp = new XMLHttpRequest();
}
catch (e) {
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
</head>
<body>
<select id="customer" onchange="showCustomer(this.value)">
<option value="-1">Select One</option>
<option value="ALFKI">Alfred</option>
<option value="NORTS">North</option>
<option value="WOLZA">Wolski</option>
</select>
<p>
<div id="txtHint">
<b>Customer info will be listed here</b>
</div>
</p>
</body>
</html>
getcustomer.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace AjaxOrnekleri
{
public partial class getcustomer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionstring = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionstring);
string sql = "SELECT * FROM CUSTOMERS WHERE CUSTOMERID='"+Request.QueryString["q"]+"'";
connection.Open();
SqlCommand cmd= new SqlCommand(sql,connection);
SqlDataReader dr= cmd.ExecuteReader(CommandBehavior.CloseConnection);
Response.Write("<table>");
while (dr.Read())
{
for (int i = 0; i < dr.FieldCount; i++)
{
Response.Write("<tr><td><b>"+dr.GetName(i).ToString()+"<b></td>");
Response.Write("<td>"+dr.GetValue(i).ToString()+"</td></tr>");
}
}
Response.Write("</table>");
Response.End();
}
}
}
Web.config
</sectionGroup>
</sectionGroup>
</configSections>
<appSettings/>
<connectionStrings>
<add name="NorthwindConnectionString" connectionString="DataSource=.; Initial Catalog=Northwind; Integrated Security=True"/>
</connectionStrings>
<system.web>
<!--
Set compilation....