Betty
Betty

Reputation: 641

Clear Textboxes

Morning All,

I would like to have a cancel button on my web page that essentially i would like to clear form field and themn redirect users to the home page.

I have 7 txt boxes that i will need to clear before the page redirects. I have done some searching on the internets and have tried to put the following sample into my page with no luck....

With this code i get an error on the X = "" line. I get a message 'Value of type string cannt be converted to system.we.UI.control'

 Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click

         Dim x As Control
           For Each x In Me.Controls
         If TypeOf x Is TextBox Then
           x = " "
         End If

         Next

 End Sub

And i have also tried the below which also produces a Compilation Error.

        For Each c As Control In Me.Controls
        If TypeOf c Is TextBox Then
            DirectCast(c, TextBox).Text = ""
        End If
        Next

Can anyone help me with resolving this issue?

Regards Betty

Upvotes: 1

Views: 505

Answers (2)

user1465587
user1465587

Reputation:

You can use html code to reset all field of current form you are working with

use follwoing code to reset all fields

<input type="reset" value="Clear" onclick="redirectFunction()" />

and in redirectFunction write following javascript code:

function redirectFunction()
{
window.location="destination.aspx";
}

using above code you can redirect to destination page.

Upvotes: 1

Random Dev
Random Dev

Reputation: 52280

Try this:

Dim x As Control
For Each x In Me.Controls
  If TypeOf x Is TextBox Then
    Dim txt as TextBox = x
    txt.Text = ""
  End If
Next

Explanation: You tried to set a string to a Control-variable and of course the compiler does not know how to to this. The version I gave will set the Text-property of each TextBox to the empty-String

Upvotes: 1

Related Questions