title: Make RequiredFieldValidator in aspx.cs (code behind) asp.net
body: <p>I have a TextBox where i add a number and at a buttonclick i create new textboxes depending on what number i put in my first TextBox. </p>
  
  <p>For example i put 5 in the TextBox and press button i get 5 new textboxes on my page.</p>
  
  <p>Now im trying to make a RequiredFieldValidator on all of this textboxes thats created but i can't get it to work. I have another button that makes an insert to my db and this values from all the new textboxes are required.</p>
  
  <p>Can someone take a look at my code to see if im on the right track and maybe help me to modify the code so it would work.</p>
  
  <pre><code>protected void btnGenerateControl_Click(object sender, EventArgs e)
      {
          int Count = Convert.ToInt32(Qty.Text);
          for (int i = 1; i &lt;= Count; i++)
          {
              Label lbl = new Label();
              TextBox txtbox = new TextBox();
              RequiredFieldValidator rfv = new RequiredFieldValidator();
              string ValidationGroup = "Group2";
              string ErrorMessage = "Bitte Flotten ID eingeben";
  
              lbl.Text = " Flotten ID " + (i).ToString() + " ";
              txtbox.EnableViewState = true;
              rfv.ControlToValidate = txtbox.Text;
  
              rfv.ErrorMessage = ErrorMessage;
              rfv.ValidationGroup = ValidationGroup;
              rfv.ForeColor = System.Drawing.Color.Red;
              Label lbl1 = new Label();
              TextBox txtbox1 = new TextBox();
  
              txtbox1.EnableViewState = true;
              pnlTextBoxes.Controls.Add(lbl);
              pnlTextBoxes.Controls.Add(new LiteralControl("&lt;input id='txt' name='FlottenID" + i + "'type='text'  /&gt;"));
              pnlTextBoxes.Controls.Add(lbl1);
              lbl1.Text = "  Bemerkungen: ";
              pnlTextBoxes.Controls.Add(new LiteralControl("&lt;input id='txt' name='Info" + i + "'type='text'  /&gt;"));
              pnlTextBoxes.Controls.Add(new LiteralControl("&lt;br /&gt;&lt;br /&gt;"));
          }
      }
  </code></pre>
  
  <hr>
  
  <p>Thanks for your help, the RequiredFieldValidator works now but at insert in my db the values from the txtbox and txtbox1 are empty. I make something wrong with the insert now. </p>
  
  <p>What do i have to Change there now? </p>
  
  <pre><code>protected void btnGenerateControl_Click(object sender, EventArgs e)
      {
          int Count = Convert.ToInt32(Qty.Text);
          for (int i = 1; i &lt;= Count; i++)
          {
  
              string ValidationGroup = "Group2";
              string ErrorMessage = "Bitte Flotten ID eingeben";
  
              Label lbl = new Label();
              lbl.Text = " Flotten ID " + (i).ToString() + " ";
  
              TextBox txtbox = new TextBox();
              txtbox.EnableViewState = true;
              txtbox.ID = "txt" + i;
  
              TextBox txtbox1 = new TextBox();
              txtbox.EnableViewState = true;
              txtbox.ID = "txt" + i;
  
  
              RequiredFieldValidator rfv = new RequiredFieldValidator();
              rfv.ControlToValidate = txtbox.ID;
              rfv.ErrorMessage = ErrorMessage;
              rfv.ID = "rfv" + i;
              rfv.ValidationGroup = ValidationGroup;
              rfv.ForeColor = System.Drawing.Color.Red;
  
              Label lbl1 = new Label();
              lbl1.Text = "  Bemerkungen: ";
  
  
  
  
              pnlTextBoxes.Controls.Add(lbl);
              //pnlTextBoxes.Controls.Add(new LiteralControl("&lt;input id='txt' name='FlottenID" + i + "'type='text'  /&gt;"));
              pnlTextBoxes.Controls.Add(txtbox);
              pnlTextBoxes.Controls.Add(rfv);
              pnlTextBoxes.Controls.Add(lbl1);
              pnlTextBoxes.Controls.Add(txtbox1);
  
              //pnlTextBoxes.Controls.Add(new LiteralControl("&lt;input id='txt' name='Info" + i + "'type='text'  /&gt;"));
              pnlTextBoxes.Controls.Add(new LiteralControl("&lt;br /&gt;&lt;br /&gt;"));
          }
      }
  
      protected void btnAddOrder_Click(object sender, EventArgs e)
      {
          int Count = Convert.ToInt32(Qty.Text);
          for (int i = 1; i &lt;= Count; i++)
          {
  
              String query = "insert into Orders (CustID, OrderDate, Time, ProductID, ProjectID, Status, FlottenID, Info)values('" + CustID.Text + "','" + OrderDate.Text + "','" + Time.Text + "','" + ProductID.Value + "','" + ProjectID.Value + "','" + Status.Value + "','" + Request.Form["FlottenID" + i.ToString()] + "','" + Request.Form["Info" + i.ToString()] + "')";
              String query1 = "commit;";
              DataLayer.DataConnector dat = new DataLayer.DataConnector("Provider=SQLOLEDB; data source=rzwsrv010;database=event;user ID=event;password=event; Persist Security Info=False");
              dat.DataInsert(query);
              dat.DataInsert(query1);
          }
  
          Response.Redirect("NewOrder.aspx");
      }
  </code></pre>