By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

How to empty all textbox in a page

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
"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

P: n/a
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 Informtica" <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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.