473,473 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to clear all textboxes on Webform using VB.Net

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,

--
Peter Afonin
Nov 20 '05 #1
7 1877
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

Nov 20 '05 #2
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
----------------------------------
Making waves on the Web
"Peter Afonin" <pa**@specialtypulltabs.com> wrote in message
news:Ox**************@TK2MSFTNGP11.phx.gbl...
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,

--
Peter Afonin

Nov 20 '05 #3
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

"Cor" <no*@non.com> wrote in message
news:3f**********************@reader20.wxs.nl...
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

Nov 20 '05 #4
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 [MVP]" <ri********@hotmail.com> wrote in message
news:#G**************@TK2MSFTNGP12.phx.gbl...
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
----------------------------------
Making waves on the Web
"Peter Afonin" <pa**@specialtypulltabs.com> wrote in message
news:Ox**************@TK2MSFTNGP11.phx.gbl...
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,

--
Peter Afonin


Nov 20 '05 #5
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
Nov 20 '05 #6
Check out this article for looping through controls in asp.net
http://www.extremeexperts.com/Net/Ar...hControls.aspx

--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com

"Peter Afonin" <pa**@specialtypulltabs.com> wrote in message
news:uR**************@TK2MSFTNGP10.phx.gbl...
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

"Cor" <no*@non.com> wrote in message
news:3f**********************@reader20.wxs.nl...
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


Nov 20 '05 #7
Cor
> http://www.extremeexperts.com/Net/Ar...hControls.aspx

This example is terrible. Peters routine has the normal VB.net standards for
doing controls using the for each loop. As far as I know is the for each
loop fastest way to control your program in VB.net, this example looks like
done by a Pascal programmer.

Cor
Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: T. Wintershoven | last post by:
Hello, I have a few textboxes placed on a SSTab control devided over 3 tabs Is there a way to clear all textboxes in one time in stead of one by one. Someone told me "Use for each item...
9
by: Peter Afonin | last post by:
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...
1
by: mg | last post by:
How can I apply validation for date and currency on the values typed in EditItemTemplate TextBoxes in a DataGrid (C# WebForm)
0
by: TN Bella | last post by:
Hi, Here is part of one sub where I want to clear all textboxes (there are three subs where I do this, one contains more textboxes, the other is below and the final one looks the same but omits...
1
by: Boonaap | last post by:
Anyone an idea how to create a Hierarchical data webform? I was trying to figure it out with a datagrid but anyone a better idea? should be C# thx
2
by: alpoor | last post by:
I have lots of textboxes on my webform. I need to bind them with fields from table. I am not sure how to bind with textboxes. I have seen so many samples for dropdown boxes and datagrid binding,...
0
by: Nathan Sokalski | last post by:
I am making a webform that allows the user to add another textbox by clicking a button to allow them to enter however many names they need. The procedure that does this uses information entered in...
8
by: Portroe | last post by:
I think I have asked this before, :-) but old posts are deleted it seems, How do you clear textboxes in a form, (on submit)? thanks portroe
2
by: =?Utf-8?B?bWF2cmljazEwMQ==?= | last post by:
Hi, I have a webform which has 4 panels. Each panel would have about 20 textboxes and dropdown boxes. I have named the controls like Panel1: _textboxP1T1; _textboxP1T2; ......
1
by: | last post by:
I'm trying to clear all the textboxes on a webpage, this is the code I use in an application, but a control doesn't have a text member in a webform????? I get an error on the "ctl.text" saying...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.