【问题标题】:ASPX Server side comments messing up controls collectionASPX 服务器端评论搞乱了控件集合
【发布时间】:2014-02-13 22:28:49
【问题描述】:

我遇到了我遇到过的最奇怪的问题之一。它的荒谬。

服务器端 cmets 作为 LiteralControl 添加到原始容器控件中。我知道这听起来很疯狂,但这就是我在这里所经历的:(

我的环境:Visual Studio 2012、IIS 7、.net frameworks 4.0

我在这里复制我的测试页:

Default.aspx 页面:

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div runat="server" id="divTest">
       <%--test--%>
       <%--test--%>
    </div>
    </form>
</body>
</html>

默认.aspx.cs:

using System;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(string.Format("divTest Control count: {0}", divTest.Controls.Count));
    }
}

web.config:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>

结果页面显示输出:

divTest Control count: 3

最有趣的部分是,我尝试在其他 3 个框中运行相同的页面(具有完全相同的配置/环境),它在其中 2 个中按预期工作,另一个显示与我相同的结果。同样,如果我将构建目标框架版本更改为 2.0,它会显示预期结果(divTest Control count: 1)。仅当我在 4.0 中构建它时才会发生这种情况

知道这种奇怪行为的原因是什么吗?我在这里错过了什么吗?

谢谢

本杰明

【问题讨论】:

  • 我试过了,通过 VS 运行时得到 3,发布时得到 1。所以我想知道 Visual Studio 是不是故意这样做的。
  • “故意”是什么意思?这里的目的是什么?你的意思是它的错误?
  • 我注意到的另一件事是,如果我在发布模式下为任何目标框架(
  • 我的意思是 Visual Studio 在内部做一些事情来跟踪 cmets。不过这只是猜测。
  • 感谢您的意见。但是,当它在任何地方都没有任何状态或对象级别的可见性时,将 cmets 添加到控件集合对我来说没有任何意义。我严重怀疑它的视觉工作室本身是否存在错误。我在微软官方的 asp.net 论坛上也没有收到任何回复。

标签: asp.net comments


【解决方案1】:

额外的 LiteralControl 不是来自注释,而是它周围的空格。

从 VS.NET 2010 切换到 2013 时,我也发生了同样的事情(是的,刚刚升级了 VS.NET - 网站目标框架没有变化 - 4.0)

我的解决方案是使用 FindControl() 方法,而不是直接通过索引访问 ControlCollection。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多