Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Clearing Controls on Submit

0.00/5 (No votes)
25 Dec 2014 1  
Clearing the controls on Submit

Introduction

When you have a Inventory/Online Adjustment claims website like I do which takes lot of data from users, you would need some function which clears out all the fields at one go.

Background

I am working on a web application which is more similar to make a claim form electronically. Instead of sending adjustment claim forms with lot of paper work, I am designing a web application which gathers all the fields that are necessary to complete a claim form. At one point of time, I was fed up with clearing the controls after user submits the form on website. Then comes the idea which made me dig more into this and finally brought me to write this tip. I have seen people clearing each control by assigning empty string. So I did some research and finally got this working.

//
Public Shared Function FlattenHierachy(ByVal root As Control) As Control()
        Dim list As New List(Of Control)()
        list.Add(root)
        If root.HasControls() Then
            For Each control As Control In root.Controls
                list.AddRange(FlattenHierachy(control))
            Next
        End If
        Return list.ToArray()
    End Function

    Private Sub ClearTextBoxes()
        Dim allControls As Control() = FlattenHierachy(Page)
        For Each control As Control In allControls
            Dim textBox As TextBox = TryCast(control, TextBox)
            If textBox IsNot Nothing Then
                textBox.Text = ""
            End If
        Next
    End Sub

    Protected Sub ClearDropDowns()
        ddlState.SelectedIndex = -1
        ddlADState.SelectedIndex = -1
        ddlDState.SelectedIndex = -1

    End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here