仅使用一个单选按钮选项进行验证

时间:2012-03-14 16:30:57

标签: c# asp.net validation

我有一个有三个选择的radiobuttonlist。当用户点击“已提供”选项时,会在其下方打开两个文本框。选择该选项时需要这两个。如果标记了该选项,我该如何才能要求这两个选项,但如果不是,那么我是否仍然允许我处理该表格?我尝试使用ValidationGroup,但由于我还不熟悉开发,我想我错过了一些东西。任何指导将不胜感激,提前感谢!

<asp:RadioButtonList ID="rblCreat" runat="server" RepeatDirection="Horizontal" CssClass="rblMargin rblCreat">
    <asp:ListItem>N/A</asp:ListItem>
    <asp:ListItem>DIC to Obtain</asp:ListItem>
    <asp:ListItem>Provided</asp:ListItem>
</asp:RadioButtonList>
<div style="display: none;" id="provided-fields">
    <br />
    <p style="margin-left: 250px">
        Results:
        <asp:TextBox ID="txtCreatResults" runat="server" Width="99px" TabIndex="21" Height="22px"
            CssClass="margin"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvResults" runat="server" ControlToValidate="txtCreatResults"
            ErrorMessage="*Required" ValidationGroup="provided"></asp:RequiredFieldValidator>
        <br />
        Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtCreatDate" runat="server" Width="99px" TabIndex="22" onkeydown="return DateFormat(this, event.keyCode)"
            Height="22px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvDate" runat="server" ControlToValidate="txtCreatDate"
            ErrorMessage="*Required" ValidationGroup="provided"></asp:RequiredFieldValidator>
    </p>
</div>

3 个答案:

答案 0 :(得分:1)

您的代码背后

protected void Page_Load(object sender, EventArgs e)
{
    rblCreat.Items[0].Attributes.Add("onclick", "abc('1');");
    rblCreat.Items[1].Attributes.Add("onclick", "abc('2');");
    rblCreat.Items[2].Attributes.Add("onclick", "abc('3');");
}

您的Java脚本

<script language="javascript" type="text/javascript">
    function abc(ID) {
        if (ID == '3') {
            var btn = document.getElementById("<%=btn.ClientID%>");
            btn.onclick = function () {
                WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(btn.id, "", true, "provided", "", false, false));
            }
            document.getElementById('providedfields').style.display = 'block';
        }
        if (ID == '1' || ID == '2') {
            var btn = document.getElementById("<%=btn.ClientID%>");
            btn.onclick = function () {
                WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(btn.id, "", true, "newValGroup", "", false, false));
            }
            document.getElementById('providedfields').style.display = 'none';
        }
    }
</script>

您的HTML

<asp:radiobuttonlist id="rblCreat" runat="server" repeatdirection="Horizontal" cssclass="rblMargin rblCreat">
        <asp:ListItem Value="1">N/A</asp:ListItem>
        <asp:ListItem Value="2">DIC to Obtain</asp:ListItem>
        <asp:ListItem Value="3">Provided</asp:ListItem>
    </asp:radiobuttonlist>
<div style="display: none;" id="providedfields">
    <br />
    <p style="margin-left: 250px">
        Results:
        <asp:textbox id="txtCreatResults" runat="server" width="99px" tabindex="21" height="22px"
            cssclass="margin"></asp:textbox>
        <asp:requiredfieldvalidator id="rfvResults" runat="server" controltovalidate="txtCreatResults"
            errormessage="*Required" validationgroup="provided"></asp:requiredfieldvalidator>
        <br />
        Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:textbox id="txtCreatDate" runat="server" width="99px" tabindex="22" height="22px"></asp:textbox>
        <asp:requiredfieldvalidator id="rfvDate" runat="server" controltovalidate="txtCreatDate"
            errormessage="*Required" validationgroup="provided"></asp:requiredfieldvalidator>
    </p>
</div>
<asp:button id="btn" validationgroup="provided" runat="server" />

当您点击按钮时,如果有其他提供的选项,它将回发。

答案 1 :(得分:0)

我相信CustomValidator可以为您提供帮助。

<asp:CustomValidator 
runat="server" 
id="cusCustom" 
controltovalidate="txtCreatDate" 
onservervalidate="cusCustom_ServerValidate" 
errormessage="You must enter a text." />

然后使用你可能需要的任何逻辑在你的代码中编写方法。

 protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        if (rblCreat.SelectedIndex == 1)
        {
            if (e.Value.Length > 0)
            {
                e.IsValid = true;
            }
            else
            {
                e.IsValid = false;
            }
        }
        else {
            e.IsValid = true;
        }
    }

注意:这不是在任何IDE中编写的,因此我无法确定语法。

答案 2 :(得分:0)

在处理表单之前,您可以(从服务器代码)检查是否选中了radiobuttonlist中的必需选项。如果不是,请将requiredfieldvalidators的Enabled属性设置为False。