views:

27

answers:

1

The following test:

[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);
    }
  }

fails, and using the string "ctl00$ContentPlaceHolder1$ControlId" as control Id provide a proper control... I know, ASP.NET contains "ClientID" property for web-controls, but is there any possibility to know in advance the control's client Id in the TEST (Under VS 2008)?

Thanks.

+1  A: 

I don't think the ClientID is what you're after here. I think your problem is that FindControl is not doing what you think it is.

FindControl is not recursive. If your textbox is inside of a ContentPlaceHolder, then you need to call FindControl on the placeholder, not the Page.

Otherwise, I suggest writing a recursive FindControl function that will search the entire control heirarchy. You can see an example here.

womp