ASP。如果复选框被选中,就需要一个文本框

时间:2022-05-19 06:31:39

Whats the best way to make a textbox required if a checkbox is checked?

如果复选框被选中,使文本框成为必需的最佳方式是什么?

I figure I could write a custom validator, but I was hoping to avoid a full post back to check the validation if possible... I was thinking AJAX had something built in for this scenario, but I've been unable to find it. I'm thinking straight Javascript would also be a solution, but I could use a head start if that's the best approach.

我认为我可以编写一个自定义验证器,但是我希望尽可能避免返回一个完整的post来检查验证……我以为AJAX为这个场景构建了一些东西,但我一直找不到。我认为纯Javascript也是一个解决方案,但如果这是最好的方法,我可以使用一个开头。

Thanks for any info.

谢谢你的任何信息。

6 个解决方案

#1


20  

The JavaScript to handle this isn't very difficult.

处理这个问题的JavaScript并不难。

Given the following ASP controls:

给定以下ASP控件:

<asp:TextBox ID="txtSubject" runat="server" />
<asp:CheckBox ID="chkSubjectRequired" runat="server" OnClick="updateValidator();" />
<asp:RequiredFieldValidator ID="rfvSubject" ControlToValidate="txtSubject" ErrorMessage="You must enter a subject." runat="server" />

Add the following JavaScript function:

添加以下JavaScript函数:

<script language="javascript" type="text/javascript">
    function updateValidator() {
        var enableValidator = !event.srcElement.status;
        var rfvSubject = document.getElementById('rfvSubject');
        ValidatorEnable(rfvSubject, enableValidator);
    }
</script>

That's all there is to it. You will also want to add the following code to your Page Load event, so that if the user has JavaScript disabled, your required field validator is still turned on or off properly:

这就是全部。您还需要将以下代码添加到页面加载事件中,以便如果用户禁用了JavaScript,您需要的字段验证器仍然可以正常打开或关闭:

rfvSubject.Enabled = chkSubjectRequired.Checked

#2


2  

You could make a custom validator, and then wrap those two controls in an UpdatePanel. That would turn it into an AJAX call for you. Kinda a waste, but it saves you having to write the JavaScript yourself.

您可以创建一个自定义验证器,然后将这两个控件封装到UpdatePanel中。这会将它转换为AJAX调用。这有点浪费,但你不必自己编写JavaScript。

Also, if you hate writing JS as much as I do, you should try jQuery instead.

此外,如果您像我一样讨厌编写JS,那么应该尝试jQuery。

#3


1  

There is already a customvalidator validator control, which can fire a client-side javascript method to evaluate the value, or a server-side method to compare the values.

已经有了一个customvalidator验证器控件,它可以触发一个客户端javascript方法来评估值,或者一个服务器端方法来比较值。

This has an example: http://msdn.microsoft.com/en-us/library/a0z2h4sw%28VS.80%29.aspx Client property explained here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx Server event here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.servervalidate.aspx

这里有一个示例:http://msdn.microsoft.com/en-us/library/a0z2h4sw%28VS.80%29.aspx客户端属性,解释如下

You can put code in to cross-reference the checkbox value.

您可以输入代码来交叉引用复选框的值。

HTH.

HTH。

#4


1  

To solve this all within ASP.Net, make the checkbox do a postback:

在ASP中解决这些问题。Net,让复选框做回发:

<asp:CheckBox 
     ID="Existing" 
     runat="server" 
     Text="Conditional ValidatorVal" 
     AutoPostBack="True" 
     OnCheckedChanged="Existing_CheckedChanged"
 />

Then on the code-behind, enable or disable the validators:

然后在代码后,启用或禁用验证器:

protected void Existing_CheckedChanged(object sender, EventArgs e)
{
     RequiredFieldValidator1.Enabled =! Existing.Checked;
}

#5


0  

You must use CustomValidator and use ClientIDMode="Static" in checkbox and textbox.

您必须使用CustomValidator并在复选框和文本框中使用ClientIDMode="Static"。

<asp:TextBox ID="txtSubject" ClientIDMode="Static" runat="server" />
<asp:CheckBox ID="chkSubjectRequired" ClientIDMode="Static" runat="server" />
<asp:CustomValidator ID="valid1" runat="server" 
           ClientValidationFunction="validateCheckboxCheck" 
           ErrorMessage="You must write anything.">
</asp:CustomValidator>

And write below script tag for function (require jQuery)

在函数的脚本标签下面写(需要jQuery)

