Today you write controls yet? ----ASP.net Control Development Series (D)

Original link: http://www.cnblogs.com/think/archive/2005/05/16/156158.html
Properties with the Properties pane

on the article, and we explore the relationship between the properties and the aspx file in HTML style tags and text, missing two things:

1 EnCodedInnerDefaultProperty and InnerDefaultProperty difference, in use , there may be some friends about this not very clear,

EncodedInnerDefaultProperty property is not allowed to control the contents of the object, for example, declares DataList Text property EncodedInnerDefaultProperty that

does not allow you to "<table ......> ..... </> "and the like containing HTML tags (exactly solvable noted object) content. And declared

the Items property of the DropDownList InnerDefaultProperty you can write <asp: datalist value = "1such content.

2, if a property should not be in control in the aspx file, how to do it?

These we can that the Attribute:

DesignerSerilizationVisibility (DesignerSerializationVisibility.Hidden)

Well, we turn today to the question: attributes and properties pane

believe that most programmers in most cases is to use the properties pane to set the properties of a control object, Therefore, the properties pane also control design essential.

In the Properties pane, said that, we need to look at PropertyGrid, PropertyGrid is located under the System.Windows.Forms.dll controls, VS.net and other IDE

Tool is to use it to provide display properties, operating functions, we may and usually has no concept of this control, though perhaps you are using the functionality it provides every day.

propertygrid.gif 

If you want a better description of it, you can build a Winform project, add the control to VS.net toolbox, and then pull to form a look at it, this work

with our programming is generally less than it , but when you want to provide custom-designed for the control (such as DataGrid property editor), using it is a good

idea to something more (for various designers and PropertyGrid, I will in the future design of the control design when the feature articles and then tell).

There are many different forms PropertyGrid when displaying the control's properties,

for example: Button Font property, ProperyGrid it appears as a deployable, folded set of attributes, first Font type attribute is a complex class

(FontInfo), it has a lot of sub-attributes, then, you want to apply attribute we mentioned above:

the DesignerSerializationVisibility (DesignerSerializationVisibility.Content)

which represents the serialization code generator sub-attribute instead of its own, how to understand it? That Font property of the Button control's child Bold and Underline attributes

will be serialized as (Font-Underline = "True" Font-Bold = "True"), rather than the Style property into the same mess sequence (style = " the INDEX-the Z: 101;

the LEFT: 224px; the POSITION: Absolute; the TOP: 240px "), and under normal circumstances, we will give more than one such property and is provided on the other of attribute values :

[NotifyParentProperty (true)] (System.ComponentModel.NotifyParentPropertyAttribute , the default is false)
It is to signal

PropertyGrid sub-attributes from the parent to notify a change.

The default value is DesignerSerializationVisibility DesignerSerializationVisibility.Visible ; and the above-mentioned Hidden value makes the corresponding attribute is not a sequence

of, for example, you do a SQLServerConnection control, you will not want to provide to the user serialization ConnectPassword property, write it in. Gong aspx file

can. At the same time, set the Hidden attribute will not appear in the PropertyGrid.

So if I want to just a property is not displayed in the PropertyGrid, but may be provided and the sequence values in the .aspx file, then you can not use the above-described Attribute

with [Browsable (false)], the default Attribute value is true, full name is System.ComponentModel.BrowsableAttribute .

Then it, programmers generally work is more structured, like me :), then we will want to control the placement of the various properties of different categories in the PropertyGrid to

do this, it is easy, as long as you attribute declaration above note on the following Attribute:

[Category ( "Behavior")] (System.ComponentModel.CategoryAttibute, its default value is "Default"), this sentence means that this property is classified as acts of

this section, please note that the image data, appearance, behavior , miscellaneous (Default) and other categories, you do not have to write [category ( "behavior")] and write [category ( "behavior")],

VS.net will automatically do the localization work.

Then, you might want to write a few attribute Comment, you can use the Attribute:

[the Description ( "Today you write control yet")] (System.ComponentModel. DescriptionAttribute , this property may you also want to be localized, so comments see

people that were so, hell that nonsense, it is also possible, but not so automatic Category, to do more, with respect to localized control, we discuss later)

now, let's look at a few different styles of support PropertyGrid to edit the properties offered:

text box type, drop-down list, pop-up window style.

TypeConverter.gif
Text Box is the default, such as Button's Text;
 
drop-down list type, for example, I wrote RockToControl property RockUControl control, and the properties of selected objects verified Validator controls series;
 
