close
http://www.morgantechspace.com/2014/01/Call-Server-Side-function-from-JavaScript-in-ASP-NET.html
<
head
id
=
"Head1"
runat
=
"server"
>
<
title
>Call Server Side method from JavaScript in ASP.NET using PageMethods</
title
>
<script type="text/javascript">
function
GetServerDate(format) {
PageMethods.GetServerDate(format, OnSuccess, OnFailure);
}
function
OnSuccess(dateTime) {
if
(dateTime) {
document.getElementById(
"currentDate"
).innerHTML = dateTime;
}
}
function
OnFailure(error) {
alert(error);
}
</script>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
asp:ScriptManager
ID
=
"scripman1"
runat
=
"server"
EnablePageMethods
=
"True"
>
</
asp:ScriptManager
> 這行一定要有
<
div
>
<
input
type
=
"button"
value
=
"Show UTC Server Time"
onclick
=
"GetServerDate('utc')"
/>
<
input
type
=
"button"
value
=
"Show Local Server Time"
onclick
=
"GetServerDate('local')"
/>
<
label
id
=
"currentDate"
>
This is current Date Time in Web Server</
label
>
</
div
>
</
form
>
</
body
>
</
html
>
protected void Page_Load( object sender, EventArgs e) { } [System.Web.Services.WebMethod(EnableSession=True)] 使用session要加 public static string GetServerDate( string format) { HttpContext.Current.Session["amy"] = "amy"; 跟Session["amy"]共用
if (format.Equals( "utc" )) { return DateTime.Now.ToUniversalTime().ToString(); } else { return DateTime.Now.ToLocalTime().ToString(); } }
|
全站熱搜