Asp.Net:扩展验证范围

编程入门 行业动态 更新时间:2024-10-28 14:28:34
本文介绍了Asp.Net:扩展验证范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用Asp.Net 2.0。我有一种情况,我需要检查对任何两个范围的用户输入。对于如我需要核对的范围100-200 500-600或一个文本框的值。我知道我可以连接2 Asp.Net RangeValidators到TextBox,但会尝试验证输入对阵双方的范围内,一个与条件,如果你愿意。的CustomValidator是一种选择,但我怎么会从服务器端传递2个范围值。是否有可能延长RangeValidator控件来解决这个特定的问题?

I'm using Asp.Net 2.0. I have a scenario where i need to check a user input against any of two ranges. For e.g. I need to check a textbox value against ranges 100-200 or 500-600. I know that i can hook up 2 Asp.Net RangeValidators to the TextBox, but that will try to validate the input against both the ranges, an AND condition,if you will. CustomValidator is an option, but how would I pass the 2 ranges values from the server-side. Is it possible to extend the RangeValidator to solve this particular problem?

[更新]对不起,我没有提到这一点,我的问题是,范围可能会有所不同。并且还在页面中不同的控制将根据一些条件不同的范围。我知道一些JS变量或隐藏输入元素我可以容纳这些值,但它不会看起来很优雅。

[Update] Sorry I didn't mention this, the problem for me is that range can vary. And also the different controls in the page will have different ranges based on some condition. I know i can hold these values in some js variable or hidden input element, but it won't look very elegant.

推荐答案

我延长了BaseValidator实现这一目标。它相当简单,一旦你了解校验是如何工作的。我已经包括code的粗版本来演示如何可以做到。你要知道它适合我的问题(如int的应始终> 0),但你可以轻松地扩展它。

I extended the BaseValidator to achieve this. Its fairly simple once you understand how Validators work. I've included a crude version of code to demonstrate how it can be done. Mind you it's tailored to my problem(like int's should always be > 0) but you can easily extend it.

public class RangeValidatorEx : BaseValidator { protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer) { base.AddAttributesToRender(writer); if (base.RenderUplevel) { string clientId = this.ClientID; // The attribute evaluation funciton holds the name of client-side js function. Page.ClientScript.RegisterExpandoAttribute(clientId, "evaluationfunction", "RangeValidatorEx"); Page.ClientScript.RegisterExpandoAttribute(clientId, "Range1High", this.Range1High.ToString()); Page.ClientScript.RegisterExpandoAttribute(clientId, "Range2High", this.Range2High.ToString()); Page.ClientScript.RegisterExpandoAttribute(clientId, "Range1Low", this.Range1Low.ToString()); Page.ClientScript.RegisterExpandoAttribute(clientId, "Range2Low", this.Range2Low.ToString()); } } // Will be invoked to validate the parameters protected override bool ControlPropertiesValid() { if ((Range1High <= 0) || (this.Range1Low <= 0) || (this.Range2High <= 0) || (this.Range2Low <= 0)) throw new HttpException("The range values cannot be less than zero"); return base.ControlPropertiesValid(); } // used to validation on server-side protected override bool EvaluateIsValid() { int code; if (!Int32.TryParse(base.GetControlValidationValue(ControlToValidate), out code)) return false; if ((code < this.Range1High && code > this.Range1Low) || (code < this.Range2High && code > this.Range2Low)) return true; else return false; } // inject the client-side script to page protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (base.RenderUplevel) { this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "RangeValidatorEx", RangeValidatorExJs(),true); } } string RangeValidatorExJs() { string js; // the validator will be rendered as a SPAN tag on the client-side and it will passed to the validation function. js = "function RangeValidatorEx(val){ " + " var code=document.getElementById(val.controltovalidate).value; " + " if ((code < rangeValidatorCtrl.Range1High && code > rangeValidatorCtrl.Range1Low ) || (code < rangeValidatorCtrl.Range2High && code > rangeValidatorCtrl.Range2Low)) return true; else return false;}"; return js; } public int Range1Low { get { object obj2 = this.ViewState["Range1Low"]; if (obj2 != null) return System.Convert.ToInt32(obj2); return 0; } set { this.ViewState["Range1Low"] = value; } } public int Range1High { get { object obj2 = this.ViewState["Range1High"]; if (obj2 != null) return System.Convert.ToInt32(obj2); return 0; } set { this.ViewState["Range1High"] = value; } } public int Range2Low { get { object obj2 = this.ViewState["Range2Low"]; if (obj2 != null) return System.Convert.ToInt32(obj2); return 0; } set { this.ViewState["Range2Low"] = value; } } public int Range2High { get { object obj2 = this.ViewState["Range2High"]; if (obj2 != null) return System.Convert.ToInt32(obj2); return 0; } set { this.ViewState["Range2High"] = value; } } }

更多推荐

Asp.Net:扩展验证范围

本文发布于:2023-11-16 07:30:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1602241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Asp   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!