zoukankan      html  css  js  c++  java
  • asp.net中前台javascript与后台C#交互

    方法一:使用Ajax开发框架,后台方法定义前添加[AjaxPro.AjaxMethod],然后就可以在前台js脚本中调用后台C#函数。

      方法二:后台方法声明为public或者protected,然后前台使用js脚本进行调用。

    以下是代码片段:

    .cs
    public string Str()
    {
    return "javaScript函数中执行后台C#方法..";
    }
    .aspx
    <script type="text/javascript">
    var a = "<%=Str()%>";
    alert(a);
    </script>

      方法三:使用Session变量

    以下是代码片段:
    .cs
    if (Session["siteName"] == null)//判断是否存在指定Key值的Session变量
    Session["siteName"] = "";//如果不存在则创建Session变量
    //给Session["siteName"]变量赋值
    .aspx
    var siteName="<%=Session["siteName"] %>";

      方法四

    以下是代码片段:
     
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" style="display:none;"/>
    <input type="button" id="btnSave " value="保存" onclick="GetBehind ();return false;" />
    .aspx.cs
    <script type="text/javascript">
    //调用后台方法
    //使用这种方式调用后台代码,可以在调用之前在前台做一些验证
    function GetBehind() {
    document.getElementById("Button1").click();
    }
    </script>
    .cs
    protected void Button1_Click(object sender, EventArgs e)
    {}

      如何在后台C#代码中执行javaScript函数

      方法一:void Page.RegisterStartupScript(string key,string script);

      方法二:使用隐藏域或者Literal控件,在前台使用js脚本把一些js函数控制的值写进隐藏域或者Literal控件,然后前台使用Hidden.Value或者Literal.Text读取前台值。

    以下是代码片段:
        .aspx
      function GetTitleID(obj)
      {
      sTitleID=obj
      if(sTitleID!=null)
      document.getElementById("HiddenField1").value=type+','+sTitleID;
      else
      document.getElementById("HiddenField1").value=type+',0';
      }
      .cs
      string hiddenValue = this.HiddenField1.Value;
  • 相关阅读:
    awk
    tac
    cat
    less
    more
    head
    vim
    linux安装redis
    Redis for Python开发手册
    Python3.x标准模块库目录
  • 原文地址:https://www.cnblogs.com/12go/p/2265373.html
Copyright © 2011-2022 走看看