RequiredFieldValidator with a UserControl

A

Andrew Robinson

Is it possible to have a RequiredFieldValidator that targets a UserControl?

<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ControlToValidate="PhoneUserControlHome" Display="dynamic"
Text="&nbsp;(Required Field)" />

This code generates a run time exception of "Control 'PhoneUserControlHome'
referenced by the ControlToValidate property of 'RequiredFieldValidator6'
cannot be validated. "

Thanks,
 
A

Andrew Robinson

Love answering my own questions:

[ValidationProperty("ValidationText")]

public partial class PhoneUserControl : System.Web.UI.UserControl

{

protected void Page_Load(object sender, EventArgs e) { }



const string template = "({0}) {1}-{2}";

public string FormattedText

{

get { return string.Format(template, TextBoxAreaCode.Text,
TextBoxPrefix.Text, TextBoxSuffix.Text); }

}



public string ValidationText

{

get

{

if (TextBoxAreaCode.Text.Length != 3 ||
TextBoxPrefix.Text.Length != 3 || TextBoxSuffix.Text.Length != 4)

{

return string.Empty;

}



return TextBoxAreaCode.Text + TextBoxPrefix.Text +
TextBoxSuffix.Text;

}

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top