JS調(diào)用CS里的帶參方法實(shí)例
更新時(shí)間:2013年08月01日 15:33:21 作者:
js調(diào)用CS里的方法有很多,我用一種簡(jiǎn)單的方法,如下,有需要的朋友可以參考一下
CS里
public string test()
{
return "Hello World";
}
aspx 頁(yè)面
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>無(wú)標(biāo)題頁(yè)</title>
<mce:script type="text/javascript" ><!--
var demo=function(){
var b= "<%=test() %>";
alert(b);
}
// --></mce:script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="id1" onclick="demo()" value="JS調(diào)用CS" />
</div>
</form>
</body>
</html>
上面的是不帶參數(shù)的,要是后臺(tái)CS里方法帶參數(shù)就要注意了。。
CS:
public string test(string a)
{
return a;
}
aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>無(wú)標(biāo)題頁(yè)</title>
<mce:script type="text/javascript" ><!--
var demo=function(){
var a="Hello World";
var b= '<%=test("'+a+'") %>';//這里一定注意單引號(hào)和雙引號(hào)的使用!!!!!
alert(b);
}
// --></mce:script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="id1" onclick="demo()" value="JS調(diào)用CS" />
</div>
</form>
</body>
</html>
總結(jié):帶參數(shù)的調(diào)用'<%=test("'+a+'") %>' 一定要用單引號(hào)包起'<%=%>',里面再是雙引號(hào)。這點(diǎn)是關(guān)鍵,否則
調(diào)用不成功!萬(wàn)惡的單引號(hào)和雙引號(hào)?。。?!
復(fù)制代碼 代碼如下:
public string test()
{
return "Hello World";
}
aspx 頁(yè)面
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>無(wú)標(biāo)題頁(yè)</title>
<mce:script type="text/javascript" ><!--
var demo=function(){
var b= "<%=test() %>";
alert(b);
}
// --></mce:script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="id1" onclick="demo()" value="JS調(diào)用CS" />
</div>
</form>
</body>
</html>
上面的是不帶參數(shù)的,要是后臺(tái)CS里方法帶參數(shù)就要注意了。。
CS:
復(fù)制代碼 代碼如下:
public string test(string a)
{
return a;
}
aspx:
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>無(wú)標(biāo)題頁(yè)</title>
<mce:script type="text/javascript" ><!--
var demo=function(){
var a="Hello World";
var b= '<%=test("'+a+'") %>';//這里一定注意單引號(hào)和雙引號(hào)的使用!!!!!
alert(b);
}
// --></mce:script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="id1" onclick="demo()" value="JS調(diào)用CS" />
</div>
</form>
</body>
</html>
總結(jié):帶參數(shù)的調(diào)用'<%=test("'+a+'") %>' 一定要用單引號(hào)包起'<%=%>',里面再是雙引號(hào)。這點(diǎn)是關(guān)鍵,否則
調(diào)用不成功!萬(wàn)惡的單引號(hào)和雙引號(hào)?。。?!
相關(guān)文章
js實(shí)現(xiàn)簡(jiǎn)單鼠標(biāo)跟隨效果的方法
這篇文章主要介紹了js實(shí)現(xiàn)簡(jiǎn)單鼠標(biāo)跟隨效果的方法,涉及javascript鼠標(biāo)事件與坐標(biāo)獲取等相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04ES6中module模塊化開(kāi)發(fā)實(shí)例淺析
這篇文章主要介紹了ES6中module模塊化開(kāi)發(fā),結(jié)合實(shí)例形式分析了ES6中模塊化開(kāi)發(fā)的相關(guān)功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-04-04Javascript實(shí)現(xiàn)數(shù)組中的元素上下移動(dòng)
這篇文章主要給大家介紹了Javascript實(shí)現(xiàn)數(shù)組中的元素上下移動(dòng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04JavaScript實(shí)現(xiàn)旋轉(zhuǎn)輪播圖
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)旋轉(zhuǎn)輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08基于JavaScript實(shí)現(xiàn)評(píng)論框展開(kāi)和隱藏功能
本文通過(guò)實(shí)例代碼給大家介紹了基于JavaScript實(shí)現(xiàn)評(píng)論框展開(kāi)和隱藏功能,感興趣的朋友參考下吧2017-08-08JS 拖動(dòng)效果實(shí)現(xiàn)代碼 比較簡(jiǎn)單
JS拖動(dòng)效果,計(jì)算的位移,主要是應(yīng)用了鼠標(biāo)事件與坐標(biāo)控制。2009-11-11