[英]javascript OO question - specify private member function in c# object?
I'm not sure how to achieve the following in javascript, or even if I'm thinking about it correctly. 我不确定如何在javascript中实现以下目标,或者即使我正在正确考虑中也不确定。 Basically I want to attach a javascript member function to each custom object rendered, so I could have something like this in C#:
基本上,我想将javascript成员函数附加到呈现的每个自定义对象上,因此我可以在C#中进行如下操作:
public class NumericTextBox : TextBox
{
...
string clientScript = "function isValid() { return isNumericValue(this.value); }";
AttachValidationFunction(clientScript);
}
public class EmailTextBox : TextBox
{
...
string clientScript = "function isValid() { return isEmail(this.value); }";
AttachValidationFunction(clientScript);
}
and then use the following javascript function in the page 然后在页面中使用以下javascript函数
function isFormValid() {
var controls = getElementsByClass("validatingControl");
...
if (!controls[i].isValid()) return false;
...
}
obvisouly in pseudo-code, but hopefully that gives the idea of what I need to achieve. 显然是用伪代码编写的,但是希望可以给出我需要实现的想法。 Any suggestions?
有什么建议么?
what i would do, is to make sure that AttachValidationFunction receives this.ClientID
along with the validation function, adds to a list (say, Dictionary<String, String>
) and at render time, registers a javascript block with an array of all the added controls, where the result would look like this: 我要做的是确保AttachValidationFunction接收到
this.ClientID
以及验证函数,添加到列表(例如Dictionary<String, String>
)中,并在渲染时,用所有添加了控件,结果如下所示:
<script type="text/javascript">
var controlsToValidate = [
{ id: 'ctl00_txtNumeric', validate: function(e) { return isNumeric(e.value); } },
{ id: 'ctl00_txtEmail', validate: function(e) { return isEmail(e.value); } }
];
</script>
And then you could iterate over that array like so: 然后您可以像这样遍历该数组:
<script type="text/javascript">
function isFormValid() {
for(var i = 0; i < controlsToValidate.length; i++) {
var control = controlsToValidate[i];
var field = document.getElementById(control.id);
if(!control.validate(field))
return false;
}
return true;
}
</script>
I hope that's quite clear. 我希望这很清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.