RequiredFieldValidator with a UserControl

  • Thread starter Thread starter Andrew Robinson
  • Start date Start date
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,
 
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;

}

}

}
 
Back
Top