从 jQuery 调用服务器端函数的方法是通过 ajax 请求。您不需要在 Session 中放置任何内容,您只需将客户端的值作为参数传递给服务器端的函数即可。这是一个例子:
function ShowDialogAndCallServerSideFunction()
{
var $dialog = $('<div class="dialog"></div>')
.html('Dialog content goes here')
.dialog({
autoOpen: false,
width: 320,
title: 'Title goes here',
closeOnEscape: true,
buttons: [
{
text: "No",
click: function() { $(this).dialog("close"); }
},
{
text: "Yes",
click: function() {
$.ajax({
"type": "POST",
"dataType": 'json',
"contentType": "application/json; charset=utf-8",
"url": "WebServiceUrl.asmx/MethodName",
"data": "{'parameter': " + your_parameterHere + " }",
"success": function(result) {
//handle success here
},
"error": function(xhr, ajaxOptions, thrownError) {
//handle any errors here
}
});
$(this).dialog("close");
}
}
]
});
$dialog.dialog('open');
}
在服务器端,您可以拥有一个 Web 服务——在我的示例中称为 WebServiceUrl——:
[WebMethod]
public void MethodName(string parameter)
{
//the value received in 'parameter' is the value passed from the client-side via jQuery
}