Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8387067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:06:55+00:00 2026-06-09T18:06:55+00:00

I have a page where users can add multiple email address to the form.

  • 0

I have a page where users can add multiple email address to the form.
Clicking add with add another dynamic textbox via ajax
Clicking save will save the values in the textboxes but if there is a blank textbox then I want to remove it on save.

I have code to remvoe it and see that the control is no longer in the placeholder but the page does not seem to update to show this.

Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If(Not Page.IsPostBack)
        AddEmailAddressBoxesLoad()
    Else
         Dim button As Control = GetPostBackControl(Me.Page)

        If (button IsNot Nothing) Then
            If (button.ID = "btnAdd") Then
                AddEmailAddressBoxes()              
            ElseIf (button.ID.Contains("btnSave"))
                AddEmailAddressBoxesSave()
                RemoveTextBox()
            End If
        End If

    End If
End Sub



Public Function RemoveTextBox() As List(Of String)
    Dim emptyTextBoxList = New List(Of TextBox
                                    )
    For Each control As Control In phMultiEmailAddressBoxes.Controls
        If(TypeOf(control) Is TextBox)
            Dim textBox = CType(control, TextBox)
            If(String.IsNullOrEmpty(textBox.Text))                  
                emptyTextBoxList.Add(textBox)
            End If
        End If
    Next

    For Each textBox As TextBox In emptyTextBoxList
        phMultiEmailAddressBoxes.Controls.Remove(textBox)
    Next
End Sub

Add TextBox Method

Private Sub AddEmailAddressBoxes()
    Dim count As Integer

    If (ViewState("EmailAddressboxCount") IsNot Nothing)
        count = CType(ViewState("EmailAddressboxCount"), Integer)
    End If

    count = count + 1

    ViewState("EmailAddressboxCount") = count

    For i As Integer = 1 To count
        AddTextBox(i)
    Next
End Sub

Private Sub AddEmailAddressBoxesLoad()
    Dim count As Integer = 1

    If(TrustSettings.HREmailAddressList.Any())
        count = TrustSettings.HREmailAddressList.Count()
    End If

    ViewState("EmailAddressboxCount") = count

    For i As Integer = 1 To count
        AddTextBox(i)
    Next
End Sub

Private Sub AddTextBox(i As Integer)
    Dim divStart = new Literal()
    divStart.Text = "<div>"
    phMultiEmailAddressBoxes.Controls.Add(divStart)

    Dim textBox = New TextBox()
    textBox.ID = "txtEmailBox" + i.ToString()
    textBox.MaxLength = 200
    textBox.CssClass = "email"
    If(TrustSettings.HREmailAddressList.Any() And TrustSettings.HREmailAddressList.Count() >= i)
        textBox.Text = TrustSettings.HREmailAddressList(i-1)
    End If

    phMultiEmailAddressBoxes.Controls.Add(textBox)

    Dim divEnd = new Literal()
    divEnd.Text = "</div>"
    phMultiEmailAddressBoxes.Controls.Add(divEnd)
End Sub

Update Panel markup

<asp:UpdatePanel runat="server" ID="UpdatePanel1">
    <ContentTemplate>
       <asp:PlaceHolder ID="phMultiEmailAddressBoxes" runat="server"></asp:PlaceHolder>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnAdd" />
        <asp:AsyncPostBackTrigger ControlID="btnSave" />
    </Triggers>
</asp:UpdatePanel>

<asp:Button runat="server" ID="btnAdd" Text="Add new email address" CssClass="but_small_png" UseSubmitBehavior="False" />
<asp:Button runat="server" ID="btnSave" Text="Save" CssClass="but_small_png" UseSubmitBehavior="False" />

EDIT: @Yuriy Rozhovetskiy’s Code ported to VB

 protected sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        if (IsPostBack)
            for each value As String in DynamicTextBoxes
                Dim tb = new TextBox() with { .ID = value, .CssClass = "email" }
                phMultiEmailAddressBoxes.Controls.Add(tb)
            Next
        End If
    End Sub

private Property  DynamicTextBoxes() As List(Of string)
    Get
        Dim list = ViewState("DynamicTextBoxes")
            if (list Is Nothing)

                list = New List(Of string)()
                ViewState("DynamicTextBoxes") = list
            End If
            return list
    End Get
    Set

    End Set
End Property

protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    AddHandler btnSave.Click, AddressOf btnSave_Click
    AddHandler btnAdd.Click, AddressOf btnAdd_Click
