ComfortablyNumb
ComfortablyNumb

Reputation: 1456

C# Custom Validation - Change div border colour

I have a set of custom validators that change the border colour of each textbox or drop down list. All works great. I'm also using a HtmlEditorExtender. I've surrounded it with a div element to style it the same as the other textboxes (a 2px solid grey).

My difficulty is to fire the custom validator to change the border of the div if the HtmlEditorExtender is empty.

Here is how I'm calling the Custom Validator for a textbox

protected void CustomValidatorNewsText_ServerValidate(object sender, ServerValidateEventArgs args)
    {
        args.IsValid = isValid(tbNewsStandFirst);
    }

protected bool isValid(System.Web.UI.WebControls.TextBox MyBox)
    {
        bool is_valid = MyBox.Text != "";
        MyBox.BorderColor = is_valid ? System.Drawing.Color.LightSlateGray : System.Drawing.Color.Crimson;
        return is_valid;
    }

What I want to do is replace Mybox.BorderColor with the ID of the div, but I can't seem to find the right syntax (I've added runat to the div).

Any suggestions?

Cheers, Numb

Upvotes: 0

Views: 805

Answers (1)

psantiago
psantiago

Reputation: 714

If you already set the div as a runat (and also gave it an id), something like the following should work:

this.divId.Style.Add(HtmlTextWriterStyle.BorderColor, System.Drawing.ColorTranslator.ToHtml(is_valid ? System.Drawing.Color.LightSlateGray : System.Drawing.Color.Crimson));

Upvotes: 1

Related Questions