卸下code-后面一个asp.net控制后面、code、net、asp

由网友(鹿死玫瑰海)分享简介:我需要从我的网页中删除控制(文本框)时,一定条件下被验证。是否有可能从隐藏的C $ C $做或者我需要使用JavaScript。I need to remove a control (a textbox) from my page when a certain condition is verified.Is i...

我需要从我的网页中删除控制(文本框)时,一定条件下被验证。 是否有可能从隐藏的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...

推荐答案

使用Controls.Remove或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;
阅读全文

相关推荐

最新文章