简体   繁体   中英

Require some text boxes to be filled in but not all

I am creating a survey, it has 60 textboxes I need to require that at least 40 of these textboxes are filled in upon hitting the submit button, it doesn't matter which ones as long as 40 have text within them. Any ideas how to accomplish this with C# or maybe another way?

A few of my textboxes:

   <div class="row">
        <div class="span3">
            <div class="control-group">
                <asp:Label ID="SupperLbl" class="control-label required" runat="server" Text="Best Supper Club"></asp:Label>
                <div class="controls">
                    <asp:TextBox ID="SupperTxtBox" class="span3" runat="server"></asp:TextBox>
                </div>
            </div>
        </div>
        <div class="span3">
            <div class="control-group">
                <asp:Label ID="YogurtLbl" class="control-label required" runat="server" Text="Best Place for Frozen Yogurt"></asp:Label>
                <div class="controls">
                    <asp:TextBox ID="YogurtTxtBox" class="span3" runat="server"></asp:TextBox>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="span3">
            <div class="control-group">
                <asp:Label ID="Label1" class="control-label required" runat="server" Text="Best Place for Dessert"></asp:Label>
                <div class="controls">
                    <asp:TextBox ID="DessertTxtBox" class="span3" runat="server"></asp:TextBox>
                </div>
            </div>
        </div>
        <div class="span3">
            <div class="control-group">
                <asp:Label ID="Label2" class="control-label required" runat="server" Text="Best Place for Chicken"></asp:Label>
                <div class="controls">
                    <asp:TextBox ID="ChickenTxtBox" class="span3" runat="server"></asp:TextBox>
                </div>
            </div>
        </div>
    </div>
    .....

Place your textboxes in an array, then do some validation.

//Place textboxes in array for easy access
TextBox[] validatedTexboxes = new TextBox[] {
     textbox1, textbox2, textbox3, ...
};

//On submit, make sure 40 are filled in.
var filledTextboxes = validatedTexboxes.Count(x => !String.IsNullOrWhiteSpace(x.Text));

if (filledTextboxes > 40)
    //Do Something
string[] txtArr = { "SupperTxtBox", "YogurtTxtBox", "DessertTxtBox" };
protected void Page_Load(object sender, EventArgs e)
{
    string[] lblArr = { "Best Supper Club", "Best Place for Frozen Yogurt", "Best Place for Dessert" };
    for (int i = 0; i < lblArr.Length; i++)
    {
        Label lbl = new Label();
        lbl.Text = "<br>" + lblArr[i] + "<br>";
        TextBox txt = new TextBox();
        txt.ID = txtArr[i];
        Form.Controls.Add(lbl);
        Form.Controls.Add(txt);
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    int Count = 0;
    foreach (string item in txtArr)
    {
        TextBox t = (TextBox)Form.FindControl(item);
        if (t != null)
        {
            if (t.Text.Trim() != "")
                Count++;
        }
    }
    if (Count < 3)
    {
        Response.Write("<br>You fill " + Count + " textbox, Please fill 3 textbox!");
    }
}

If you want the validation on the server side, you can loop recursively on all the controls of the page, take the textboxes and verify if at least 40 contains text.

For the recursive loop, you can use the extension method from this answer :

public static IEnumerable<T> FindControls<T>(this Control control, bool recurse) where T : Control
{
    List<T> found = new List<T>();
    Action<Control> search = null;
    search = ctrl =>
        {
            foreach (Control child in ctrl.Controls)
            {
                if (typeof(T).IsAssignableFrom(child.GetType()))
                {
                    found.Add((T)child);
                }
                if (recurse)
                {
                    search(child);
                }
            }
        };
    search(control);
    return found;
}

All you need to do now is get the array and verify that at least 40 of them contain text :

private bool AreAtLeast40TextBoxesFilled()
{
    var allTextBoxes = this.Page.FindControls<TextBox>(true);
    return allTextBoxes.Count(t => !string.IsNullOrWhiteSpace(t.Text)) >= 40;
}

Use Phil Haack's tutorials on MVC list model binding so you can keep track of the value and the id of the textbox in a list and then check the count on the server side.

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

If the total count of elements in the list is less than your limit than return a modelstate validation error and return the same view.

If you need a specific example let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM