C# 为什么我在尝试使用设计器中form类的变量时出错?

C# 为什么我在尝试使用设计器中form类的变量时出错?,c#,asp.net,webforms,C#,Asp.net,Webforms,在代码隐藏中,我有一个名为ReportFeatures和Page\u Load事件的属性: public partial class FeatureList : System.Web.UI.Page { protected string ReportFeatures; protected void Page_Load(object sender, EventArgs e) { IEnumerable<

在代码隐藏中,我有一个名为
ReportFeatures
Page\u Load
事件的属性:

    public partial class FeatureList : System.Web.UI.Page
    {
        protected string ReportFeatures;

        protected void Page_Load(object sender, EventArgs e)
        {
            IEnumerable<ReportFeature> featureProps = fim.getFeatureProperties();

            ReportFeatures = featureProps.ToJson();
        }
    }
public部分类功能列表:System.Web.UI.Page
{
受保护的字符串特性;
受保护的无效页面加载(对象发送方、事件参数e)
{
IEnumerable featureProps=fim.getFeatureProperties();
ReportFeatures=featureProps.ToJson();
}
}
在designer中,我尝试访问ReportFeatures变量:

<head runat="server">
    <title></title>
    <script type="text/javascript">
        window.reportFeatures = <%= ReportFeatures%>;
    </script>
</head>

window.reportFeatures=;
加载页面时,我收到以下错误:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
无法修改控件集合,因为控件包含代码块(即)。
你知道我为什么会犯这样的错误,以及如何修复它吗

不要使用
块,尝试使用数据绑定表达式语法(
),因为
隐式调用
页面中的
Response.Write()
方法。Header
作为代码块计算,而数据绑定表达式不计算:

<head runat="server">
    <title></title>
    <script type="text/javascript">
        window.reportFeatures = <%# ReportFeatures %>;
    </script>
</head>
有关此问题的更多详细信息,请参见

protected void Page_Load(object sender, EventArgs e)
{
    IEnumerable<ReportFeature> featureProps = fim.getFeatureProperties();

    ReportFeatures = featureProps.ToJson();

    // add this line
    Page.Header.DataBind();
}