在html include中设置js变量

时间:2009-08-06 17:02:13

标签: c# asp.net javascript

我有一个ASP.NET页面,它使用头文件的include方法。我正在添加一个从JS函数

访问的JS变量

head.htm

<script language="javascript" type="text/javascript">
    var render=<%= RenderProperty %>;
</script>

RenderProperty是基页类中的一个方法(继承自System.Web.UI.Page的.cs文件)

它看起来像这样:

private bool _renderProp = false; 
public bool RenderProperty 
{
  get
  {
    return _renderProp;
  }
  set
  {
    _renderProp = value;
  }
}

在逐页的基础上,我将RenderProperty设置在aspx页面的Page_Load

protected void Page_Load(object sender, EventArgs e)
{
  RenderProperty = true;
}

我收到一个编译时错误,上面写着:

The name 'RenderProperty' does not exist in the current context 
C:\...\head.htm

1 个答案:

答案 0 :(得分:0)

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 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public string pepe
    {
        get { return "ddd"; }
    }
}


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%=this.pepe %>
    </div>
    </form>
</body>
</html>