RE: CustomValidator

Tech-Archive recommends: Speed Up your PC by fixing your registry



Hi Allen,

Thanks. That worked. And for responses to my questions.

One more question:
How do I display a custom error message in the client validation? On the
server side I have the following that works.

protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
GridView gv1 = GridView1 as GridView;
TextBox tb3 = gv1.Rows[gv1.EditIndex].FindControl("TextBox3") as
TextBox;
CustomValidator cv1 =
gv1.Rows[gv1.EditIndex].FindControl("CustomValidator1") as CustomValidator;
string tb3Text = tb3.Text as string;
string ddl1Value = args.Value;
if (ddl1Value == "6")
{
if (tb3.Text == "0")
{
args.IsValid = true;
}
else
{
args.IsValid = false;
cv1.ErrorMessage = "Record Number MUST be zero if
Notification Type set to Message Light";
}
}

if (tb3.Text == "0")
{
if (ddl1Value == "6")
{
args.IsValid = true;
}
else
{
args.IsValid = false;
cv1.ErrorMessage = "Notification Type MUST be set to Message
Light if Record Number is zero";
}

}
}


My client script is now:

<script type="text/javascript">
function NotfType_RecNo(sender, args)
{
var clientid = document.getElementById('<%=hidden1.ClientID
%>').value;
var tb3 = document.getElementById(clientid);
var tb3Value = tb3.value;
var ddl1Value = args.Value;

if (ddl1Value == "6")
{
if (tb3Value == "0")
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
}
</script>
--
Thanks
Morris


"Allen Chen [MSFT]" wrote:

Hi Morris,

Thanks for the update. I think it's still the ClientID issue. You can try
<%=hidden1.ClientID %> to get the ClientID of the hidden input:

function NotfType_RecNo(sender, args) {
var clientid = document.getElementById('<%=hidden1.ClientID
%>').value;

.....

Quote from Morris ==================================================
How is validation done normally, custom validating the values between 2 or
more fields? This client side validation seems quite complex for 1 field,
I
cannot imaging maintaining for additional fields. Is this complicated as I
have each field in its own template in a gridview? Is a better design to
keep all fields as a single template field (I loose the sorting or would
then
have to do some manual work around).

I thought field validations would be so simple. Your help and advice are
much appreciated.
==================================================

We need to write additional code when using CustomValidator, especially
when it's placed in the GridView and we need client validation. Your design
is good that to keep each field in its own template. You don't have to
change it.

In general we have two options to do the client side validation for
CustomValidator in GridView.

" Store the client id of the control in a hidden input, as I suggested.
Then get it in the ClientValidationFunction to do the client side
validation.

" Navigate in the DOM to find the element. It's even more complicated and
hard to maintain comparing to the above option.

Validation would be simple if you use other validation controls such as
RequiredFieldValidator. However, as the name indicates the CustomValidator
control provides more flexibility but requires us to write more code.

Regards,
Allen Chen
Microsoft Online Support


.


Quantcast