You have to find the content place holder first, then use the content placeholder to find the control instead of using page.findcontrol
find the cph.findcontrol in the 2nd code example.
<asp:content id="Content3" contentplaceholderid="MainContent" runat="Server" xmlns:asp="#unknown">
<div style="width: 350px; text-align: left;">
<asp:label>
ID="lblFirstName"
Text="First Name"
AssociatedControlID="txtFirstName"
runat="server" />
</asp:label></div>
<div style="width: 350px; text-align: left;">
<asp:textbox id="txtFirstName" runat="server" />
<asp:requiredfieldvalidator id="reqFirstName" controltovalidate="txtFirstName" text="(Required)" enableclientscript="false" runat="server" />
</div>
<div style="width: 350px; text-align: left;">
<asp:label id="lblLastName" text="Last Name" associatedcontrolid="txtLastName" runat="server" />
</div>
<div style="width: 350px; text-align: left;">
<asp:textbox id="txtLastname" runat="server" />
<asp:requiredfieldvalidator>
ID="reqLastName" ControlToValidate="txtLastName" Text="(Required)" EnableClientScript="false" runat="server" />
</asp:requiredfieldvalidator></div>
<div style="width: 350px; text-align: left;">
<asp:button id="btnSubmit" text="Submit" runat="server" />
</div>
</asp:content>
Partial Class test
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
If Page.IsPostBack Then
'Indicate to the user that the page is not valid
Dim cphMainContent As ContentPlaceHolder = Me.Master.FindControl("MainContent")
For Each fieldValidator As RequiredFieldValidator In Page.Validators
Dim txtControl As TextBox = cphMainContent.FindControl(fieldValidator.ControlToValidate.ToString)
If Not txtControl Is Nothing Then
If Not fieldValidator.IsValid Then
txtControl.Style.Remove("border")
txtControl.Style.Add("border", "solid 1px rgb(255,0,0)")
Else
txtControl.Style.Remove("border")
txtControl.Style.Add("border", "solid 1px rgb(140,140,140)")
End If
End If
Next
End If
End Sub
End Class