-1

I have multiple TextBox on my page. I want to validate if atleast one TextBox value is not null or 0 in asp.net webform.

The TextBox id's are from txtvalue1 to txtvalue20. I tried manually but instead of doing manually, for looping could be the best option I think. How do I do that? Thanks!

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
Dungeon
  • 972
  • 2
  • 16
  • 32
  • In windows forms google how to loop through all controls in form or in a control (such as a panel) and how to check if a control is a textbox. In web forms, you can do similar in code behind if you place your textboxes in another control or form or you can use jquery to loop through. P.S. there tons of examples online – JustLearning Sep 21 '18 at 05:57
  • @mjwills asp.net webform. I updated the question – Dungeon Sep 21 '18 at 05:58
  • 1
    [Here](https://stackoverflow.com/questions/4863051/loop-through-textboxes) is an example how to Loop through all textboxes of your form – Shmosi Sep 21 '18 at 06:00
  • @JustLearning Got it – Dungeon Sep 21 '18 at 06:02

3 Answers3

1

Use reflection and do something like this (untested).

bool areOneOrMoreFieldsEmpty()
{
    var textboxControls = GetType().GetFields().Where(field => field.Name.StartsWith("txtvalue");

    foreach(var control in textboxControls)
    {
        var textValueProperty = control.GetProperty(nameof(TextBoxControl.Text));
        var stringValue = textValueProperty.GetValue(this, null) as string;       

        if (string.IsNullOrEmpty(stringValue) || stringValue == "0")
        {
              return false;
        }
    }
    return true;
}
1

You can use customvalidator:

In .aspx:

    <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:CustomValidator runat="server" ErrorMessage="Text must not be null or 0" ControlToValidate="txt1" OnServerValidate="TextBoxValidate" ForeColor="Red"  />

<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<asp:CustomValidator runat="server" ErrorMessage="Text must not be null or 0" ControlToValidate="txt2" OnServerValidate="TextBoxValidate" ForeColor="Red" />

<asp:TextBox ID="txt3" runat="server"></asp:TextBox>
<asp:CustomValidator runat="server" ErrorMessage="Text must not be null or 0" ControlToValidate="txt3" OnServerValidate="TextBoxValidate" ForeColor="Red"/>

<asp:Button ID="btnDoSomething" runat="server" Text="Do something" OnClick="btnDoSomething_Click" />

In .cs:

    protected void btnDoSomething_Click(object sender, EventArgs e)
{
    if (!Page.IsValid)
        return;

    //Do something
}

protected void TextBoxValidate(object source, ServerValidateEventArgs args)
{
    args.IsValid = (args.Value != null && args.Value != "0");
}
Fredrik
  • 111
  • 3
0

To loop through all buttons knowing just their names, you would have to use reflection, which can be cumbersome - but that's one direction you can research.

Other option is to have collection which you can iterate over:

Button[] myButtons = new Button[]{txtvalue1, ..., txtvalue20};

foreach(var button in myButtons)
{
    // do operations here..
}
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69