End Sub

Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
    For Each value As string In DynamicTextBoxes

        Dim tb = CType(phMultiEmailAddressBoxes.FindControl(value), TextBox)
        if (tb IsNot Nothing And string.IsNullOrEmpty(tb.Text))

            phMultiEmailAddressBoxes.Controls.Remove(tb)
            DynamicTextBoxes.Remove(value)
        End If
    Next
    End Sub

Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)  Handles btnAdd.Click

    Dim value = "tbEmailAddress" + DynamicTextBoxes.Count.ToString()
    Dim tb = new TextBox() with { .ID = value, .CssClass = "email" }
    phMultiEmailAddressBoxes.Controls.Add(tb)
    DynamicTextBoxes.Add(id)
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:06:57+00:00Added an answer on June 9, 2026 at 6:06 pm

    Not sure what’s wrong with your code, but code below works well for me:

    <asp:UpdatePanel runat="server">
         <ContentTemplate>
              <asp:TextBox runat="server" ID="tbDefaultEmailAddress" />
              <asp:Panel runat="server" ID="phMultiEmailAddressBoxes">
              </asp:Panel>
         </ContentTemplate>
         <Triggers>
              <asp:AsyncPostBackTrigger ControlID="btnSave" />
              <asp:AsyncPostBackTrigger ControlID="btnAdd" />
         </Triggers>
    </asp:UpdatePanel>
    <asp:Button runat="server" ID="btnAdd" Text="Add new email address" />
    <asp:Button runat="server" ID="btnSave" Text="Save" />
    
    private List<string> DynamicTextBoxes
    {
        get
        {
            var list = ViewState["DynamicTextBoxes"] as List<string>;
            if (list == null)
            {
                list = new List<string>();
                ViewState["DynamicTextBoxes"] = list;
            }
            return list;
        }
    }
    
    protected void Page_Init(object sender, EventArgs e)
    {
        btnAdd.Click += new EventHandler(btnAdd_Click);
        btnSave.Click += new EventHandler(btnSave_Click);
    }
    
    void btnSave_Click(object sender, EventArgs e)
    {
        foreach (var id in DynamicTextBoxes.ToArray())
        {
            var tb = phMultiEmailAddressBoxes.FindControl(id) as TextBox;
            if (tb != null && string.IsNullOrEmpty(tb.Text))
            {
                phMultiEmailAddressBoxes.Controls.Remove(tb);
                DynamicTextBoxes.Remove(id);
            }
        }
    }
    
    void btnAdd_Click(object sender, EventArgs e)
    {
        var id = "tbEmailAddress" + DynamicTextBoxes.Count.ToString();
        var tb = new TextBox() { ID = id, CssClass = "email" };
        phMultiEmailAddressBoxes.Controls.Add(tb);
        DynamicTextBoxes.Add(id);
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            foreach (var id in DynamicTextBoxes)
            {
                var tb = new TextBox() { ID = id, CssClass = "email" };
                phMultiEmailAddressBoxes.Controls.Add(tb);
            }
        }
    }
    

    Especially for brother in arms forced to use VB.NET 🙂

    Private ReadOnly Property DynamicTextBoxes As List(Of String)
        Get
            Dim list As List(Of String) = TryCast(ViewState("DynamicTextBoxes"), List(Of String))
            If list Is Nothing Then
                list = New List(Of String)
                ViewState("DynamicTextBoxes") = list
            End If
            Return list
        End Get
    End Property
    
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal evetArgs As EventArgs) Handles Me.Load
        If IsPostBack Then
            For Each id As String In DynamicTextBoxes
                AddNewTextBox(id)
            Next
        End If
    End Sub
    
    Protected Sub AddButton_Click(ByVal sender As Object, ByVal eventAtgs As EventArgs) Handles btnAdd.Click
        Dim id = "tbEmailAddress" & DynamicTextBoxes.Count.ToString()
        AddNewTextBox(id)
        DynamicTextBoxes.Add(id)
    End Sub
    
    Private Sub AddNewTextBox(ByVal id As String)
        Dim tb As TextBox = New TextBox
        tb.ID = id
        tb.CssClass = "email"
        phMultiEmailAddressBoxes.Controls.Add(tb)
    End Sub
    
    Protected Sub SaveButton_Click(ByVal sender As Object, ByVal evebtArgs As EventArgs) Handles btnSave.Click
        For Each id As String In DynamicTextBoxes.ToArray()
            Dim tb As TextBox = TryCast(phMultiEmailAddressBoxes.FindControl(id), TextBox)
            If Not tb Is Nothing AndAlso String.IsNullOrEmpty(tb.Text) Then
                phMultiEmailAddressBoxes.Controls.Remove(tb)
                DynamicTextBoxes.Remove(id)
            End If
        Next
    End Sub
    

    One clarification why I use List of Strings instead of just count of added textboxes: with this approach you can add three dynamic textboxes, fill first and third but skip second and all will work as required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page where the user can dynamically add file upload boxes. Adding
I have a page where users can select different documents files in a datalist
I have a web page where users can see data from MongoDB on a
I have a jqGrid on a page and users can click a button to
I have a Page with many Comments. Many users can access this page and
I have created a page on my site where users can post directly to
I have a subscription link on a page that the logged in users can
I have an ASP.NET page that requires the ability to have users enter multiple
I have a page where a user can import data to the site. either
I have a page where the user can edit various content using buttons and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.