Click here to Skip to main content
15,916,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using following code but its not for all textboxes
VB
For Each ctrl As Control In form.controls
    If TypeOf ctrl Is TextBox Then
        CType(ctrl, TextBox).Text = String.Empty
    End If
Next
Posted
Updated 2-Apr-13 21:50pm
v2

Try this if you are having the TextBox directly in page:
C#
foreach (var item in Page.Controls)
{
    if (item is TextBox)
    {
        ((TextBox)item).Text = "";
    }
}

And if you are using any panels, placeholder for your TextBox then try this:
C#
private void ResetTextBoxValues(Control parent)
{
    foreach (Control c in parent.Controls)
    {
        if (c.Controls.Count > 0)
        {
            ResetTextBoxValues(c);
        }
        else
        {
            switch(c.GetType().ToString())
            {
                case "System.Web.UI.WebControls.TextBox":
                    ((TextBox)c).Text = "";
                    break;             
            }              
        }
    }
}

--Amit
 
Share this answer
 
v3
All controls not clear in your code because if you have paste textbox inside panel or groupbox or any other control then in your code it will not check them all, it is clear contents of textbox which are on form directly.

so use below function(recursion)
VB
'clearing all textboxes on a form
    Public Sub ClearAll(ByVal Frm As Control)
        On Error Resume Next
        Dim OutCtl As Control
        For Each OutCtl In Frm.Controls
            If OutCtl.Tag <> "save" Then
                If OutCtl.Controls.Count > 0 Then
                    ClearAll(OutCtl)
                Else
                    'Texboxes,Decimalx,LongX
                        If TypeOf (OutCtl) Is TextBox And InStr(OutCtl.Tag.ToString, "save", CompareMethod.Text) <> 0 Then
                            OutCtl.Text = ""
                        End If
                    End If
           End If
        Next
    End Sub

Happy Coding!
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900