日期:2014-05-18  浏览次数:20908 次

js调用后台函数 传递变量怎么写?
ss 是后台函数;
C# code

   public string ss(string a)
    {
    }


下面前台调用没问题
JScript code

  var    Obj = document.getElementById("TextBox1").value;
       
         var a = "<%=ss("1")%>";


下面传递变量不知道怎么写了
JScript code

  var    Obj = document.getElementById("TextBox1").value;
       
         var a = "<%=ss(Obj)%>";//不对
        var a = "<%=ss("Obj")%>";//不对
var a = "<%=ss("+Obj+")%>";//不对


请问改怎么写才正确

------解决方案--------------------
你直接在TextBox1上加runat=server, 然后 var a = "<%=ss(TextBox1.value)%>";
------解决方案--------------------
服务器代码执行完毕才发送给客户端的,你要搞清楚基本原理,
你需要采用ajax实现
或者回调
------解决方案--------------------
参见
<%@ Page Language="C#" EnableViewState="false" %>
 
<%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private string ReturnBackValue = " 返回到客户端的内容 | ";
 
//声明函数,用来返回到客户端。
public string GetCallbackResult()
{
return ReturnBackValue;//将结果返回客户端
}
 
//声明一个函数,用来接收客户端的参数;函数名字不可以随便,必须是RaiseCallbackEvent才可以
public void RaiseCallbackEvent(String eventArgument)
{
this.ReturnBackValue += eventArgument;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function myfunction(arg) {
<%=Page.ClientScript.GetCallbackEventReference(Page, "arg", "showMsg","")%>;
}
function showMsg(rValue) {
alert(rValue);
}
alert("刷新测试");
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="button" onclick="myfunction('客户端传入的参数')" value="无刷新调用" />
</form>
</body>
</html>
------解决方案--------------------
回调函数或者AJAX
AJAX:
JScript code
   function Button1_onclick() {
   
            var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
            if (!xmlhttp) {
                alert("XMLHTTP对象失败");
                return false;
            }
            xmlhttp.open("POST", "Handler.ashx?value="+document.getElementById("TextBox1").value, false);
            xmlhttp.onreadystatechange = function (){  
             
                if (xmlhttp.readyState == 4&&xmlhttp.status == 200) {

                    alert(xmlhttp.responseText);  //返回值
                }
          }
            xmlhttp.send();
     
        }