卸下code-后面一个asp.net控制 [英] Remove an asp.net control from code-behind

查看:147
本文介绍了卸下code-后面一个asp.net控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的网页中删除控制(文本框)时,一定条件下被验证。 是否有可能从隐藏的C $ C $做或者我需要使用JavaScript。

I need to remove a control (a textbox) from my page when a certain condition is verified. Is it possible to do from code-behind or I need to use JavaScript.

注意我需要删除的控制,不隐藏...

NOTE I need to remove the control, not to hide...

推荐答案

使用<一个href="http://msdn.microsoft.com/en-us/library/system.web.ui.controlcollection.remove.aspx"><$c$c>Controls.Remove或<一href="http://msdn.microsoft.com/en-us/library/system.web.ui.controlcollection.removeat.aspx"><$c$c>Controls.RemoveAt的ControlCollection

例如,如果你想从页面的顶部删除所有文本框:

For example, if you want to remove all TextBoxes from the page's top:

var allTextBoxes = Page.Controls.OfType<TextBox>().ToList();
foreach(TextBox txt in allTextBoxes)
    Page.Controls.Remove(txt);

<子>(注意,你需要添加使用System.Linq的 Enumerable.OfType

如果你想删除一个TextBox给定ID:

or if you want to remove a TextBox with a given ID:

TextBox textBox1 = (TextBox)Page.FindControl("TextBox1"); // note that this doesn't work when you use MasterPages
if(textBox1 != null)
    Page.Controls.Remove(textBox1);

如果你只想隐藏它(并从客户方完全删除它),你也可以把它无形的:

If you just want to hide it (and remove it from clientside completely), you can also make it invisible:

textBox1.Visible = false;

这篇关于卸下code-后面一个asp.net控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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