| 订阅 | 在线投稿
分享
 
 
 

.net中前台javascript与c#后台代码调用

2008-11-06 07:16:03  编辑来源:互联网  宽屏版  评论

本文为【.net中前台javascript与c#后台代码调用】的汉字拼音对照版显示拼音

.netzhongC#daimayujavaScripthanshudexianghutiaoyongwenti

wen

1.ruhezaiJavaScript访fangwenC#hanshu?

2.ruhezaiJavaScript访fangwenC#bianliang?

3.ruhezaiC#zhong访fangwenJavaScriptdeyiyoubianliang?

4.ruhezaiC#zhong访fangwenJavaScripthanshu?

wenti1daanruxia

javaScripthanshuzhongzhixingC#daimazhongdehanshu

fangfayi1shouxianjianliyigeanniuzaihoutaijiangtiaoyonghuochulideneirongxierubutton_clickzhong;

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

3zaiqiantaihuohoutaitiaoyongjshanshujifaclickshijiandengyu访fangwenhoutaic#hanshu;

fangfaer1hanshushengmingweipublic

houtaidaima(bapublicgaichengprotectedyekeyi)

public string ss()

{

return("a");

}

2zaihtmlliyongkeyitiaoyong

qiantaijiaoben

<script language=javascript>

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

alert(a);

</script>

fangfasan1

<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 id="Button1" type="button" name="Button1" value="anniu" onclick="javascript:__doPostBack('Button1','')">

fangfasi

<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 onkeypress="SubmitKeyClick()" id="aaa" type="text">

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

zai.CSliyou

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

}

wenti2.ruhezaiJavaScript访fangwenC#bianliang?

daanruxia

fangfayi1tongguoyemianshangyincangyu访fangwen

<input id="xx" type="hidden" runat="server">

fangfaer1ruhoutaidingyilePUBLIC STRING N; qiantaijszhongyinyonggaibianliangdegeshiwei''huo"++"

fangfasan1huozhenikeyizaifuwuqiduanbianliangfuzhihouzaiyemianzhuceyiduanjiaoben

"<script language='javascript'>var temp=" + tmp + "</script>"

tmpshihoutaibianliangranhoujszhongkeyizhijie访fangwentemphuodezhi

wenti3.ruhezaiC#zhong访fangwenJavaScriptdeyiyoubianliang?

daanruxia

fangfayi1qiantai使shiyongjingtaiwenbenkongjianyincangyujiangjsbianliangzhixieruqizhong;2houtaiyongrequest["id"]laihuoquzhi;

fangfaerkeyiyongcookiehuosession

wenti4.ruhezaiC#zhong访fangwenJavaScripthanshu?

daanruxia

c#daimazhongzhixingjavaScripthanshu

fangfayi1

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

fangfaer使shiyongLiteralleiranhou

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

{

string str;

str=" ";

//Literal1.Visible=true;

Literal1.Text=str;

}

原文
.net中C#代码与javaScript函数的相互调用问题。   问:   1.如何在JavaScript访问C#函数?   2.如何在JavaScript访问C#变量?   3.如何在C#中访问JavaScript的已有变量?   4.如何在C#中访问JavaScript函数?   问题1答案如下:   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>   方法三: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 id="Button1" type="button" name="Button1" value="按钮" onclick="javascript:__doPostBack('Button1','')">   方法四: <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 onkeypress="SubmitKeyClick()" id="aaa" type="text">   <input type="hidden" name="FunName"> 〈!--用来存储你要调用的函数 --〉   在.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 id="xx" type="hidden" runat="server">   方法二:1、如后台定义了PUBLIC STRING N; 前台js中引用该变量的格式为''或"++"   方法三: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"," ");   方法二:使用Literal类,然后 private void Button2_Click(object sender, System.EventArgs e)   {   string str;   str=" ";   //Literal1.Visible=true;   Literal1.Text=str;   }
󰈣󰈤
 
 
 
>>返回首页<<
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
©2005- 王朝网络 版权所有