如何在aspx页面中隐藏ASP.NET自定义控件的属性?

时间:2022-04-21 15:11:22

I'm writing ASP.NET custom control, and I want it to have a few properties which should be visible only from code behind during run-time - I mean, these properties should not be visible both in a designer and in a aspx code of page containing this control. I've tried to use following attributes:

我正在编写ASP.NET自定义控件,我希望它有一些属性,这些属性只能在运行时从代码中看到 - 我的意思是,这些属性在设计器和aspx代码中都不应该是可见的包含此控件的页面。我试过使用以下属性:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public List<Item> SomeData { ... }

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [可浏览(false)] public List SomeData {...}

but unfortunately this property is still visible in an Intellisense combobox when editing aspx page. Is it possible to hide this property everywhere besides server-side code ?

但不幸的是,在编辑aspx页面时,这个属性在Intellisense组合框中仍然可见。是否可以在服务器端代码之外的任何地方隐藏此属性?

2 个解决方案

#1


12  

This should do the trick:

这应该是诀窍:

//Hide from Designer Property Grid
[Browsable(false)]
// Hide from VS.NET Code Editor IntelliSense
[EditorBrowsable(EditorBrowsableState.Never)]
// Not Serialized in Designer Source code "HTML view"
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public List<Item> SomeData { ... }

#2


2  

Amiir's answer definitely works, but I'd like to add that sometimes even after applying the attributes, the Intellisense still displays the properties. This is a result of Visual Studio caching the Intellisense files. If you build the same project on a different machine, it will not show the properties. If this really bugs you, you can clear the cache by deleting all files in the folder "C:\Documents and Settings\[YOUR_USER_NAME]\Application Data\Microsoft\VisualStudio\10.0\ReflectedSchemas."

Amiir的答案肯定有效,但我想补充说,有时甚至在应用属性后,Intellisense仍会显示属性。这是Visual Studio缓存Intellisense文件的结果。如果在不同的计算机上构建相同的项目,则不会显示属性。如果这确实让您感到烦恼,可以通过删除文件夹“C:\ Documents and Settings \ [YOUR_USER_NAME] \ Application Data \ Microsoft \ VisualStudio \ 10.0 \ ReflectedSchemas”中的所有文件来清除缓存。

#1


12  

This should do the trick:

这应该是诀窍:

//Hide from Designer Property Grid
[Browsable(false)]
// Hide from VS.NET Code Editor IntelliSense
[EditorBrowsable(EditorBrowsableState.Never)]
// Not Serialized in Designer Source code "HTML view"
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public List<Item> SomeData { ... }

#2


2  

Amiir's answer definitely works, but I'd like to add that sometimes even after applying the attributes, the Intellisense still displays the properties. This is a result of Visual Studio caching the Intellisense files. If you build the same project on a different machine, it will not show the properties. If this really bugs you, you can clear the cache by deleting all files in the folder "C:\Documents and Settings\[YOUR_USER_NAME]\Application Data\Microsoft\VisualStudio\10.0\ReflectedSchemas."

Amiir的答案肯定有效,但我想补充说,有时甚至在应用属性后,Intellisense仍会显示属性。这是Visual Studio缓存Intellisense文件的结果。如果在不同的计算机上构建相同的项目,则不会显示属性。如果这确实让您感到烦恼,可以通过删除文件夹“C:\ Documents and Settings \ [YOUR_USER_NAME] \ Application Data \ Microsoft \ VisualStudio \ 10.0 \ ReflectedSchemas”中的所有文件来清除缓存。