在自定义验证.NET客户端验证控件名称 [英] control names in a custom validator .NET Client Side Validation

查看:178
本文介绍了在自定义验证.NET客户端验证控件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框的三列的GridView控件。它可以有很多行作为必要的,但它通常只有5行。每一行需要被验证。

I have a gridview with three columns of textboxes. It can have as many rows as necessary but its usually only about 5 rows. Each row needs to be validated.

我想创建一个客户端验证,总结2列在一起,并将其与第三列进行比较,以检查用户输入正确的数据。

I want to create a client side validator that sums 2 of the columns together and compares it with the third column to check that the user has entered the data correctly.

以防万一你想知道,那就是操作人员必须进入第三列而不是简单地总结两个previous一起列在code后面的规范的一部分。这样做是为了确保操作者正确地转录的信息。

Just in case you are wondering, it's part of the spec that the operator must enter the third column rather than simply summing the two previous columns together in the code behind. This is done to ensure the operator is transcribing the information correctly.

我想使用自定义验证的.NET创建这个客户端验证。但我不能找到一种方法来传递给它的三个文本框的名称。 我可以使用的ControlToValidate参数给它的目标控件的名字,但我怎么传中,另外两个控件的ID的?

I am trying to use the custom validator in .net to create this client side validation. but I can't find a way to pass to it the names of the three text boxes. I can give it the target controls name using the ControlToValidate parameter, but how do I pass in the other two control id's ?

我要寻找的正确的方式做到这一点,我们的想法是使用JavaScript创建了的ControlToValidate的名字引用的数组。

I am looking for the 'proper' way to do this, one thought is to create an array in javascript referenced by the controltovalidate's name.

DC

推荐答案

我解决了这个问题。不是一个完美的解决方案,但它的工作原理。

I solved the problem. not an elegant solution but it works.

首先我把code到一个div的页面

first I placed the code into a div on the page

<div align="right"><asp:CustomValidator ID="RowValidator" runat="server"
ErrorMessage="Total of #total# does not equal 1st Preference + Ticket"
ControlToValidate="Total" ValidateEmptyText="True" 
ClientValidationFunction="CheckRow" SetFocusOnError="True" EnableClientScript="True"
enableViewState="False" Display="Dynamic"></asp:CustomValidator></div>

然后,我创建了一个JavaScript函数...

Then I created a JavaScript function...

function CheckRow(sender,args) {
// get the name of the control to validate
try {
    args.IsValid = true;
    ctv = sender.controltovalidate;

// get the data from the other controls
    nt = document.getElementById(ctv.replace('_Total','_NonTicket'));
    t = document.getElementById(ctv.replace('_Total','_Ticket'));

    if (nt && t) {
        v1 = Number(nt.value);
        v2 = Number(t.value);
        v3 = Number(args.Value);
        if ((v1 + v2) != v3){
            msg = GetMessage(sender);
            sender.innerHTML = msg.replace("#total#",Number(args.Value));
            args.IsValid = false;
            return false;
        }
   }
}
catch (e) {
    // something wrong default to server side validation
}
return true;
}

这是调用自定义验证为我用发送者的的ControlToValidate参数来获取这个名字的每一行

This is called by the custom validator for each row I use the controltovalidate parameter of the sender to get the name

那么位串操纵它的问题得到其他字段的名称。

then its a matter of a bit of string manipulation to get the names of the other fields.

一旦检索到你可以做你喜欢什么,在我的情况我想补充和比较。如果有错误和isValid标志被清除,该消息被修改以适应

Once retrieved you can do what you like, in my case I add and compare. if there is an error the Isvalid flag is cleared and the message is modified to suit.

该GetMessage函数是必需的,因为我改变邮件给一个更有意义的错误消息

The getmessage function is required because I alter the message to give a more meaningful error message

/*
get the error message from the validator
store it so it can be retrieved again
this is done because the message is altered
*/
function GetMessage(sender){        

msg = window[sender.id+"_msg"];
if (!msg){
    msg = sender.innerHTML;
    window[sender.id+"_msg"] = msg;
}
return msg;
}

该GetMessage函数保持原始邮件的副本,因此如果用户犯错不止一次的消息可以检索其原始的形式,其他明智的我们第一次修改,我们覆盖的占位符消息(#总# )。

The getmessage function keeps a copy of the original message so if the user makes a mistake more than once the message can be retrieved in its pristine form, other wise the first time we edit a message we overwrite the placeholder (#total#).

DC

这篇关于在自定义验证.NET客户端验证控件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