【问题标题】:Can't find control (ASP.NET, MS TEST)找不到控件(ASP.NET,MS TEST)
【发布时间】:2011-03-05 04:41:15
【问题描述】:

以下测试:

[TestClass]
public class MyTestClass
{
    private TestContext _testContext;
    protected TestContext TestContext
    {
        get { return _testContext; }
        set { _testContext = value; }
    }

    [TestMethod]
    [HostType("ASP.NET")]
    [UrlToTest("http://localhost/MyPage.aspx")]
    public void TestMyPage()
    {
        TextBox tb = TestContext.RequestedPage.FindControl("ControlId") as TextBox;
        Assert.IsNotNull(tb);
    }
  }

失败,并且使用字符串“ctl00$ContentPlaceHolder1$ControlId”作为控件 ID 提供了适当的控件...我知道,ASP.NET 包含用于 Web 控件的“ClientID”属性,但是否有可能知道在 TEST 中推进控件的客户端 ID(VS 2008 下)?

谢谢。

【问题讨论】:

    标签: asp.net unit-testing testing


    【解决方案1】:

    我不认为ClientID 是你在这里所追求的。我认为你的问题是 FindControl 没有按照你的想法做。

    FindControl 不是递归的。如果您的文本框在 ContentPlaceHolder 内,那么您需要在占位符上调用 FindControl,而不是 Page

    否则,我建议编写一个递归 FindControl 函数来搜索整个控件层次结构。你可以看到一个例子here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 2022-11-03
      • 2010-11-25
      相关资源
      最近更新 更多