.net 中 前台aspx页面调用后台.cs文件中的变量

定义全局变量
在Page_load上面写
public string Url;

后台代码:

public partial class WebForm2 : System.Web.UI.Page
   {
       public string GetVariableStr;//注意变量的修饰符
       protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               GetVariableStr = "hello world from variable";
           }
       }
       protected string GetFunctionStr()//注意返回值的修饰符
       {
           return "hello world from Function";
       }
   }

  前台代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        function fun() {

            var str = '<%= DateTime.Now %>';
            //前台位置1,绑定的是第三种变量类型(也是第二种方式,?因为Now是个属性)
            alert(str);
        }
    </script>
</head>
<body onload="fun()">
    <form id="form1" runat="server">   
        <div>
             <input type="text" value="<%= GetVariableStr %>" />
                                                  <%--前台位置2,绑定的是成员变量--%>
             "<%= GetFunctionStr() %>"
                                                  <%--前台位置3,绑定的是一个方法的返回值>--%>
        </div>
    </form>
</body>
</html>
原文地址:https://www.cnblogs.com/y0umer/p/3839240.html