I have a CustomValidator control and within the OnServerValidate event i want to set properties of the control that is being validated.
You would think the validated control object would be available in the OnServerValidate event, but it is not. I then tried to do a FindControl but can't seem to get access to the ControlToValidate value.
Would someone please help me get the TextBox control being validated so that i can modify its properties from the OnServerValidate event?
Thanks, D
I tried the above answer but as my TextBox was in a ListView in a ContentPlaceHolder it all got a bit out of hand. I ended up using this which should work in all circumstances:
string textBoxName = ((CustomValidator)source).ControlToValidate;
var textBox = ((CustomValidator)source).Parent.FindControl(textBoxName) as TextBox;
Can you do something like this?
protected void CustomValidator1_ServerValidate (object source, ServerValidateEventArgs args)
{
var validationControl = source as CustomValidator;
var textBox = FindControl(validationControl.ControlToValidate) as TextBox;
if (textBox != null)
{
// Do something
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With