How to clear all textboxes on Webform using VB.Net

P

Peter Afonin

Hello:

I need to clear all textboxes on the webform after the data has been
submitted. How can I do this in one step?

I found a C# code:

// get a reference to your form control
Control frm = FindControl("YourFormID");
foreach(Control ctrl in frm.Controls)
{
TextBox tb = ctrl as TextBox;
if(ctrl!=null)
tb.Text="";
}

I tried to interpret it into VB.Net like this:

Dim frm As Control = Me.FindControl("Form1")
Dim ctl As Control
For Each ctl In frm.Controls
Dim tb As TextBox = CType(ctl, TextBox)
tb.Text = ""
Next

but I get an error message: "Specified cast is not valid".
So this line is incorrect: Dim tb As TextBox = CType(ctl, TextBox)

Could you help me please?

Thank you,
 
C

Cor

Hi Peter,

This works on a winform but as far as I know (and I did test it often) not
on a webform

On a winform it is (if there is a typing error don't look, I make a lot of
them)

\\\
dim ctr as control
for each ctr in controls
if typeof ctr is textbox
ctr.text = ""
end if
next
///

I wished it did, the documentation is full of it, there was one page in the
knowledge base, which said it doesn't work, but I cannot find it anymore.

That it does not work has to do with the typeof, it is not useable when a
page is starting to load they wrote I thought.

But if it works on a webform , tell me how you did it!!!

:-((((
Cor
 
R

Rick Strahl [MVP]

Peter,

The problem is that in ASP.Net you need to drill into the containers. Form 1
is actually a single control contained in a Page object. It then is a
container that contains the main form controls, which in turn may contain
other controls.

You can use recursive code to loop through all controls. Something liek
this:
static void FormBindData(Control Container, Page WebForm)
{

foreach( Control loControl in Container.Controls)
{

// ** Recursively call down into any containers
if (loControl.Controls.Count > 0)
FormBindData(loControl, WebForm);

if (loControl is TextBox )
{
...
}

}

}

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
 
P

Peter Afonin

Hi Cor,

Thank you, I modified your statement and made it work.

Your code doesn't work exactly right because ctl.text is invalid (control
doesn't have text property). So I modified it a little, and it works great:

Dim frm As Control = Me.FindControl("Form1")
Dim ctl As Control

For Each ctl In frm.Controls
Dim tb As TextBox
If TypeOf ctl Is TextBox Then
tb = CType(ctl, TextBox)
tb.Text = String.Empty
End If
Next

Best,

Peter
 
P

Peter Afonin

Thank you, Rick.

I modified the code that Cor suggested, and it works so far:

Dim frm As Control = Me.FindControl("Form1")
Dim ctl As Control

For Each ctl In frm.Controls
Dim tb As TextBox
If TypeOf ctl Is TextBox Then
tb = CType(ctl, TextBox)
tb.Text = String.Empty
End If
Next

I think it's exactly what you're saying - it's drilling into the container
Form1.
--
Peter Afonin

Rick Strahl said:
Peter,

The problem is that in ASP.Net you need to drill into the containers. Form 1
is actually a single control contained in a Page object. It then is a
container that contains the main form controls, which in turn may contain
other controls.

You can use recursive code to loop through all controls. Something liek
this:
static void FormBindData(Control Container, Page WebForm)
{

foreach( Control loControl in Container.Controls)
{

// ** Recursively call down into any containers
if (loControl.Controls.Count > 0)
FormBindData(loControl, WebForm);

if (loControl is TextBox )
{
...
}

}

}

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
 
C

Cor

Hi Peter,

You are right, this is greath

A little aprovement, that you can try (works I tested it)
tb = CType(ctl, TextBox)
tb = DirectCast(ctl,TextBox)

But you did find it,

Greath
:)
Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top