[英]ASP.Net: Validating Radio Button Input with C#
我在為單選按鈕列表創建自定義驗證器輸入時遇到了問題。 我已經將所有內容編碼完畢,並且所有內容運行都沒有問題。 當應該進行驗證時,什么也沒有發生。 我的其他驗證工作正常。
這是我的HTML代碼:
<asp:RadioButtonList ID="SalaryPaidByEFT" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" CssClass="width-100 radio">
<asp:ListItem Text="Yes" Value="true"></asp:ListItem>
<asp:ListItem Text="No" Value="false"></asp:ListItem>
<asp:ListItem Text="Not Applicable" Value="null" Selected="True"></asp:ListItem>
</asp:RadioButtonList>
<asp:CustomValidator ID="cvSalaryPaidByEFT" runat="server"
ErrorMessage="Salary Must Be Paid By EFT" ControlToValidate="SalaryPaidByEFT"
OnServerValidate="cvSalaryPaidByEFT_ServerValidate" CssClass="has-error"
Display="Dynamic"></asp:CustomValidator>
這是我的C#代碼:
protected void cvSalaryPaidByEFT_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = (this.SalaryPaidByEFT.SelectedIndex == 0);
}
任何人都可以發現我可能缺少的任何內容來指導我解決問題嗎?
嘗試兩件事:
this.SalaryPaidByEFT.SelectedItem.Text == "Yes"
要么
args.Value == "Yes"
您是否缺少對this.Validate()方法的調用並檢查頁面的IsValid屬性。.從msdn示例:
if (this.IsPostBack)
{
this.Validate();
if (!this.IsValid)
{
string msg = "";
// Loop through all validation controls to see which
// generated the errors.
foreach (IValidator aValidator in this.Validators)
{
if (!aValidator.IsValid)
{
msg += "<br />" + aValidator.ErrorMessage;
}
}
Label1.Text = msg;
}
}
對於自定義驗證程序,您可能缺少此 msdn指令上的步驟4。
步驟4-“在ASP.NET網頁代碼中添加測試以檢查有效性。有關詳細信息,請參見如何:以編程方式測試ASP.NET Server控件的有效性。”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.