【问题标题】:Trouble with Custom Validator using C# in ASP.NET在 ASP.NET 中使用 C# 的自定义验证器出现问题
【发布时间】:2014-11-26 19:33:05
【问题描述】:

我需要为此分配使用自定义验证器,我必须解决的问题是:我必须验证在表单中输入州缩写的人是有效的美国州缩写(例如 AK、AL、AR , ETC。)。我没有看到任何语法错误,所以我的表单可以正常发布,但是当我进入无效状态(例如 ZZ)时,它不会给我应该自动发布的错误消息(在这种情况下,我的错误消息显示“请输入有效的美国州(使用全部大写))。这是我的控制:

     <asp:Label
 id="state"
 text="State:"
 runat="server" />
 <br />
<asp:TextBox
 id="valState"
 MaxLength="2"
 Width="20"
 Runat="server" />
 <asp:CustomValidator
 id="reqState"
 ControlToValidate="valState"
 OnServerValidate="stateArrayCheck"
 Text="Please enter a valid U.S. State (using all caps)"
 Runat="server" />

     <br /><br />

 <asp:Button
 id="btnSubmit"
 Text="Submit"
 Runat="server" />

我的活动的逻辑在这里:

    void stateArrayCheck (Object source, ServerValidateEventArgs args)
    {

                ArrayList stateList = new ArrayList();

                stateList.Add("AL");
                stateList.Add("AK");
                stateList.Add("AR");
                stateList.Add("AZ");
                stateList.Add("CA");
                stateList.Add("CO");
                stateList.Add("AL");
                stateList.Add("CT");
                stateList.Add("DE");
                stateList.Add("FL");
                stateList.Add("GA");
                stateList.Add("HI");
                stateList.Add("ID");
                stateList.Add("IL");
                stateList.Add("IN");
                stateList.Add("IA");
                stateList.Add("KS");
                stateList.Add("KY");
                stateList.Add("LA");
                stateList.Add("ME");
                stateList.Add("MD");
                stateList.Add("MA");
                stateList.Add("MI");
                stateList.Add("MN");
                stateList.Add("MO");
                stateList.Add("MS");
                stateList.Add("MT");
                stateList.Add("NC");
                stateList.Add("NE");
                stateList.Add("NH");
                stateList.Add("NJ");                    
                stateList.Add("NM");
                stateList.Add("NY");
                stateList.Add("ND");
                stateList.Add("OH");
                stateList.Add("OK");
                stateList.Add("OR");
                stateList.Add("PA");
                stateList.Add("RI");
                stateList.Add("SC");
                stateList.Add("SD");            
                stateList.Add("TN");
                stateList.Add("TX");
                stateList.Add("UT");
                stateList.Add("VA");
                stateList.Add("VT");
                stateList.Add("NM");
                stateList.Add("WA");
                stateList.Add("WY");                    


                for(int i=0; i <= stateList.Count; i++)
                {
                    if (valState.Text != stateList[i])

                        args.IsValid = false;
                        else
                        args.IsValid = true;

                }
          }








</script>

不太确定问题出在哪里,但我的其他验证器工作正常。它们只是简单的必需验证器,但如果您未在文本框中输入任何内容,则文本会按预期显示。任何帮助将非常感激。谢谢。

【问题讨论】:

  • 为什么不直接使用下拉列表?
  • 如果这是一个真实世界的网站,我可能会这样做。但这是给 schol 的,我们正在学习验证控件,所以我必须使用一个。

标签: c# asp.net


【解决方案1】:

for (int i = 0; i &lt;= stateList.Count; i++) 循环的这个逻辑是不正确的 - 它一直切换 args.IsValid 的值而不退出,直到它完成循环(并且它超出了上限)。找到有效状态后即可退出。

此外,如果验证方法 stateArrayCheck 位于代码后面(.aspx.cs,而不是 &lt;script runat="server"&gt; 标记),则您需要确保验证方法至少是受保护的范围。

您还可以考虑将状态常量列表移动到静态 HashSet - 因为它是常量,因此无需在每次验证调用时实例化它,并且像 HashSets 和 Dictionaries 这样的“键控”集非常适合快速、独特的查找。

然后您可以在一行中进行验证:

// Move the state list to a static / cached initialization
private static HashSet<string> StateList = new HashSet<string>
{
    "AL","AK", ...
};

// The check for valid state is now a simple lookup in the HashSet
protected void stateArrayCheck(Object source, ServerValidateEventArgs args)
{
    args.IsValid = StateList.Contains(valState.Text);
}

【讨论】:

    【解决方案2】:

    你很接近。正如 StuartLC 所说,即使您之前找到了匹配项,您也会再次将 IsValid 设置为 false。由于我假设您仍在学习 C#,因此请坚持您现在所掌握(并理解)的内容,并像这样适应:

    ArrayList stateList = new ArrayList();
    
    stateList.Add("AL");
    /* your entire list here */
    stateList.Add("WY");
    
    // expect to not find the state
    args.IsValid = false;
    
    // check if you can find the state in your list
    foreach (string state in stateList)
    {
        if (valState.Text == state){
            args.IsValid = true;
            break;  // no point in checking further elements, break the loop
        }
    }
    

    foreach 循环而不是for 循环只是个人喜好。但是,您不能使用 foreach 循环超出集合的范围。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      相关资源
      最近更新 更多