【问题标题】:ASP .net validation techniquesASP .net 验证技术
【发布时间】:2012-08-09 16:22:58
【问题描述】:

过去 1.5 年我在 ASP.net MVC 工作。我使用企业应用程序块进行服务器端验证的地方。我喜欢视图模型绑定到视图控件和验证以这种方式工作的方式。但现在我正在从事一个没有 MVC 的纯网络表单项目。

这里 jQuery 用于客户端验证,根本没有服务器端验证。我强调了服务器端验证的重要性,并计划使用企业库来做同样的事情。

由于某些原因(可能是由于我最近在 ASP.NET MVC 中工作的事实),我遇到了困难。

在我的 webforms 应用程序中,我的验证必须包含客户端和服务器端的相同逻辑。或者一般来说,在 ASP .net Web 表单中进行编码验证的最佳实践是什么?

我想遵循被广泛接受的做法。还有比企业库更好的网络表单验证新概念。示例应该可以帮助我理解。

【问题讨论】:

    标签: asp.net .net validation webforms


    【解决方案1】:

    我认为您希望验证更多而不是更少...除了客户端检查之外,服务器端检查是一个好主意(尤其是对于面向外部的应用程序)。你要特别小心文本框。请记住,用户可以关闭脚本(并完全绕过您的客户端脚本)。

    使用触发 ServerValidate 事件的自定义验证器控件很容易实现服务器端验证。

    我没有使用过企业库,所以我无法回答有关它提供的验证例程的任何信息。

    【讨论】:

      【解决方案2】:

      您可以将 CustomValidators 用于任何事情,它们是我的最爱!

      如果您使用像 required="required" 这样的 HTML5 属性,您可以免费获得客户端反馈。

      您也可以使用它们来执行服务器端验证,如下所示:

      <asp:ValidationSummary runat="server" id="vSummary" />
      
      <asp:TextBox runat="server" id="txtReq" required="required" />
      <asp:DropDownList runat="server" id="ddlReq" required="required">
          <asp:ListItem text="..." value="" />
          <asp:ListItem text="Yes" value="1" />
          <asp:ListItem text="No" value="0" />
      </asp:DropDownList>
      
      <asp:Button runat="server" id="cmdSubmit" text="Submit" />
      

      函数背后的代码:

      private void buildRequiredWebControls(List<WebControl> lst, Control c)
      {
          if (c is WebControl)
              if (String.Compare((c as WebControl).Attributes["required"] ?? String.Empty, "required", true) == 0)
                  lst.Add((c as WebControl));
      
          foreach (Control ch in c.Controls)
              buildRequiredWebControls(lst, ch);
      }
      
      /* --------------------------------------------- */
      
      private Boolean addAllFieldsRequired(List<WebControl> controls)
      {
          foreach (WebControl w in controls)
          {
              if (w as TextBox != null)
                  if (String.IsNullOrWhiteSpace((w as TextBox).Text)) return false;
      
              if (w as DropDownList != null)
                  if (String.IsNullOrWhiteSpace((w as DropDownList).SelectedValue)) return false;
          }
          return true;
      }
      
      /* --------------------------------------------- */
      
      private void cmdSubmit_Click(object sender, EventArgs e)
      {
          vSummary.ValidationGroup = "StackOverflow";
          Page.Validate("StackOverflow");
      
          List<WebControl> requiredFields = new List<WebControl>();
          this.buildRequiredWebControls(requiredFields, this);
      
          Page.Validators.Add(new CustomValidator()
          {
              IsValid = this.addAllFieldsRequired(requiredFields),
              ErrorMessage = "Please complete all required fields.",
              ValidationGroup = "StackOverflow"
          });
      
          if (Page.IsValid)
          {
              //Good to Go on Required Fields
          }
      }
      

      击败了非常单调的替代方案,即在每个控件之后手动将它们插入到 html 中:

      <asp:ValidationSummary runat="server" id="vSummary" ValidationGroup="StackOverflow" />
      
      <asp:TextBox runat="server" id="txtReq" required="required" />
      <asp:RequiredFieldValidator runat="server" ControlToValidate="txtReq" ErrorMessage="Please Fill Out Required Field" Text="*" ValidationGroup="StackOverflow" />
      
      <asp:DropDownList runat="server" id="ddlReq" required="required">
          <asp:ListItem text="..." value="" />
          <asp:ListItem text="Yes" value="1" />
          <asp:ListItem text="No" value="0" />
      </asp:DropDownList>
      <asp:RequiredFieldValidator runat="server" ControlToValidate="ddlReq" ErrorMessage="Please Fill Out Required Field" Text="*" ValidationGroup="StackOverflow" />
      
      <asp:Button runat="server" id="cmdSubmit" text="Submit" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-12
        • 2014-06-14
        相关资源
        最近更新 更多