对RadioButton的ASP.net验证

时间:2015-12-28 18:49:17

标签: asp.net validation

我想要验证,如果我选择状态为拒绝,那么必须有一些评论(必修)。如果状态为Accept,则注释可能为空(不是强制性的)

enter image description here

我将如何在ASP.NET中执行此操作,请找到我的代码

 <tr> 
                        <td width="30%">
                            <b>Status:</b> 
                        </td>


                        <td>
                            <asp:RadioButton ID="lAccept" runat="server" AutoPostBack="True" 
                                CausesValidation="True" Text="Accept" />
                            <asp:RadioButton ID="lReject" runat="server" AutoPostBack="True" 
                                CausesValidation="True" Text="Reject " />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" 
                                ErrorMessage="Please Select it is Accepted or Rejected" ForeColor="Red"></asp:RequiredFieldValidator>

                        </tr>
                    <tr> 
                        <td width="30%">
                            <b>Qty Rejected:</b> 
                        </td>

                        <td>
                            <asp:TextBox ID="lRejectedQty" runat="server" CausesValidation="True"></asp:TextBox>
                            <%--  <asp:CustomValidator ID="CustomValidator1" runat="server" 
                                ErrorMessage="Only interger between 1 to 10000000 " ondisposed="Page_Load" 
                                oninit="Page_Load" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>--%>
                            <asp:RangeValidator ID="RangeValidator3" runat="server" 
                                ErrorMessage="Rejected Quantity must be in change of 1 to 10,000,000" 
                                ControlToValidate="lRejectedQty" Display="Dynamic" ForeColor="Red" 
                                MaximumValue="10000000" MinimumValue="1"></asp:RangeValidator>

                        </td>   

                    </tr> 
                    <tr> <td width="30%">
                            <b>Comments:</b> 
                        </td>


                        <td>
                            <TEXTAREA rows=5 cols=40  name="lComments" id="lComments"></TEXTAREA>
                        </td>   

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用div#five

MSDN:https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator(v=vs.110).aspx

CustomValidator

然后在您的代码隐藏中,您可以对其有效或无效进行更详细的检查

<asp:CustomValidator ID="CustomValidator1" runat="server"
    ErrorMessage="You must enter Comments if you choose to Reject."
    OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>

缺点是这需要 PostBack 到服务器,并且在大多数情况下将在其他类型之后进行验证。