473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How to empty all textbox in a page

I Have a page (clientes.aspx), inside a masterpage
I have some textbox, and when the user clicks the button 'Cancel', I need to
empty all controls. I tried this, with runtine error:

For Each txtControl As TextBox In Me.Controls
txtControl.Text = ""
Next

error message in runtime:
can't convert as object of type 'ASP.masterpage_master' in type
'System.Web.UI.WebControls.TextBox'.
--
Thanks in advance
Hércules
HRsoft Informática - Rio de Janeiro - Brasil
http://www.hrsoft.com.br

Jul 18 '08 #1
6 2227
"HRsoft Informática" <HR**************@discussions.microsoft.comwrote in
message news:70**********************************@microsof t.com...
I Have a page (clientes.aspx), inside a masterpage
I have some textbox, and when the user clicks the button 'Cancel', I need
to
empty all controls. I tried this, with runtine error:

For Each txtControl As TextBox In Me.Controls
txtControl.Text = ""
Next

error message in runtime:
can't convert as object of type 'ASP.masterpage_master' in type
'System.Web.UI.WebControls.TextBox'.
Yes, that's correct. Think about it...

Your code says iterate through *every* control in the page's control
collection, cast it to a TextBox (even if it isn't), and then clear its Text
property... As soon as the For loop reaches a control which *isn't* a
TextBox, the exception will be thrown.

So...

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

Apologies if the above isn't syntactically correct - I never go anywhere
near VB.NET...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 18 '08 #2
Plus you may have to recurse if you are on a container that holds
textboxes...

--
Patrice

"Mark Rae [MVP]" <ma**@markNOSPAMrae.neta écrit dans le message de groupe
de discussion : uK**************@TK2MSFTNGP02.phx.gbl...
"HRsoft Informática" <HR**************@discussions.microsoft.comwrote in
message news:70**********************************@microsof t.com...
>I Have a page (clientes.aspx), inside a masterpage
I have some textbox, and when the user clicks the button 'Cancel', I need
to
empty all controls. I tried this, with runtine error:

For Each txtControl As TextBox In Me.Controls
txtControl.Text = ""
Next

error message in runtime:
can't convert as object of type 'ASP.masterpage_master' in type
'System.Web.UI.WebControls.TextBox'.

Yes, that's correct. Think about it...

Your code says iterate through *every* control in the page's control
collection, cast it to a TextBox (even if it isn't), and then clear its
Text property... As soon as the For loop reaches a control which *isn't* a
TextBox, the exception will be thrown.

So...

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

Apologies if the above isn't syntactically correct - I never go anywhere
near VB.NET...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 18 '08 #3
"Patrice" <http://www.chez.com/scribe/wrote in message
news:3D**********************************@microsof t.com...

[top-posting corrected]
>For Each objControl As Control In Me.Controls
If GetType(objControl) = "TextBox" Then
DirectCast(objControl, TextBox).Text = ""
End If
Next

Apologies if the above isn't syntactically correct - I never go anywhere
near VB.NET...

Plus you may have to recurse if you are on a container that holds
textboxes...
Yes, that's true - I should have mentioned that...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 18 '08 #4
pretty simple:
// find all textbox controls on page
Control[] list = ControlWalker(this, ctl =ctl is TextBox);

// clear text
foreach (Control ctl in list)
{
((TextBox) ctl).Text = "";
}

......
public delegate bool ControlWalkerMatcher (Control ctl);
public Control[] ControlWalker(Control ctl, ControlWalkerMatcher matcher)
{
ArrayList list = new ArrayList();
if (matcher(ctl)) list.Add(ctl);
for (int i=0; i < ctl.Controls.Count; ++i)
{
Control[] childList = ControlWalker(ctl.Controls[i],matcher);
if (childList.Length 0) list.AddRange(childList);
}
return (Control[]) list.ToArray(typeof(Control));
}

-- bruce (sqlwork.com)
"HRsoft Informática" wrote:
I Have a page (clientes.aspx), inside a masterpage
I have some textbox, and when the user clicks the button 'Cancel', I need to
empty all controls. I tried this, with runtine error:

For Each txtControl As TextBox In Me.Controls
txtControl.Text = ""
Next

error message in runtime:
can't convert as object of type 'ASP.masterpage_master' in type
'System.Web.UI.WebControls.TextBox'.
--
Thanks in advance
Hércules
HRsoft Informática - Rio de Janeiro - Brasil
http://www.hrsoft.com.br
Jul 18 '08 #5
Dear friend

Unfortunatelly, this syntax not compile:
Error 78 Type 'objcontrol' is not
defined. C:\inetpub\wwwroot\AjaxControlToolkitWebSite1\clie ntes.aspx.vb 317 28 C:\...\AjaxControlToolkitWebSite1\

--
Hércules
HRsoft Informática - Rio de Janeiro - Brasil
http://www.hrsoft.com.br

"Mark Rae [MVP]" wrote:
"Patrice" <http://www.chez.com/scribe/wrote in message
news:3D**********************************@microsof t.com...

[top-posting corrected]
For Each objControl As Control In Me.Controls
If GetType(objControl) = "TextBox" Then
DirectCast(objControl, TextBox).Text = ""
End If
Next

Apologies if the above isn't syntactically correct - I never go anywhere
near VB.NET...
Plus you may have to recurse if you are on a container that holds
textboxes...

Yes, that's true - I should have mentioned that...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 18 '08 #6
"HRsoft Informática" <HR**************@discussions.microsoft.comwrote in
message news:64**********************************@microsof t.com...

[top-posting corrected again]
>For Each objControl As Control In Me.Controls
If GetType(objControl) = "TextBox" Then
DirectCast(objControl, TextBox).Text = ""
End If
Next

Apologies if the above isn't syntactically correct - I never go
anywhere
near VB.NET...

Plus you may have to recurse if you are on a container that holds
textboxes...

Yes, that's true - I should have mentioned that...

Unfortunatelly, this syntax not compile:
Error 78 Type 'objcontrol' is not
defined. C:\inetpub\wwwroot\AjaxControlToolkitWebSite1\clie ntes.aspx.vb
317 28 C:\...\AjaxControlToolkitWebSite1\
Apologies, try this instead:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
For Each objControl As Control In Me.Controls
If TypeOf (objControl) Is TextBox Then
DirectCast(objControl, TextBox).Text = ""
End If
Next
End Sub
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 19 '08 #7

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

Similar topics

1
by: Rimu Atkinson | last post by:
hi all i use FindControl to get a textbox, and i get a textbox object, but it's ..Text property is empty. some background info: i have a DataList, which has a template column in it, and in...
0
by: Tetet B via .NET 247 | last post by:
I'm using VB.Net and trying to retrieve the data in the textboxcell of a datagrid after a successful display (used Databind).User can either display data or enter data in the textbox. I canalways get...
2
by: Eric Maia | last post by:
I have a textbox (StartDateTextBox) in a UserControl on my page, that is supposed to have a date entered into it. I have a RequiredFieldValidator that has its ControlToValidate property set to the...
3
by: Sandy | last post by:
Hello - I have a page that runs a stored procedure checking for the existence of a name in a database. If it exists, lblMessage returns text indicating same. When a user goes back and clears...
2
by: Alan Silver | last post by:
Hello, I have a form that contains a repeater. A simplified version of the ItemTemplate is shown below (air code)... <br><asp:Literal id="litID" runat="server" /> <br><asp:TextBox id="txtQty"...
0
by: VMI | last post by:
If I'm on "Page 1" of my Wizard webcontrol and I leave a required textbox empty, is there a way that I can validate that before moving on to another page? For example, I can't leave Page 1 until I...
1
by: emailseshu | last post by:
Hi, Iam trying to check the Client Side CallBack Feature in ASP.NET Iam able to call th RaiseCallbackEvent from the Client Side Javascript Function but when I go to the debug mode to check the...
8
by: Joe Kovac | last post by:
Hi! I want the user to edit a textbox which allows following values only: - Time (Format: 23:59, HH:MM) or - NULL (empty string) What can I do, so that this works kinda automatically, meaning...
1
by: Brad Pears | last post by:
I am using vb.net 2005 and SQL server 2000. In my table I have a date field of type "smalldatetime". In my vb application, the user may or may not enter a date value into the appropriate text box....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.