Is it possible to call an ASP.NET function inside a Javascript function?
Is it possible to call an ASP.NET function inside a Javascript function?
eg
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio开发者_运维问答nal//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btnSave_Click(object sender, EventArgs e)
{
}
</script>
<head>.....
</head>
<script type="text/javascript" language="javascript">
function abc()
{ .
.
.
.
btnSave_Click(sender,e);
}
This code is just to give you an idea of what I want.
No, you would need to do a post back to the server to accomplish this.
not directly. the javascript code is executed by the browser, ie on the computer of the visitor of your page, whereas the asp code is executed on the server. ways exist to mix the two - you might want to read up on AJAX (asynchronous javascript) which is basically all about getting the client-side to update based on server-side functionality without an actual reload.
You could use javascript to call a web service (.asmx) on the server and return a result. It is easy to do and probably sounds more daunting than the actual work involved.
Take a look at "jQuery, AJAX, and ASMX" for a tutorial to get you started. It is easy to write a simple "Hello World" web service and it is definitely worth the effort to learn more about making AJAX calls to the server.
It's also worth looking at WCF. There are some good posts on SO to look at:
- What are the benefits of using WCF over ASMX web services?
- Webservices vs WCF
In order to call server-side code from client-side code you'll need to post back to the server. Here's a quick result from Google on the subject:
http://www.dotnetspider.com/resources/1521-How-call-Postback-from-Javascript.aspx
精彩评论