当“其他”时,如何验证所需的文本选项从下拉列表中选择? [英] How to validate required text when "Other" option is selected from a dropdownlist?

查看:147
本文介绍了当“其他”时,如何验证所需的文本选项从下拉列表中选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



来源[DropDownList]



网站



搜索引擎



其他



其他来源[TextBox]



我想使用ASP.Net验证器(我认为比较验证器),以便当其他在下拉列表中被选中,没有输入任何文本,验证被触发,页面无法提交。



这是可能吗?



我试图将下拉列表中的其他选项的值设置为string.empty,并将其与空文本框进行比较,但这不起作用。



我继承的全部是在向导控件中,否则我将连接一些客户端脚本来自己触发验证。我不认为我可以使用向导控件来执行此操作吗?



提前感谢

解决方案

ASP.NET提供的验证器都不允许您基于另一个控件执行条件验证。但是,您可以通过使用在客户端,服务器端或两者执行验证的CustomValidator来实现此目的(至少建议使用服务器端验证)。验证器与向导结合使用。



ASP.NET标记示例:

 < asp:DropDownList ID =OptionsDropDownListrunat =server> 
< asp:ListItem Text =网站/>
< asp:ListItem Text =搜索引擎/>
< asp:ListItem Text =Other/>
< / asp:DropDownList>
< asp:TextBox ID =OtherTextBoxrunat =server/>
< asp:CustomValidator ID =custvOptionsDropDownListrunat =serverControlToValidate =OptionsDropDownList
ValidateEmptyText =trueDisplay =DynamicClientValidationFunction =validateOtherTextBox
ErrorMessage =这是必填栏! OnServerValidate =ValidateOtherTextBox/>

ClientValidationFunction的JavaScript:

 < script type =text / javascriptlanguage =javascript> 
函数validateOtherTextBox(event,args){
var textbox = document.getElementById('<%= OtherTextBox.ClientID%>')。
if(args.Value =='Other')
args.IsValid =(textbox!='');
else
args.IsValid = true;
}
< / script>

OnServerValidate的代码背后:

  protected void ValidateOtherTextBox(object source,ServerValidateEventArgs args)
{
if(OptionsDropDownList.SelectedValue ==Other)
{
args.IsValid =(OtherTextBox.Text.Trim()!=);
}
}

请注意,您可以选择实现所需的任何内容。您可以完全跳过Javascript验证,并删除该代码和 ClientValidationFunction 属性。另外,请注意,Javascript通过使用ClientID属性来引用目标控件。这是必需的,因为ASP.NET在输出页面时分配不同的ID,并且您希望以这种方式将其提供给Javascript方法(在页面上查看源代码,您将看到控件名称具有额外的前缀等)。


I have the following on my website.

Source [DropDownList]

Website

Search Engine

Other

Other Source [TextBox]

I want to use the ASP.Net validators (I think the compare validator) so that when "Other" is selected in the dropdownlist and no text is entered the validation is triggered and the page cannot be submitted.

Is this possible?

Ive tried to set the value of the "Other" option in the drop down to string.empty and compare it to an empty text box but this didn't work.

The whole thing that I have inherited is inside a wizard control, otherwise I would hook up some client script to trigger the validation myself. I don't think I can do this with a wizard control?

Thanks in advance.

解决方案

None of the ASP.NET provided validators allow you to perform conditional validation based on another control. However, you can achieve this by using a CustomValidator that performs validation on the client-side, server-side, or both (at a minimum, server-side validation is recommended). The validators work well in conjunction with wizards.

ASP.NET markup example:

    <asp:DropDownList ID="OptionsDropDownList" runat="server">
        <asp:ListItem Text="Website" />
        <asp:ListItem Text="Search Engine" />
        <asp:ListItem Text="Other" />
    </asp:DropDownList>
    <asp:TextBox ID="OtherTextBox" runat="server" />
    <asp:CustomValidator ID="custvOptionsDropDownList" runat="server" ControlToValidate="OptionsDropDownList"
        ValidateEmptyText="true" Display="Dynamic" ClientValidationFunction="validateOtherTextBox"
        ErrorMessage="This field is required!" OnServerValidate="ValidateOtherTextBox" />

Javascript for ClientValidationFunction:

<script type="text/javascript" language="javascript">
    function validateOtherTextBox(event, args) {
        var textbox = document.getElementById('<%= OtherTextBox.ClientID %>').value;
        if (args.Value == 'Other')
            args.IsValid = (textbox != '');
        else
            args.IsValid = true;
    }
</script>

Code-Behind for OnServerValidate:

    protected void ValidateOtherTextBox(object source, ServerValidateEventArgs args)
    {
        if (OptionsDropDownList.SelectedValue == "Other")
        {
            args.IsValid = (OtherTextBox.Text.Trim() != "");
        }
    }

Note that it's your choice to implement whatever you need. You could completely skip Javascript validation and remove that code and the ClientValidationFunction attribute. Also, notice that the Javascript refers to the target control by using the ClientID property. This is needed since ASP.NET assigns a different ID when the page is output and you'll want it to be provided to the Javascript method in this manner (view source on the page and you'll see that the control name has an extra prefix etc.).

这篇关于当“其他”时,如何验证所需的文本选项从下拉列表中选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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