asp.net 服务器端控件使用服务器端变量

后台代码 
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";
       }
   }

前台:

 <asp:Label ID="Label1" runat="server" >"<%= GetVariableStr %>"</asp:Label>
  <
asp:TextBox ID="TextBox1" runat="server" >"<%= GetVariableStr %>"</asp:TextBox>

<input type="button" id="bt" runat="server"  onclick="fun()" value="确定" />

  <script type="text/javascript">
        function
fun() {

            var str = '<%= GetVariableStr%>';
            //前台位置1,绑定的是第三种变量类型(也是第二种方式,?因为Now是个属性)
           
alert(str);
        }
    </script>


posted @ 2011-04-26 16:25  茶海  阅读(419)  评论(0编辑  收藏  举报