Good day!
Im trying to unit test my current project(the code behind) but I can't manipulate the controls inside the content page. Say I want to put some text inside the textbox, where textbox is inside the content page. I tried the accessor but failed, example: MyClass_Accessor acc = new MyClass_Accessor(); acc.textboxUserName.Text = "MyName"; this attempt was not successful it says Object reference not set to an instance of an object... I also tried the page.FindControl("textboxUserName") but failed also.
When I tried to manipulate the controls inside the Master page, it works using page.Master.FindControl("labelTitle").

How to manipulate asp controls inside the content page with unit testing?

Hope you can help me on this.

thanks and best regards,
gilmar

If I understand your question correctly you would have to make the text property of the textbox public to access it from an external class. Using FindControl, especially in the context of an entire page, is not recommended as it is a recursive operation and resource intensive. I would only use FindControl if there was really no other alternative (like getting a handle for a control in a repeater template for example)

public string TextBoxText
{
    get { return txtTextBox.Text; }
    set { txtTextBox.Text = value; }
}

If I understand your question correctly you would have to make the text property of the textbox public to access it from an external class. Using FindControl, especially in the context of an entire page, is not recommended as it is a recursive operation and resource intensive. I would only use FindControl if there was really no other alternative (like getting a handle for a control in a repeater template for example)

public string TextBoxText
{
    get { return txtTextBox.Text; }
    set { txtTextBox.Text = value; }
}

Thanks sedgey for the reply and tips.
Actually, I was using page.FindControl for unit testing only just to test my code behind if it really works as expected.


regards,
gilmar

no probs, if that fixed the problem could you mark it as solved,
thanks

no probs, if that fixed the problem could you mark it as solved,
thanks

Problem Solved!:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.