在js中试试这个:
$('#Button1').click(function () {
// this calls default.aspx
$.ajax({
type: "POST",
url: '/Default.aspx',
data: "{ServerSideMethod : '1'}", // send a parameter, to tell it what we want
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
function(data){
// data is json sent from server, if not, try $.parseJSON(data)
// do something here after the server side code has finished
if(data.ok){
//success from server side
}
}
});
return false;
});
});
在 Default.aspx.cs 中:
// fires anytime default.aspx is loaded
protected void Page_Load(object sender, EventArgs e)
{
// check if is ajax call and not normal page load in the browser
if (Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
Response.Clear(); //empty everithing so we don't send mixed content
// no cache on ajax, IE caches ajas if this is missing
Response.Cache.SetExpires(DateTime.Today.AddMilliseconds(1.0));
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
// here we are checking what we want to do, what client side asked
if(!string.IsNullOrWhiteSpace(Request["ServerSideMethod"])) // this will be "1"
{
doAll(); // do your work
}
Response.End();
}
}
private void doAll()
{
// do work, then send some json, as this is what you expect
// JavaScriptSerializer is located in System.Web.Script.Serialization
Response.Write(new JavaScriptSerializer().Serialize(new { ok = 1, error = 0 }));
}