如何使用VB.Net清除Webform上的所有文本框 [英] How to clear all textboxes on Webform using VB.Net

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

问题描述

您好:


我需要在提交数据后提交清除webform上的所有文本框。我怎样才能一步到位?


我找到了一个C#代码:


//获取对表单控件的引用

控制frm = FindControl(" YourFormID");

foreach(控制ctrl in frm.Controls)

{

TextBox tb = ctrl as TextBox;

if(ctrl!= null)

tb.Text ="" ;;

}


我试着像这样把它解释成VB.Net:


Dim frm As Control = Me.FindControl(" Form1")

Dim ctl As Control

For each ctl in frm.Controls

Dim tb As TextBox = CType(ctl,TextBox)

tb.Text =""

Next


但我收到一条错误消息:指定的演员表无效。

所以这一行不正确:Dim tb As TextBox = CType(ctl,TextBox)


你能帮帮我吗?


谢谢,


-

Peter Afonin

Hello:

I need to clear all textboxes on the webform after the data has been
submitted. How can I do this in one step?

I found a C# code:

// get a reference to your form control
Control frm = FindControl("YourFormID");
foreach(Control ctrl in frm.Controls)
{
TextBox tb = ctrl as TextBox;
if(ctrl!=null)
tb.Text="";
}

I tried to interpret it into VB.Net like this:

Dim frm As Control = Me.FindControl("Form1")
Dim ctl As Control
For Each ctl In frm.Controls
Dim tb As TextBox = CType(ctl, TextBox)
tb.Text = ""
Next

but I get an error message: "Specified cast is not valid".
So this line is incorrect: Dim tb As TextBox = CType(ctl, TextBox)

Could you help me please?

Thank you,

--
Peter Afonin

推荐答案

嗨彼得,


这适用于winform但据我所知(我经常测试它)不是

在webform上


