.NET Framework - How to empty all textbox in a page

Asked By HRsoftInformtic on 18-Jul-08 10:10 AM
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




Mark Rae [MVP] replied on 18-Jul-08 10:56 AM
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
Patrice replied on 18-Jul-08 11:02 AM
Plus you may have to recurse if you are on a container that holds
textboxes...

--
Patrice
Mark Rae [MVP] replied on 18-Jul-08 11:07 AM
[top-posting corrected]


Yes, that is true - I should have mentioned that...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
David Wier replied on 18-Jul-08 11:30 AM
One inexpensive solution is a new control called ControlGrouper
You can blank all the text from all textboxes grouped with it in 1-2 lines,
plus control other properties like enabled, visible, width
http://cg.augustwind.com

David Wier
http://aspnet101.com
http://cg.augustwind.com - ControlGrouper (ASP.Net Control) - download and
register for contest prizes
http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
bloated markup
brucebarke replied on 18-Jul-08 11:40 AM
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)
HRsoftInformtic replied on 18-Jul-08 03:31 PM
Dear friend

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

--
Hércules
HRsoft Informática - Rio de Janeiro - Brasil
http://www.hrsoft.com.br
Mark Rae [MVP] replied on 19-Jul-08 07:17 AM
[top-posting corrected again]


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