I have two textboxes and two comparevalidators. I want to ensure that one is lower than the other. Both text boxes also have to have required validation.
The code for one of the textboxs and its validator is
<asp:TextBox runat="server" ID="txtRiesgo_Total_Des" MaxLength="18" Visible="false"></asp:TextBox>
<asp:CompareValidator runat="server" ID="compareRiesgoDesdeHasta" ControlToValidate="txtRiesgo_Total_Des"
Font-Size="XX-Small" Type="Double" ControlToCompare="txtRiesgo_Total_Has" ErrorMessage="Desde < Hasta<br>"
Operator="LessThan" Display="Static">
</asp:CompareValidator>
答案 0 :(得分:0)
我同意CustomValidator是最好的方法,因为检查文本是int而不是字符串也是个好主意。
在你的aspx中:
<asp:CustomValidator ID="cvCompareInt" runat="server"
ErrorMessage="Must be lower"
ControlToValidate="txtRiesgo_Total_Des"
OnServerValidate="Riesgo_Total_Des_Validate"
Display="Dynamic" >
</asp:CustomValidator>
<asp:TextBox runat="server" ID="txtRiesgo_Total_Des" MaxLength="18" ></asp:TextBox>
<asp:TextBox runat="server" ID="txtRiesgo_Total_Has" MaxLength="18"></asp:TextBox>
<asp:Button runat="server" id="btnSubmit" Text="submit"/>
在你的代码背后:
protected void Riesgo_Total_Des_Validate(object source, ServerValidateEventArgs args)
{
int lowerNum;
int higherNum;
bool lower = int.TryParse(txtRiesgo_Total_Des.Text, out lowerNum);
bool higher = int.TryParse(txtRiesgo_Total_Has.Text, out higherNum);
if (lower && higher)
{
if (lowerNum >= higherNum)
{
args.IsValid = false;
}
}
}