| 订阅 | 在线投稿
分享
 
 
 

C#代码与JavaScript函数的相互调用

2008-08-15 05:14:18  编辑来源:互联网  宽屏版  评论

本文为【C#代码与JavaScript函数的相互调用】的汉字拼音对照版显示拼音

womentongguowendadefangshilaiweidajiajiedaC#yuJavaScripthanshudexianghutiaoyong

1ruhezaiJavaScript访fangwenC#hanshu?

2ruhezaiJavaScript访fangwenC#bianliang?

3ruhezaiC#zhong访fangwenJavaScriptdeyiyoubianliang?

4ruhezaiC#zhong访fangwenJavaScripthanshu?

wenti1ruhezaiJavaScript访fangwenC#hanshu?

daanruxia

javaScripthanshuzhongzhixingC#daimazhongdehanshu

fangfayi

1shouxianjianliyigeanniuzaihoutaijiangtiaoyonghuochulideneirongxierubutton_clickzhong

2zaiqiantaixieyigejshanshuneirongweidocument.getElementById("btn1").click()

3zaiqiantaihuohoutaitiaoyongjshanshujifaclickshijiandengyu访fangwenhoutaic#hanshu

fangfaer

1hanshushengmingweipublic

houtaidaima(bapublicgaichengprotectedyekeyi)

yixiashiyinyongpianduan

public string ss()

{

return("a");

}

2zaihtmllikeyitiaoyongqiantaijiaoben

yixiashiyinyongpianduan

<script language=javascript>

var a = "<%=ss()%>";

alert(a);

</script>

fangfasan

yixiashiyinyongpianduan

<script language="javascript">

<!--

function __doPostBack(eventTarget, eventArgument)

{

var theForm = document.Form1; //zhirunat=serverdeform

theForm.__EVENTTARGET.value = eventTarget;

theFrom.__EVENTARGUMENT.value = eventArgument;

theForm.submit();

}

-->

</script>

<input type="button" value="anniu"

onclick="">

fangfasi

yixiashiyinyongpianduan

script language="javascript">

function SubmitKeyClick()

{

if (event.keyCode == 13)

{

event.cancelBubble = true;

event.returnValue = false;

document.all.FunName.value="niyaotiaoyongdehanshuming"

document.form[0].submit();

}

}

</script>

<INPUT type="text">

<input type="hidden" > --yonglaicunchuniyaotiaoyongdehanshu --

zai.CSliyou

yixiashiyinyongpianduan

public Page_OnLoad()

{

if (!Page.IsPost())

{

string strFunName=Request.Form["FunName"]!=null?Request.Form["FunName"]:"";

//genjuchuanhuilaidezhijuedingtiaoyongnagehanshu

switch(strFunName)

{

case "enter()":

enter() ; //tiaoyonggaihanshu

break;

case "qita":

//tiaoyongqitahanshu

break;

default:

//tiaoyongmorenhanshu

break;

}

}

}

public void enter()

{

//birujisuanmouzhi

}wenti2ruhezaiJavaScript访fangwenC#bianliang?

daanruxia

fangfayitongguoyemianshangyincangyu访fangwen<input type="hidden" runat="server">

fangfaerruhoutaidingyilePUBLIC STRING N;qiantaijszhongyinyonggaibianliangdegeshiweihuo"++"

fangfasanhuozhenikeyizaifuwuqiduanbianliangfuzhihouzaiyemianzhuceyiduanjiaoben

"<script language=javascript>var temp=" + tmp + "</script>" tmpshihoutaibianliangranhoujszhongkeyizhijie访fangwentemphuodezhi

wenti3ruhezaiC#zhong访fangwenJavaScriptdeyiyoubianliang?

daanruxia

fangfayi

1qiantai使shiyongjingtaiwenbenkongjianyincangyujiangjsbianliangzhixieruqizhong

2houtaiyongrequest["id"]laihuoquzhi

fangfaer

keyiyongcookiehuosession

wenti4ruhezaiC#zhong访fangwenJavaScripthanshu?

daanruxia

c#daimazhongzhixingjavaScripthanshu

fangfayi

Page.RegisterStartupScript("ggg"," ");

fangfaer

使shiyongLiteralleiranhou

yixiashiyinyongpianduan

private void Button2_Click(object sender, System.EventArgs e)

{

string str;

str="";

//Literal1.Visible=true;

Literal1.Text=str;

}

原文
我们通过问答的方式来为大家解答C#与JavaScript函数的相互调用: 1、如何在JavaScript访问C#函数? 2、如何在JavaScript访问C#变量? 3、如何在C#中访问JavaScript的已有变量? 4、如何在C#中访问JavaScript函数? 问题1:如何在JavaScript访问C#函数? 答案如下: javaScript函数中执行C#代码中的函数: 方法一: 1、首先建立一个按钮,在后台将调用或处理的内容写入button_click中; 2、在前台写一个js函数,内容为document.getElementById("btn1").click(); 3、在前台或后台调用js函数,激发click事件,等于访问后台c#函数; 方法二: 1、函数声明为public 后台代码(把public改成protected也可以) 以下是引用片段: public string ss() { return("a"); } 2、在html里,可以调用前台脚本 以下是引用片段: <script language=javascript> var a = "<%=ss()%>"; alert(a); </script> 方法三: 以下是引用片段: <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="按钮" onclick=""> 方法四: 以下是引用片段: 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#变量? 答案如下: 方法一:通过页面上隐藏域访问<input type="hidden" runat="server">; 方法二:如后台定义了PUBLIC STRING N;前台js中引用该变量的格式为’’或"++"; 方法三:或者你可以在服务器端变量赋值后在页面注册一段脚本; "<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函数: 方法一: Page.RegisterStartupScript("ggg"," "); 方法二: 使用Literal类,然后 以下是引用片段: private void Button2_Click(object sender, System.EventArgs e) { string str; str=""; //Literal1.Visible=true; Literal1.Text=str; }
󰈣󰈤
 
 
 
>>返回首页<<
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
©2005- 王朝网络 版权所有