日期:2014-05-17  浏览次数:20446 次

Asp.net AJAX怎样在JavaScript中调用后台的方法?
听说Asp.net   AJAX不能通过JavaScript调用后台的方法,但我觉得不大可能,
因为ajaxpro都可以通过JavaScript调用后台的方法,Asp.net   AJAX为什么不能呢?
哪位大哥懂得Asp.net   AJAX通过JavaScript调用后台的方法请指点一下

------解决方案--------------------
神呐,谁说滴
一,后台方法:
protected string CsharpVoid(string strCC)
{
return strCC;
}
javascript 调用
<script language= "javascript ">
var s = " <%=CsharpVoid( "www.hagen.cn ")%> ";
document.write(s);
</script>

二,

1.后代码:
protected void CsharpVoid()
{
string strCC = "www.hagen.cn ";
Response.Write(strCC);
}

2.调用方法:CsharpVoid()
<script language= "javascript ">
document.write( " <%CsharpVoid();%> ");
</script>

三,

把需要调用的函数写在服务器控件里,然后在javascript中加入 <img src= "/Images/None.gif " align=top> document.getElementById( "你控件ID ").click();

------解决方案--------------------
Asp.net AJAX一般建议是调用webservice方法,而不是直接调用页面的方法
------解决方案--------------------
javascript+webservice.htc 可以调用后台在webservice写的代码

可以搜索关键字 webservice

godgreat说的方法,也算吧,不过好象更似技巧
------解决方案--------------------
最原始办法如下:
.aspx代码:

<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "test1.aspx.cs " Inherits= "test1 " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<asp:ScriptManager ID= "sm1 " runat= "server "> </asp:ScriptManager>
<asp:TextBox ID= "txt_PostServerMethod " runat= "server " OnTextChanged= "txt_PostServerMethod_TextChanged " > </asp:TextBox>
<input id= "btn_ExecMethod " onclick= "_do_ServerMethod(document.getElementById( 'txt_PostServerMethod ').value);_do_ClientMethod( 'red '); " type= "button " value= "调用后台方法 " />
<asp:UpdatePanel ID= "UpdatePanel1 " runat= "server " UpdateMode= "Conditional ">
<ContentTemplate>
<div id= "div_Result " runat= "server "> </div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID= "txt_PostServerMethod " EventName= "TextChanged " />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
<script language= "javascript ">
/**************调用服务端方法*************/
function _do_ServerMethod(ServerMethodName)
{
if(ServerMethodName!= " ")
{
__doPostBack( 'txt_PostServerMethod ', ' ');
}
}
/**************调用客户端方法*************/
function _do_ClientMethod(color)
{alert( "客户端方法 ")
document.getElementById( 'div_Result ').style.color=color;