aspx 页面中多个下拉列表的必填字段验证器

发布于 2024-09-28 15:08:39 字数 225 浏览 5 评论 0原文

我有一个 aspx 页面,其中有 18 个(是的 18 个)下拉列表和 18 个文本框。需要选择每个下拉列表并填充每个文本框。在这 36 个控件上拖放所需的字段验证器并维护它们是一项痛苦的任务,而且似乎不是一个合乎逻辑的选项,因为我需要的只是让用户从下拉列表中选择一个值。

无论如何,我是否可以循环遍历所有这些下拉控件和文本框控件,检查它们是否为空并相应地向用户显示警告?客户端验证解决方案或服务器端验证解决方案适合我。

I have an aspx page which has 18 (yes 18) dropdown lists and 18 text boxes. Each dropdown needs to be selected and each textbox needs to be filled. Dragging and dropping required field validators on these 36 controls and maintaining them is a painful task and does not seem to be the logical option as all I need is for the user to select a value from the dropdown.

Is there anyway I can loop through all these dropdown controls and textbox controls, check if they are empty and display warnings to users accordingly? Client-side validation solution or server side validation solution is fine with me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放赐 2024-10-05 15:08:39

使用 CustomValidator 并具有客户端脚本函数,确保每个文本框/下拉列表都有一个值。

Use a CustomValidator and have a client script function that makes sure every text box/drop down has a value.

忘你却要生生世世 2024-10-05 15:08:39

一项建议是循环遍历所有控件 在页面中,使用递归函数动态绑定RequiredFieldValidator 添加到找到的控件。您可以调整我的代码以满足您的需求。

不过,此代码有一些缺点:

  1. 使用 control.ID 而不是关联的标签文本
  2. 将RequiredFieldValidator 添加到page.controls 将修改其ControlCollection。这将破坏 foreach 方法。因此,我只能将RequiredFieldValidator添加到Panel中。

.aspx

<asp:Panel ID="pnlValidation" runat="server">
</asp:Panel>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" />
<asp:DropDownList ID="DropDownList2" runat="server" />
<asp:DropDownList ID="DropDownList3" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />

.cs

protected void Page_Load(object sender, EventArgs e)
{
    AddValidator(this);
}

private void AddValidator(Control ctrl)
{
    if (ctrl is TextBox || ctrl is DropDownList)
    {
        RequiredFieldValidator rfv = new RequiredFieldValidator();
        rfv.ControlToValidate = ctrl.ID;
        rfv.Display = ValidatorDisplay.Dynamic;
        rfv.ErrorMessage = ctrl.ID + " is required<br />";
        pnlValidation.Controls.Add(rfv);
    }

    foreach (Control subctrl in ctrl.Controls)
        AddValidator(subctrl);
}

One suggestion is to loop through all the controls on the page, use recursive function to dynamically bind RequiredFieldValidator to the found controls. You can tweak my code to suit your needs.

This code has some drawbacks though:

  1. Use control.ID instead of associated label text
  2. Adding RequiredFieldValidator to the page.controls will modify its ControlCollection. This will break the foreach method. Thus, I can only add RequiredFieldValidator to Panel instead.

.aspx

<asp:Panel ID="pnlValidation" runat="server">
</asp:Panel>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" />
<asp:DropDownList ID="DropDownList2" runat="server" />
<asp:DropDownList ID="DropDownList3" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />

.cs

protected void Page_Load(object sender, EventArgs e)
{
    AddValidator(this);
}

private void AddValidator(Control ctrl)
{
    if (ctrl is TextBox || ctrl is DropDownList)
    {
        RequiredFieldValidator rfv = new RequiredFieldValidator();
        rfv.ControlToValidate = ctrl.ID;
        rfv.Display = ValidatorDisplay.Dynamic;
        rfv.ErrorMessage = ctrl.ID + " is required<br />";
        pnlValidation.Controls.Add(rfv);
    }

    foreach (Control subctrl in ctrl.Controls)
        AddValidator(subctrl);
}
想挽留 2024-10-05 15:08:39

如果您动态生成文本框和下拉列表,您可能还想动态生成验证控件,但如果所有下拉列表和文本框都是静态的,您可以使用以下内容:

使用 CustomValidator Web 控件,编写客户端 javascript检查下拉列表和文本框的所有属性并使用该函数配置 Web 控件的 ClientValidationFunction 并设置 EnableClientScript=true 的方法。另外,b/c 并非所有用户都启用了 javascript,而且为了确保这是最佳实践,始终还创建一个服务器端验证函数并在提交操作上调用 Page.IsValid()。

.aspx 示例代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" 

Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function ValidateMe(sender, args) {
            var txt = document.getElementById("txt");
            if (txt.value != "")
                args.IsValid = true;
            else
                args.IsValid = false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox id="txt" runat="server" />
        <asp:CustomValidator ClientValidationFunction="ValidateMe" ID="custval" 
            runat="server" ErrorMessage="Fail" onservervalidate="custval_ServerValidate" />
        <asp:Button ID="btn" runat="server" Text="push" onclick="btn_Click1" />
    </form>
</body>
</html>

c# 代码隐藏示例代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Threading;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

        }


    }

    protected void btn_Click1(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            btn.Text = "PASS";
        }
        else
        {
            btn.Text = "FAIL";
        }
    }
    protected void custval_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (txt.Text != "")
            custval.IsValid = true;
        else
            custval.IsValid = false;
    }
}

If you are dynamically generating the textboxes and dropdownlists, you would probably want to dynamically generate the validation controls as well, but if all the drop down lists and textboxes are static you can use the following:

Use a CustomValidator Web Control, write client side javascript method that checks all the properties of the drop down lists and the textboxes and configure the web control's ClientValidationFunction with the function and set EnableClientScript=true. Also, b/c not all users have javascript enabled, plus to be sure as it is best practice, always also create a server side validation function as well and call Page.IsValid() on the submit action.

.aspx Sample Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" 

Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function ValidateMe(sender, args) {
            var txt = document.getElementById("txt");
            if (txt.value != "")
                args.IsValid = true;
            else
                args.IsValid = false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox id="txt" runat="server" />
        <asp:CustomValidator ClientValidationFunction="ValidateMe" ID="custval" 
            runat="server" ErrorMessage="Fail" onservervalidate="custval_ServerValidate" />
        <asp:Button ID="btn" runat="server" Text="push" onclick="btn_Click1" />
    </form>
</body>
</html>

c# codebehind sample code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Threading;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

        }


    }

    protected void btn_Click1(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            btn.Text = "PASS";
        }
        else
        {
            btn.Text = "FAIL";
        }
    }
    protected void custval_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (txt.Text != "")
            custval.IsValid = true;
        else
            custval.IsValid = false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文