Asp.Net:扩展验证范围 [英] Asp.Net : Extended range validation

查看:84
本文介绍了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:扩展验证范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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