js调用.net后台事件,和后台调用前台等方法总结 ajaxPro.dll基础教程(前台调用后台方法,后台调用前台方法)
1. javaScript函数中执行C#代码中的函数:
方法一:1、首先建立一个按钮,在后台将调用或处理的内容写入button_click中;
2、在前台写一个js函数,内容为document.getElementByIdx("btn1").click();
3、在前台或后台调用js函数,激发click事件,等于访问后台c#函数; 方法二:1、函数声明为public
后台代码(把public改成protected也可以)
public string ss()
{
return("a");
}//这种方法的缺点是不能将前台的参数传入到后台的函数体中
2、在html里用 <%=fucntion()%>可以调用
前台脚本
<script language=javascript>
var a = " <%=ss()%>";
alert(a);
</script>
方法三:1、 <script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument)
{
var theForm = document.Form1; //指runat=server的form theForm.__EVENTTARGET.value = eventTarget;
theFrom.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
-->
</script>
<input type="button" value="按钮" >
方法四: <script language="javascript">
function SubmitKeyClick()
{
if (event.keyCode == 13)
{
event.cancelBubble = true;
event.returnValue = false;
document.all.FunName.value="你要调用的函数名";
document.form[0].submit();
}
}
</script>
<INPUT type="text">
<input type="hidden" > 〈!--用来存储你要调用的函数 --〉
在.CS里有:
public Page_OnLoad()
{
if (!Page.IsPost())
{
string strFunName=Request.Form["FunName"]!=null?Request.Form["FunName"]:"";
//根据传回来的值决定调用哪个函数
switch(strFunName)
{
case "enter()":
enter() ; //调用该函数
break;
case "其他":
//调用其他函数
break;
default:
//调用默认函数
break;
}
}
}
public void enter()
{
//……比如计算某值
}
2.如何在JavaScript访问C#变量?
答案如下:
方法一:1、通过页面上隐藏域访问 <input type="hidden" runat="server"> 方法二:1、如后台定义了PUBLIC STRING N;前台js中引用该变量的格式为' <%=n%>'或"+ <%=n%>+"
方法三:1、或者你可以在服务器端变量赋值后在页面注册一段脚本
" <script language='javascript'>var temp=" + tmp + " </script>" tmp是后台变量,然后js中可以直接访问temp获得值。
3.如何在C#中访问JavaScript的已有变量?
答案如下:
方法一:1、前台使用静态文本控件隐藏域,将js变量值写入其中;
2、后台用request["id"]来获取值;
方法二:可以用cookie或session
4.如何在C#中访问JavaScript函数?
答案如下:
c#代码中执行javaScript函数:
方法一:1、Page.RegisterStartupScript("ggg"," <script>SetVisible(1); </script>");
方法二:使用Literal类,然后
private void Button2_Click(object sender, System.EventArgs e)
{
string str;
str=" <script language='javascript'>";
str+="selectRange()";
str+=" </script>";
//Literal1.Visible=true;
Literal1.Text=str;
}
原文来自:
第二篇:js调用.net后台事件,和后台调用前台等方法总结
js调用.net后台事件,和后台调用前台
1. javaScript函数中执行C#代码中的函数:
方法一:1、首先建立一个按钮,在后台将调用或处理的内容写入button_click中;
2、在前台写一个js函数,内容为
document.getElementByIdx("btn1").click();
3、在前台或后台调用js函数,激发click事件,等于访问后台c#函数;
方法二:1、函数声明为public
后台代码(把public改成protected也可以)
public string ss()
{
return("a");
}
2、在html里用 <%=fucntion()%>可以调用
前台脚本
<script language=javascript>
var a = " <%=ss()%>";
alert(a);
</script>
方法三:1、 <script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument)
{
var theForm = document.Form1; //指runat=server的form theForm.__EVENTTARGET.value = eventTarget;
theFrom.__EVENTARGUMENT.value = eventArgument; theForm.submit();
}
-->
</script>
<input type="button" value="按钮" >
方法四: <script language="javascript">
function SubmitKeyClick()
{
if (event.keyCode == 13)
{
event.cancelBubble = true;
event.returnValue = false;
document.all.FunName.value="你要调用的函数名";
document.form[0].submit();
}
}
</script>
<INPUT type="text">
<input type="hidden" > 〈!--用来存储你要调用的函数 --〉
在.CS里有:
public Page_OnLoad()
{
if (!Page.IsPost())
{
string
strFunName=Request.Form["FunName"]!=null?Request.Form["FunName"]:""; //根据传回来的值决定调用哪个函数
switch(strFunName)
{
case "enter()":
enter() ; //调用该函数
break;
case "其他":
//调用其他函数
break;
default:
//调用默认函数
break;
}
}
}
public void enter()
{
//……比如计算某值
}
2.如何在JavaScript访问C#变量?
答案如下:
方法一:1、通过页面上隐藏域访问 <input type="hidden" runat="server"> 方法二:1、如后台定义了PUBLIC STRING N;前台js中引用该变量的格式为' <%=n%>'或"+ <%=n%>+"
方法三:1、或者你可以在服务器端变量赋值后在页面注册一段脚本
" <script language='javascript'>var temp=" + tmp + " </script>" tmp是后台变量,然后js中可以直接访问temp获得值。
3.如何在C#中访问JavaScript的已有变量?
答案如下:
方法一:1、前台使用静态文本控件隐藏域,将js变量值写入其中;
2、后台用request["id"]来获取值;
方法二:可以用cookie或session
4.如何在C#中访问JavaScript函数?
答案如下:
c#代码中执行javaScript函数:
方法一:1、Page.RegisterStartupScript("ggg"," <script>SetVisible(1); </script>");
方法二:使用Literal类,然后
private void Button2_Click(object sender, System.EventArgs e) {
string str;
str=" <script language='javascript'>"; str+="selectRange()";
str+=" </script>";
//Literal1.Visible=true;
Literal1.Text=str;
}
第三篇:不同json数据格式之间的转换
待转换的json数据格式为:
[{
"id":1,
"text":"菜单",
"children":[{
"id":11,
"text":"菜单1",
"state":"closed",
"children":[{
"id":111,
"text":"测试11",
"attributes":{
"key":"Custom Attribute1" }
},{
"id":112,
"text":"测试12",
"attributes":{
"key":"xx"
},
},{
"id":113,
"text":"测试13",
"attributes":{
"key":"Custom Attribute1" }
}]
},{
"id":12,
"text":"菜单2",
"children":[{
"id":121,
"text":"测试21",
"attributes":{
"key":"Custom Attribute1" }
},{
"id":122,
"text":"测试22",
"attributes":{
"key":"Custom Attribute1" }
},{
"id":123,
"text":"测试23",
"attributes":{
"key":"Custom Attribute1"
}
}]
}]
}]
该json格式为jQuery easyui tree的json数据格式,先要把它转换成微信自定义菜单的json数据格式,如:
{
"button": [
{
"name": "菜单1",
"sub_button": [
{
"type": "click",
"name": "测试11",
"key": "Custom Attribute1",
"sub_button": [ ]
},
{
"type": "click",
"name": "测试12",
"key": "xx",
"sub_button": [ ]
},
{
"type": "click",
"name": "测试13",
"key": "Custom Attribute1",
"sub_button": [ ]
}
]
},
{
"name": "菜单2",
"sub_button": [
{
"type": "click",
"name": "测试21",
"key": "Custom Attribute1",
"sub_button": [ ]
},
{
"type": "click",
"name": "测试22",
"key": "Custom Attribute1",
"sub_button": [ ]
},
{
"type": "click",
"name": "测试23",
"key": "Custom Attribute1",
"sub_button": [ ]
}
]
}
]
}
所用函数为:
private static void toWeixinMenuJson(JSONObject jsonObject, JSONObject target) { if (Integer.parseInt(jsonObject.getString("id").toString()) != 1) { target.put("name", jsonObject.getString("text"));
JSONObject json = null;
json = jsonObject.getJSONObject("attributes");
if (json.size() != 0) {
String url = null;
String key = null;
if (json.containsKey("url")) {
url = json.getString("url");
}
if (json.containsKey("key")) {
key = json.getString("key");
}
if (url != null) {
target.put("type", "view");
target.put("url", url);
}
if (key != null) {
target.put("type", "click");
target.put("key", key);
}
}
}
JSONArray jsonArray = (JSONArray) jsonObject.get("children"); if (jsonArray != null) {
List<JSONObject> list = new ArrayList<JSONObject>();
Iterator itr = jsonArray.iterator();
while (itr.hasNext()) {
JSONObject jo2 = new JSONObject();
list.add(jo2);
toWeixinMenuJson((JSONObject) itr.next(), jo2);
}
JSONArray ja0 = new JSONArray();
ja0.addAll(list);
if (Integer.parseInt(jsonObject.getString("id").toString()) == 1) {
target.element("button", ja0);
} else {
target.element("sub_button", ja0);
}
}
}
该函数用了递归的思想。
反过来,先要把微信自定义菜单的json数据格式转换成Query easyui tree的json数据格式,可用如下函数,该函数同样用了递归的思想。
private static void toEasyuiTreeJson(JSONObject jsonObject, JSONObject target,int i) { JSONArray array = null;
target.put("id", i);
if (jsonObject.get("button")!=null){
target.put("text", "菜单");
array=jsonObject.getJSONArray("button");
}else{
target.put("text", jsonObject.get("name"));
array=jsonObject.getJSONArray("sub_button");
}
if(jsonObject.containsKey("type")){
String type=jsonObject.getString("type");
JSONObject json=new JSONObject();
if("click".equals(type)){
json.accumulate("key", jsonObject.getString("key"));
}
if("view".equals(type)){
json.accumulate("url", jsonObject.getString("url"));
}
} target.put("attributes", json); } if(array!=null){ List<JSONObject> list=new ArrayList<JSONObject>(); Iterator itr=array.iterator(); while(itr.hasNext()){ JSONObject jObject=new JSONObject(); list.add(jObject); toEasyuiTreeJson((JSONObject)itr.next(), jObject, ++i); } JSONArray array2=new JSONArray(); array2.addAll(list); target.element("children",array2); }