ti do un esempio: prendi il codice javascript, non quello server
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 comando_ajax = ModuloWeb.RequestParams("comando_ajax");
if (comando_ajax == "aggiorna_database") aggiorna_database();
}
void aggiorna_database()
{
string testo = "";
string sql = "UPDATE CAMPI SET TESTO = ? WHERE ID = 1 ";
OleDbConnection con = null;
try
{
testo = ModuloWeb.RequestParams("testo");
con = new OleDbConnection(MioModulo.StringaConnessioneTest);
con.Open();
OleDbCommand com = con.CreateCommand();
com.CommandText = sql;
com.Parameters.Add("testo", OleDbType.VarChar, 50).Value = ModuloWeb.StringNullToDBNull(testo);
com.ExecuteNonQuery();
this.Response.Write("Tabella aggiornata con successo");
}
catch
{
this.Response.Write("Errore aggiornamento tabella");
}
finally
{
if (con != null) con.Close();
}
Response.End();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Pagina senza titolo</title>
<link href="../../../stili/Styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../../js/sajax.js"></script>
<script language="javascript" type="text/javascript">
// <!CDATA[
function Button1_onclick()
{
var parametri = "testo=" + $("Text1").value;
sajax("?comando_ajax=aggiorna_database", onload, parametri);
function onload()
{
$("div1").innerHTML = this.request.responseText;
}
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server" >
<label for="Text1">nome: </label>
<input id="Text1" type="text" style="border:1px black solid;" />
<input id="Button1" type="button" value="aggiorna" onclick="Button1_onclick();"/>
<div id="div1"></div>
</form>
</body>
</html>
la funzione ajax è
codice:
// File JScript
function sajax(url, onload, parameters, onerror)
{
parameters = (parameters == undefined)? "" : parameters;
//creazione oggetto per richiesta web
var objHTTP = getXMLHttp();
objHTTP.open("post", url, true);
objHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
objHTTP.setRequestHeader('Content-length',parameters.length);
objHTTP.setRequestHeader('Connection', 'close');
objHTTP.onreadystatechange = function()
{
if (objHTTP.readyState == 4)
{
if (objHTTP.status == 200 || objHTTP.status == 0)
{
this.request = objHTTP;
if(onload) onload.call(this);
}
else {if(onerror && typeof(onerror) == "function") {onerror(defaultError); return;}else {defaultError(); return;}}
}
};
objHTTP.send(parameters);
function getXMLHttp()
{
var xmlhttp = null;
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest(); // Gecko (Firefox, Moz), KHTML (Konqueror, Safari), Opera, Internet Explorer 7
}
else if(window.ActiveXObject)
{
try
{
xmlhttp = new ActiveXObject("MSXML2.XMLHTTP"); // Internet Explorer 6
}
catch(e)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer 4,5
}
}
else
{
xmlhttp = null;
}
return xmlhttp;
};
function defaultError()
{
alert("ERRORE NELLA TRASMISSIONE DATI!");
};
}
//al posto di mettere document.getElementById("div1"), mettere $("div1")
//da prototype.js
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}