zoukankan      html  css  js  c++  java
  • 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" onclick="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" onclick="Show()" value="hhhh" />

  • 相关阅读:
    680. Valid Palindrome II【easy】
    125. Valid Palindrome【easy】
    459. Repeated Substring Pattern【easy】
    2. Trailing Zeros【easy】
    142. O(1) Check Power of 2【easy】
    181. Flip Bits【easy】
    183.Wood Cut【hard】
    61. Search for a Range【medium】
    关闭微软对win10的推送
    让未激活的win8.1不再跳出提示激活的窗口
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/2014838.html
Copyright © 2011-2022 走看看