1

I have created a visual web part in which user has to specify a field's value from taxonomy picker control. I want to make that value mandatory. The control is placed successfully but when I use the <asp:requieredfieldvalidator> it throws error.

My code is:

<Taxonomy:TaxonomyWebTaggingControl runat="server" 
        ID="taxProjectOwnerDepartment" Visible="true" IsMulti="false" IsDisplayPickerButton="true">
            </Taxonomy:TaxonomyWebTaggingControl>
        <asp:RequiredFieldValidator ID="rfvProjectOwnerDepartment" runat="server" ControlToValidate="taxProjectOwnerDepartment"
             ErrorMessage="Please Specfiy Owner's Department" ForeColor="Red" Display="Dynamic" />

The log shows this

What should I do?

3

change your code to:

<Taxonomy:TaxonomyWebTaggingControl runat="server" ID="taxProjectOwnerDepartment" Visible="true" IsMulti="false" IsDisplayPickerButton="true">
</Taxonomy:TaxonomyWebTaggingControl>

<asp:CustomValidator ID="rfvProjectOwnerDepartment" runat="server"  OnServerValidate="validator_ServerValidate" />

now in your codebehind add the new method validator_ServerValidate:

    protected void validator_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
    {
        string errorMessage = "Please Specfiy Owner's Department";
        if (!taxProjectOwnerDepartment.Validate(out errorMessage))
        {
            args.IsValid = false;
            rfvProjectOwnerDepartment.ForeColor = ConsoleColor.Red;
            rfvProjectOwnerDepartment.Display = ValidatorDisplay.Dynamic;
            rfvProjectOwnerDepartment.ErrorMessage = errorMessage;
        }
    }

the value of taxonomy is stored in a hidden input field so its not really a input field and throws an error when calling ControlToValidate="taxProjectOwnerDepartment" unfortunatly

Is it possible to have a required field validator for Taxonomy webtagging control

EDIT

For issues with empty input you need to add:

if(e.Value.Length == 8)
{
    e.IsValid = true;
}
else
{
    e.IsValid = false;
}

so code will look like this:

    protected void validator_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
    {
        string errorMessage = "Please Specfiy Owner's Department";

        if(e.Value.Length == 0)
        {
             args.IsValid = false;
             rfvProjectOwnerDepartment.ForeColor = ConsoleColor.Red;
             rfvProjectOwnerDepartment.Display = ValidatorDisplay.Dynamic;
             rfvProjectOwnerDepartment.ErrorMessage = errorMessage;
        }
        else
        {
            if (!taxProjectOwnerDepartment.Validate(out errorMessage))
            {
               args.IsValid = false;
               rfvProjectOwnerDepartment.ForeColor = ConsoleColor.Red;
               rfvProjectOwnerDepartment.Display = ValidatorDisplay.Dynamic;
               rfvProjectOwnerDepartment.ErrorMessage = errorMessage;
            }
        }
    }

so now we have changed the codebehind to first check if the length is correct (not 0) if its isnt 0 then do check what was entered to validate! code is a bit bulky, you could change it so that the if function is in one checking with an or statment like this:

    protected void validator_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
    {
        string errorMessage = "Please Specfiy Owner's Department";

            if (e.Value.Length == 0 || !taxProjectOwnerDepartment.Validate(out errorMessage))
            {
               args.IsValid = false;
               rfvProjectOwnerDepartment.ForeColor = ConsoleColor.Red;
               rfvProjectOwnerDepartment.Display = ValidatorDisplay.Dynamic;
               rfvProjectOwnerDepartment.ErrorMessage = errorMessage;
            }
    }
  • I am not finding region.Validate – users1100 Oct 1 '13 at 13:27
  • sorry just updated the code! just copy paste the code and it will work, iv changed everything so that it matches – Ali Jafer Oct 1 '13 at 13:27
  • tried as per your suggestion but not working – users1100 Oct 1 '13 at 13:33
  • what happens? does it show an error message or simply does nothing atall? make sure you try the new code that iv just ammened! this code should work! – Ali Jafer Oct 1 '13 at 13:42
  • if that doesnt work you can try this method! its very similar msdn.microsoft.com/en-us/library/… use the same code that i posted but also add in from the link above from the onload function – Ali Jafer Oct 1 '13 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.