:master: sinceramente, date le premesse ("non capisco niente di javascript e ajax"), credo che il miglior consiglio sia di guardarsi qualche guida di ajax (quella di html.it è buona)
Infatti, il codice di sotto fa quello che chiedi. Ma serve a qualcosa se non si ha una minima conoscenza di javascript? :master:
codice:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string result = "";
string comando_ajax = this.Request.QueryString["comando_ajax"];
if (comando_ajax != null && comando_ajax == "1")
{
try
{
string username = this.Request.Params["username"];
int n = (int)msole.RisultatoAggregazioneSQL(gl.StringaConnessioneTest, @"select count(""username"") from utenti where [username] = ?", msole.createParameterString("username", 50, username));
result = ((n > 0) ? "utente già esistente" : "utente valido");
}
catch (Exception ex)
{ result = ex.Message; }
finally
{
}
this.Response.Write(result);
this.Response.End();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
/** ABC - Ajax Basic Call
* @author Andrea Giammarchi
* @site www.devpro.it */
ABC = (function(ABC){
return function(data, url, async, user, pass){
var time = new Date,
abc = ABC(),
send = [],
key = null;
if(data){
for(key in data)
if(typeof data[key] !== "function" && data.hasOwnProperty(key)){
if(data[key] instanceof Array)
for(var i = 0; i < data[key].length; i++)
send.push(encodeURIComponent(key).concat("=", encodeURIComponent(data[key][i])));
else
send.push(encodeURIComponent(key).concat("=", encodeURIComponent(data[key])));
}
key = 0 < send.length ? send.join("&") : null;
};
abc.open(
key !== null ? "POST" : "GET",
url,
async = async === undefined ? true : !!async,
user !== undefined && user,
pass !== undefined && pass
);
abc.setRequestHeader("If-Modified-Since", "Mon, 26 Jul 1997 05:00:00 GMT");
abc.setRequestHeader("Cache-Control", "no-cache");
abc.setRequestHeader("X-Requested-With", "XMLHttpRequest");
if(key !== null)
abc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
if(async)
abc.onreadystatechange= function(){
if(abc.readyState === 4 && data){
key = /^(2|3)[0-9]{2}$/.test(abc.status) ? "onLoad" : "onError";
if(typeof data[key] === "function")
data[key](abc, new Date - time);
}
};
abc.send(key);
return abc;
};
})(this.XMLHttpRequest ?
function(){return new XMLHttpRequest;} :
function(){return new ActiveXObject("Microsoft.XMLHTTP");}
);
function username_onchange()
{
document.getElementById("username").disabled = true;
ABC({username:document.getElementById("username").value, onLoad:onload, onError:onerror}, "?comando_ajax=1");
function onload(request)
{
document.getElementById("username").disabled = false;
document.getElementById("span1").innerHTML = request.responseText;
}
function onerror(xhr)
{
document.getElementById("username").disabled = false;
alert("Errore di trasmissione: " + xhr.status);
}
}
</script>
</head>
<body>
<div>
<input id="username" type="text" onchange="return username_onchange()" />
<span id="span1"></span>
</div>
</body>
</html>