top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to get Value (Text) of dynamically created TextBox in ASP.Net using C# and VB.Net?

+1 vote
2,348 views
How to get Value (Text) of dynamically created TextBox in ASP.Net using C# and VB.Net?
posted Dec 31, 2015 by Sathaybama

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

HTML Markup
The HTML Markup consists of an ASP.Net Panel control and two Buttons, one for adding TextBoxes and other one for getting the values of the dynamically generated TextBoxes.

<asp:Panel ID="pnlTextBoxes" runat="server">
</asp:Panel>
<hr />
<asp:Button ID="btnAdd" runat="server" Text="Add New" OnClick="AddTextBox" />
<asp:Button ID="btnGet" runat="server" Text="Get Values" OnClick="GetTextBoxValues" />

Dynamically creating and adding TextBoxes on Button Click
C#

protected void AddTextBox(object sender, EventArgs e)
{
    int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
    this.CreateTextBox("txtDynamic" + index);
}

private void CreateTextBox(string id)
{
    TextBox txt = new TextBox();
    txt.ID = id;
    pnlTextBoxes.Controls.Add(txt);

    Literal lt = new Literal();
    lt.Text = "<br />";
    pnlTextBoxes.Controls.Add(lt);
}

VB.Net

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)
End Sub

Private Sub CreateTextBox(id As String)
    Dim txt As New TextBox()
    txt.ID = id
    pnlTextBoxes.Controls.Add(txt)

    Dim lt As New Literal()
    lt.Text = "<br />"
    pnlTextBoxes.Controls.Add(lt)
End Sub

Retaining the dynamic TextBoxes on PostBack

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

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 += 1
    Next
End Sub

Getting values of dynamically created TextBoxes

C#

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 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
answer Dec 31, 2015 by Shivaranjini
...