pop-up window type, such as color properties, provide a form to We choose.
 
The first is the default, we do not control, we look at the drop-down, to achieve this effect, use the Attribute like a bee with horses and cattle are not the same:
 
[TypeConverter (typeof (YourCustomConverter))]

As RockToControl attribute my RockUControl control

None.gif         [Bindable( false )]
None.gif        [Category(
" Behavior " )]
None.gif        [DefaultValue(
"" )]
None.gif        [TypeConverter(
typeof (THINControls.WebControls.Designer.FormControlsConverter))]
None.gif        [Description(
" 要滚动的对象。 " )]
None.gif        
public   string  RockToControl
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object o =  ViewState["RockToControl"];
InBlock.gif                
return (o==null)?"":o.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"RockToControl"= value;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

再看FormControlsConverter类(请认真查看我加的注释)
 
None.gif      public   class  FormControlsConverter:StringConverter
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public FormControlsConverter()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//这一个override说明要用下拉列表编辑属性
InBlock.gif
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//这个override返回下拉列表项,有朋友曾问怎么实现拿出当前页面的控件,以下就是我摸索出来的代码。
InBlock.gif
        public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ControlCollection Controls 
= ((Page)context.Container.Components[0]).Controls;
InBlock.gif            ArrayList controlsArray 
= new ArrayList();
InBlock.gif            
for(int i = 0 ;i < Controls.Count ; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if((Controls[i] is HtmlTable
InBlock.gif                    
|| Controls[i] is HtmlForm
InBlock.gif                    
|| Controls[i] is HtmlGenericControl
InBlock.gif                    
|| Controls[i] is HtmlImage
InBlock.gif                    
|| Controls[i] is Label
InBlock.gif                    
|| Controls[i] is DataGrid
InBlock.gif                    
|| Controls[i] is DataList
InBlock.gif                    
|| Controls[i] is Table
InBlock.gif                    
|| Controls[i] is Repeater
InBlock.gif                    
|| Controls[i] is Image
InBlock.gif                    
|| Controls[i] is Panel
InBlock.gif                    
|| Controls[i] is PlaceHolder
InBlock.gif                    
|| Controls[i] is Calendar
InBlock.gif                    
|| Controls[i] is AdRotator
InBlock.gif                    
|| Controls[i] is Xml
InBlock.gif                    ))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    controlsArray.Add(Controls[i].ClientID);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return new StandardValuesCollection(controlsArray);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//return ture的话只能选,return flase可选可填
InBlock.gif
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }


 

弹出窗体的比较麻烦(要用到UITypeEdit,还有要实现ForeColor那种选好颜色会显示一个小方框在属性框中表示颜色的功能也

是),我们在下一篇文章中再谈。另外,颜色选择框是[TypeConverter(typeof(WebColorConverter)],Enum可用TypeConverter

(typeof(EnumConverter))。

此外,我们还要关注一下这几个比较简单的Attribute:

[Bindable(true/false)],绑定数据到属性是否有意义,不过你设为false,用户一样可以在.aspx文件中输入表达式要把属性和一个数

据绑定表达式关联起来。(关于数据绑定,也是后话)
 
[DefaultEvent("Click")],双击控件,进入.cs文件,并开始编辑Click事件代码。

[DefaultProperty("Text")],选择控件,PropertyGrid把Text属性高亮显示。

[EditorBrowsable(EditorBrowableState.Always/Advanced/Never)],代码编辑器(不是后台代码编辑器,是指aspx文件的)是否为属

性、方法、事件提供IntelliSence支持,Default为Always,Advanced只用于VB.net,在用户选择查看高级成员时提供IntelliSence,

Never was not browse IntelliSence information. (I could not give an example of this where used)

[the DefaultValue (PropertyType.ProperDefaultValue)], set the default value of the attribute, the type of note corresponding to the default value of the attribute type, such as the default BorderStyle property

values you can set [the DefaultValue (BorderStyle .NotSet)] and not to write became a [DefaultValue ( "NotSet")]


The next article, we look at:
 
serialized property is a string, and how to interact with the real property type ------ TypeConverter

how to change the default control logic analysis ------ PersistChildren (false ) and analysis of ASP.net ControlBuilder to customize content control tags in the

property editor

......

Reproduced in: https: //www.cnblogs.com/think/archive/2005/05/16/156158.html

Guess you like

Origin blog.csdn.net/weixin_30814319/article/details/94793763