如何清除asp.net,VB.net中的所有文本框 [英] how to clear all the textboxes in asp.net,VB.net

查看:167
本文介绍了如何清除asp.net,VB.net中的所有文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码但不适用于所有文本框

 对于 每个 ctrl 作为控制 form.controls 
< span class =code-keyword>如果 TypeOf ctrl TextBox 然后
CType (ctrl,TextBox).Text = String .Empty
结束 如果
下一步

解决方案

如果您直接在页面中使用TextBox,请尝试此操作:

  foreach  var  item  in  Page.Controls)
{
if (item TextBox)
{
((TextBox)item).Text = ;
}
}



如果你使用任何面板,占用TextBox的占位符,那么试试这个:

< pre lang =c#> private void ResetTextBoxValues(控制父级)
{
foreach (控制c parent.Controls)
{
if (c.Controls.Count > 0
{
ResetTextBoxValues(c);
}
其他
{
开关(c .GetType()。ToString())
{
case System.Web.UI.WebControls.TextBox
((TextBox)c).Text = ;
break ;
}
}
}
}



--Amit


全部您的代码中的控件不清楚,因为如果您在面板或组框中有粘贴文本框或任何其他控件,那么在您的代码中它不会全部检查它们,文本框的内容很明显直接在表单上。



所以请使用以下函数(递归)

 ' 清除表单上的所有文本框 
公共 Sub ClearAll( ByVal Frm As Control)
错误 恢复 下一步
Dim OutCtl As Control
< span class =code-keyword>对于 每个 OutCtl Frm.Controls
如果 OutCtl.Tag<> save 然后
如果 OutCtl.Controls.Count> 0 然后
ClearAll(OutCtl)
否则
' Texboxes,Decimalx,LongX
< span class =code-keyword>如果 TypeOf (OutCtl) TextBox InStr(OutCtl.Tag.ToString, save,CompareMethod.Text)<> 0 然后
OutCtl.Text =
结束 如果
结束 如果
结束 如果
下一步
结束 Sub



快乐编码!

: )


i am using following code but its not for all textboxes

For Each ctrl As Control In form.controls
    If TypeOf ctrl Is TextBox Then
        CType(ctrl, TextBox).Text = String.Empty
    End If
Next

解决方案

Try this if you are having the TextBox directly in page:

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:

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


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)

'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!
:)


这篇关于如何清除asp.net,VB.net中的所有文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