Same way you need to add the RequiredFiledValidator object to the panel pnlTextBoxes whenever you create the textboxes as per there type values and sets there properties like you are setting CssClass property based on their Fieldtype. Refer the below Sample code for your reference and implement it as per your code logic
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
input[type=text]
{
margin-bottom: 10pt;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="pnlTextBoxes" runat="server">
</asp:Panel>
<hr />
<asp:Button ID="btnAdd" runat="server" Text="Add New" OnClick="AddTextBox" CausesValidation="false" />
<asp:Button ID="btnGet" runat="server" Text="Get Values" OnClick="GetTextBoxValues" />
</form>
</body>
</html>
C#
protected void Page_PreInit(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
int i = 1;
foreach (string key in keys)
{
this.CreateTextBox("txtDynamic" + i, i);
i++;
}
}
protected void AddTextBox(object sender, EventArgs e)
{
int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
this.CreateTextBox("txtDynamic" + index, index);
}
private void CreateTextBox(string id, int index)
{
TextBox txt = new TextBox();
txt.ID = id;
pnlTextBoxes.Controls.Add(txt);
RequiredFieldValidator req = new RequiredFieldValidator();
req.ID = "rfvDynamic" + index;
req.Font.Bold = true;
req.SetFocusOnError = true;
req.ErrorMessage = "Required";
req.ControlToValidate = id;
pnlTextBoxes.Controls.Add(req);
Literal lt = new Literal();
lt.Text = "<br />";
pnlTextBoxes.Controls.Add(lt);
}
protected void GetTextBoxValues(object sender, EventArgs e)
{
string message = "";
foreach (TextBox textBox in pnlTextBoxes.Controls.OfType<TextBox>())
{
message += textBox.ID + ": " + textBox.Text + "\\n";
}
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);
}
VB.Net
Protected Sub Page_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
Dim keys As List(Of String) = Request.Form.AllKeys.Where(Function(key) key.Contains("txtDynamic")).ToList()
Dim i As Integer = 1
For Each key As String In keys
Me.CreateTextBox("txtDynamic" & i, i)
i += 1
Next
End Sub
Protected Sub AddTextBox(sender As Object, e As EventArgs)
Dim index As Integer = pnlTextBoxes.Controls.OfType(Of TextBox)().ToList().Count + 1
Me.CreateTextBox("txtDynamic" & index, index)
End Sub
Private Sub CreateTextBox(id As String, index As Integer)
Dim txt As New TextBox()
txt.ID = id
pnlTextBoxes.Controls.Add(txt)
Dim req As New RequiredFieldValidator()
req.ID = "rfvDynamic" & index
req.Font.Bold = True
req.SetFocusOnError = True
req.ErrorMessage = "Required"
req.ControlToValidate = id
pnlTextBoxes.Controls.Add(req)
Dim lt As New Literal()
lt.Text = "<br />"
pnlTextBoxes.Controls.Add(lt)
End Sub
Protected Sub GetTextBoxValues(sender As Object, e As EventArgs)
Dim message As String = ""
For Each textBox As TextBox In pnlTextBoxes.Controls.OfType(Of TextBox)()
message += textBox.ID + ": " + textBox.Text + "\n"
Next
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
Screenshot