<script type="text/javascript">
function validateCheckboxCheck(source, args) {
            if ($("#chkSubjectRequired").is(":checked")) {
                if ($("#txtSubject").val()==="") {
                    // return false for error message
                    args.IsValid = false;
                } else {
                    // return true
                    args.IsValid = true;
                }
            } else {
                // return true
                args.IsValid = true;
            }
        }
</script>

#6


-2  

You'll need to check for it in whatever validation routine you're currently using, both client and server-side.

您需要在当前使用的任何验证例程中检查它,包括客户端和服务器端。

#1


20  

The JavaScript to handle this isn't very difficult.

处理这个问题的JavaScript并不难。

Given the following ASP controls:

给定以下ASP控件:

<asp:TextBox ID="txtSubject" runat="server" />
<asp:CheckBox ID="chkSubjectRequired" runat="server" OnClick="updateValidator();" />
<asp:RequiredFieldValidator ID="rfvSubject" ControlToValidate="txtSubject" ErrorMessage="You must enter a subject." runat="server" />

Add the following JavaScript function:

添加以下JavaScript函数:

<script language="javascript" type="text/javascript">
    function updateValidator() {
        var enableValidator = !event.srcElement.status;
        var rfvSubject = document.getElementById('rfvSubject');
        ValidatorEnable(rfvSubject, enableValidator);
    }
</script>

That's all there is to it. You will also want to add the following code to your Page Load event, so that if the user has JavaScript disabled, your required field validator is still turned on or off properly:

这就是全部。您还需要将以下代码添加到页面加载事件中,以便如果用户禁用了JavaScript,您需要的字段验证器仍然可以正常打开或关闭:

rfvSubject.Enabled = chkSubjectRequired.Checked

#2


2  

You could make a custom validator, and then wrap those two controls in an UpdatePanel. That would turn it into an AJAX call for you. Kinda a waste, but it saves you having to write the JavaScript yourself.

您可以创建一个自定义验证器,然后将这两个控件封装到UpdatePanel中。这会将它转换为AJAX调用。这有点浪费,但你不必自己编写JavaScript。

Also, if you hate writing JS as much as I do, you should try jQuery instead.

此外,如果您像我一样讨厌编写JS,那么应该尝试jQuery。

#3


1  

There is already a customvalidator validator control, which can fire a client-side javascript method to evaluate the value, or a server-side method to compare the values.

已经有了一个customvalidator验证器控件,它可以触发一个客户端javascript方法来评估值,或者一个服务器端方法来比较值。

This has an example: http://msdn.microsoft.com/en-us/library/a0z2h4sw%28VS.80%29.aspx Client property explained here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx Server event here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.servervalidate.aspx

这里有一个示例:http://msdn.microsoft.com/en-us/library/a0z2h4sw%28VS.80%29.aspx客户端属性,解释如下

You can put code in to cross-reference the checkbox value.

您可以输入代码来交叉引用复选框的值。

HTH.

HTH。

#4


1  

To solve this all within ASP.Net, make the checkbox do a postback:

在ASP中解决这些问题。Net,让复选框做回发:

<asp:CheckBox 
     ID="Existing" 
     runat="server" 
     Text="Conditional ValidatorVal" 
     AutoPostBack="True" 
     OnCheckedChanged="Existing_CheckedChanged"
 />

Then on the code-behind, enable or disable the validators:

然后在代码后,启用或禁用验证器:

protected void Existing_CheckedChanged(object sender, EventArgs e)
{
     RequiredFieldValidator1.Enabled =! Existing.Checked;
}

#5


0  

You must use CustomValidator and use ClientIDMode="Static" in checkbox and textbox.

您必须使用CustomValidator并在复选框和文本框中使用ClientIDMode="Static"。

<asp:TextBox ID="txtSubject" ClientIDMode="Static" runat="server" />
<asp:CheckBox ID="chkSubjectRequired" ClientIDMode="Static" runat="server" />
<asp:CustomValidator ID="valid1" runat="server" 
           ClientValidationFunction="validateCheckboxCheck" 
           ErrorMessage="You must write anything.">
</asp:CustomValidator>

And write below script tag for function (require jQuery)

在函数的脚本标签下面写(需要jQuery)

<script type="text/javascript">
function validateCheckboxCheck(source, args) {
            if ($("#chkSubjectRequired").is(":checked")) {
                if ($("#txtSubject").val()==="") {
                    // return false for error message
                    args.IsValid = false;
                } else {
                    // return true
                    args.IsValid = true;
                }
            } else {
                // return true
                args.IsValid = true;
            }
        }
</script>

#6


-2  

You'll need to check for it in whatever validation routine you're currently using, both client and server-side.

您需要在当前使用的任何验证例程中检查它,包括客户端和服务器端。