在winform上它是(如果有打字错误不看,我会赚很多




\\\

dim ctr作为控制

控制中的每个ctr

如果typeof ctr是文本框

ctr.text =""

结束如果

next

///


我希望它能做到,文档充满了它,

知识库中有一页,说它不起作用,但我找不到它已经不再了。


它不起作用与typeof有关,当

页面开始加载时它是不可用的。


但如果它适用于webform,请告诉我你是如何做到的!


: - (((< />
Cor

Hi Peter,

This works on a winform but as far as I know (and I did test it often) not
on a webform

On a winform it is (if there is a typing error don''t look, I make a lot of
them)

\\\
dim ctr as control
for each ctr in controls
if typeof ctr is textbox
ctr.text = ""
end if
next
///

I wished it did, the documentation is full of it, there was one page in the
knowledge base, which said it doesn''t work, but I cannot find it anymore.

That it does not work has to do with the typeof, it is not useable when a
page is starting to load they wrote I thought.

But if it works on a webform , tell me how you did it!!!

:-((((
Cor


彼得,


专业人士在ASP.Net中你需要钻进容器中。表单1

实际上是Page对象中包含的单个控件。它是一个

容器,包含主窗体控件,而后者可能包含

其他控件。


你可以使用递归代码循环遍历所有控件。有点谎言

这个:

static void FormBindData(控制容器,Page WebForm)

{


foreach(在Container.Controls中控制loControl)

{


// **递归调用任何容器

如果(loControl.Controls.Count> 0)

FormBindData(loControl,WebForm);


if(loControl是TextBox)

{

...

}


}


}


+++ Rick ---


-


Rick Strahl

West Wind Technologies
http://www.west-wind.com /
http://www.west-wind.com / wwHelp

----------------------------------

在网上制造波浪

" Peter Afonin" < PA ** @ specialtypulltabs.com>写在消息

新闻:Ox ************** @ TK2MSFTNGP11.phx.gbl ...
Peter,

The problem is that in ASP.Net you need to drill into the containers. Form 1
is actually a single control contained in a Page object. It then is a
container that contains the main form controls, which in turn may contain
other controls.

You can use recursive code to loop through all controls. Something liek
this:
static void FormBindData(Control Container, Page WebForm)
{

foreach( Control loControl in Container.Controls)
{

// ** Recursively call down into any containers
if (loControl.Controls.Count > 0)
FormBindData(loControl, WebForm);

if (loControl is TextBox )
{
...
}

}

}

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
----------------------------------
Making waves on the Web
"Peter Afonin" <pa**@specialtypulltabs.com> wrote in message
news:Ox**************@TK2MSFTNGP11.phx.gbl...
你好:

我需要在提交数据后清除网络表单上的所有文本框。我怎样才能一步到位?

我找到了一个C#代码:

//获取对表单控件的引用
控制frm = FindControl(& ; YourFormID");
foreach(在frm.Controls中控制ctrl)
{
TextBox tb = ctrl as TextBox;
if(ctrl!= null)
tb .Text ="" ;;
}
我试着将它解释成VB.Net,如下所示:

Dim frm As Control = Me.FindControl( Form1)
Dim ctl As Control
For each ctl in frm.Controls
Dim tb As TextBox = CType(ctl,TextBox)
tb.Text ="" ;


但是我收到一条错误消息:指定的强制转换无效。
所以这一行不正确:Dim tb As TextBox = CType(ctl,TextBox) )

你能帮我吗?

谢谢你,

-
Peter Afonin
Hello:

I need to clear all textboxes on the webform after the data has been
submitted. How can I do this in one step?

I found a C# code:

// get a reference to your form control
Control frm = FindControl("YourFormID");
foreach(Control ctrl in frm.Controls)
{
TextBox tb = ctrl as TextBox;
if(ctrl!=null)
tb.Text="";
}

I tried to interpret it into VB.Net like this:

Dim frm As Control = Me.FindControl("Form1")
Dim ctl As Control
For Each ctl In frm.Controls
Dim tb As TextBox = CType(ctl, TextBox)
tb.Text = ""
Next

but I get an error message: "Specified cast is not valid".
So this line is incorrect: Dim tb As TextBox = CType(ctl, TextBox)

Could you help me please?

Thank you,

--
Peter Afonin



您好Cor,


谢谢,我修改了您的陈述并使其成功。


您的代码无法正常工作,因为ctl.text无效(控件

没有text属性)。所以我修改了一下,效果很好:


Dim frm As Control = Me.FindControl(" Form1")

Dim ctl As Control


For each ctl in frm.Controls

Dim tb As TextBox

如果TypeOf ctl是TextBox那么

tb = CType(ctl,TextBox)

tb.Text = String.Empty

结束如果

下一页
< br $> b $ b Best,


彼得


" Cor" < no*@non.com>在消息中写道

news:3f ********************** @ reader20.wxs.nl ...
Hi Cor,

Thank you, I modified your statement and made it work.

Your code doesn''t work exactly right because ctl.text is invalid (control
doesn''t have text property). So I modified it a little, and it works great:

Dim frm As Control = Me.FindControl("Form1")
Dim ctl As Control

For Each ctl In frm.Controls
Dim tb As TextBox
If TypeOf ctl Is TextBox Then
tb = CType(ctl, TextBox)
tb.Text = String.Empty
End If
Next

Best,

Peter

"Cor" <no*@non.com> wrote in message
news:3f**********************@reader20.wxs.nl...
嗨彼得,

这适用于winform但据我所知(我经常测试它)不是在网页上

它是一个winform(如果有打字错误不看,我做了很多
他们)

\\\
dim ctr作为控件
对于控件中的每个ctr
如果typeof ctr是文本框
ctr.text =""
结束如果
下一页
///

我希望它能做到,文档充满了它,知识库中有一页
,它说它不起作用,但我再也找不到它了。

它不起作用与typeof有关,当我开始编写
页面开始加载时它是不可用的。

但如果它可以工作一个webform,告诉我你是怎么做到的!

: - (((
Hi Peter,

This works on a winform but as far as I know (and I did test it often) not
on a webform

On a winform it is (if there is a typing error don''t look, I make a lot of
them)

\\\
dim ctr as control
for each ctr in controls
if typeof ctr is textbox
ctr.text = ""
end if
next
///

I wished it did, the documentation is full of it, there was one page in the knowledge base, which said it doesn''t work, but I cannot find it anymore.

That it does not work has to do with the typeof, it is not useable when a
page is starting to load they wrote I thought.

But if it works on a webform , tell me how you did it!!!

:-((((
Cor


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

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