js 调用C#.NET后台方法 转载自:http://www.cnblogs.com/lizhao/archive/2010/11/23/1990436.html...
第一種:
<script type="text/javascript">
?$(document).ready(function() {
?sshow();
?});
?
?function sshow()
?{?
????? var s = '<%=IsShow() %>';?
????? if(s == '0')
????? {
?????? document.getElementById("trr").style.display= "none";????
????? }else
????? {
????? document.getElementById("trr").style.display= "";
????? }
????? alert(s);
?}
public int IsShow()
??? {
??????? int sis = 0;
??????? ASPxLabel urll = (ASPxLabel)DataList1.Items[0].FindControl("ASPxLabelURL");
??????? //Response.Write(urll.Text.Length);
??????? //Response.Write(urll.Text.IndexOf(".", 9, 2).ToString());
??????? if (urll.Text.Length > 12)
??????? {
??????????? if (urll.Text.IndexOf(".", 9, 3) > -1)
??????????? {
??????????????? sis = 1;
??????????? }
??????? }
??????? return sis;
??? }
第二種
// 需要標識為WebMethod?
[System.Web.Services.WebMethod]
// 注意,要讓前臺調用的方法,一定要是public和static的?
public static string aaa(string name)
{
??? string result = "Hello:" + name;
??? return result;
}
??? <mce:script type="text/javascript"><!--
?
??????? function btnClick(){
??????????? // 調用頁面后臺方法,前面跟方法所需的參數,接著是方法回調成功時要執行的js函數,最后一個是方法回調失敗時要執行的js函數
??????????? PageMethods.aaa("you",funReady,funError);
??????? }????????
??????? // result 就是后臺方法返回的數據
??????? function funReady(result){
??????????? alert(result);
??????? }
??????? // err 就是后臺方法返回的錯誤信息
??????? function funError(err){
??????????? alert("Error:" + err._message );
??????? }
???????
// --></mce:script>
??? <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
??????? <input type="button" οnclick="btnClick()" value="test" />
?方法2:
???
??? function Show()
??? {
????? var v = "中國";
????? var s = '<%=CsharpVoid("'+v+'") %>';? // 你好!“+V+”
????? alert(s);
??? }
??? protected string CsharpVoid(string strCC)
??? {
?????? strCC = "你好!" + strCC;
?????? return strCC;
??? }
<input type="button" οnclick="Show()" value="hhhh" />
轉載于:https://www.cnblogs.com/wuhuisheng/archive/2011/04/13/2014838.html
總結
以上是生活随笔為你收集整理的js 调用C#.NET后台方法 转载自:http://www.cnblogs.com/lizhao/archive/2010/11/23/1990436.html...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ascx页面获取标签的通用方法
- 下一篇: [转载] 中文核心期刊目录(2008)