开发者

Required field validator for multiple dropdown lists in an aspx page

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 control开发者_开发百科s 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.


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


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);
}


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