卸下code-后面一个asp.net控制 [英] Remove an asp.net control from code-behind
问题描述
我需要从我的网页中删除控制(文本框)时,一定条件下被验证。 是否有可能从隐藏的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$c$c>或<一href="http://msdn.microsoft.com/en-us/library/system.web.ui.controlcollection.removeat.aspx"><$c$c>Controls.RemoveAt$c$c>父的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屋!