JS 之 前台调用后台变量和方法

来源:互联网 发布:js车牌代表什么意思 编辑:程序博客网 时间:2024/05/20 01:13

      在我们平时的网站开发过程中,并不是所有的网页都是后台查询出一个DataTable或者List然后绑定到DataGrid上。我们可能还会遇到前台需要后台的某一个变量,或者前台某个显示的地方需要调用一下后台的某一个查询的方法。下面我就给大家列举一个小例子:



Asp.Net前台代码:

<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title></head><body>    <form id="form1" runat="server">    <div>        <input id="Text1" type="text" value="<%=str%>" />    </div>    <div>        <input id="Text2" type="text" value="<%=Fun()%>" />    </div>    <div>        <input id="Text3" type="text" value="<%#str%>" />    </div>    <div>        <input id="Text4" type="text" value="<%#Fun()%>" />    </div>        </form></body></html>


C#后台代码:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page{    public string str = "";    protected void Page_Load(object sender, EventArgs e)    {        str = "我是一个后台全局变量";        //this.DataBind();        }    //不能为private      protected string Fun()    {        return "我是后台方法的返回值";    }}


效果一:

C#后台代码:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page{    public string str = "";    protected void Page_Load(object sender, EventArgs e)    {        str = "我是一个后台全局变量";        this.DataBind();        }    //不能为private      protected string Fun()    {        return "我是后台方法的返回值";    }}


效果二:


        相信大家通过上面的小例子已经知道怎么在前台调用后台的变量和方法,需要说明的是<%=%>和<%#%>都可以调用后台的变量和方法,但是<%#%>必须在代码中执行DataBind()方法的时候才会执行。







1 0
原创粉丝点击