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 7 1877
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
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
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
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
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
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
> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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)
|
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...
|
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
| |
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,...
|
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...
|
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
|
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;
......
|
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...
|
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,...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |