I have my setup below. When rendering the page it throws this error: The name 'UserName' does not exist in the current context
. I don't understand why because my control is right above the call. I have this same setup in a separate control and it works just fine. Can anyone explain this?
<asp:TextBox ID="UserName" runat="server" Width="136px"></asp:TextBox>
<asp:CustomValidator ID="cvUserNameOrEmailRequired" ValidationGroup="LoginForm"
runat="server" CssClass="input-error" ErrorMessage="Username is required"
ControlToValidate="UserName" Display="Dynamic"
ClientValidationFunction="UsernameValidateTextBox" ValidateEmptyText="True">
</asp:CustomValidator>
<script type="text/javascript">
function UsernameValidateTextBox(source, arguments) {
if (arguments.Value % 2 == 0) {
arguments.IsValid = false;
} else {
arguments.IsValid = true;
}
}
**//ERROR IS THROWN HERE**
$("#<%=UserName.ClientID %>").focus(function () {
$("#<%=cvUserNameOrEmailRequired.ClientID %>").css({ visibility: "hidden" });
});
</script>
UPDATE
If I remove this call:$("#<%=UserName.ClientID %>").focus(function () {
I then get the same error for <%=cvUserNameOrEmailRequired.ClientID %>
The code above is inside a <asp:Login>
tag placing it outside removes the error.
UPDATE
I moved the jQuery code outside of the <asp:Login>
and the error went away. I used:
$('#<%=LoginForm.FindControl("UserName").ClientID%>').focus(function () {
$('#<%=LoginForm.FindControl("cvUserNameOrEmailRequired").ClientID%>')
.css({ visibility: "hidden" });
});
And no problems. But why doesn't it work within the <asp:Login>
tag?