My Web Control Development Experience (2)--General class modification

Source: Internet
Author: User
Tags reflection require serialization

Don't want to have too many unknowns. If you inherit from control, you feel too much of the unknown.
Try 1: Create a normal class library, to be public:







using System;
namespace test1
{
public class Test1
{

}
}

Compiled into a DLL, ready to put on the test ASPX page, the result is not even the toolbox, the error.

View the definition of control, 8 interfaces, first: IComponent







using System;
using System.ComponentModel;

namespace test1
... {
public class test1:icomponent
...{

IComponent members #region IComponent members

public event EventHandler disposed;

Public ISite Site
...{
get
            ...{
Throw the new Exception ("Themethodor operation isn't Implemented. " );
}
            Set
            ...{
Throw the new Exception ("Themethodor operation isn't Implemented. " );
}
        }

#endregion

IDisposable Members #region IDisposable members

public void Dispose ()
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

#endregion
    }


}

there is no error in the toolbox, but you can't see it. is not "one can not be less"? The 8 have been realized and put again, still invisible. The code that reflects control (the reflection tool reflector the resources I upload to http://download.111cn.net/user/whwqs), discovers the metadata of the control head, Add it to the. You can see it in the toolbox.







using System;
using System.ComponentModel;
using System.Web.UI;
using system.web;
using System.ComponentModel.Design.Serialization;
using System.Security.Permissions;

namespace test1
... {
[Designercategory ("Code"), ToolboxItemFilter ("System.Web.UI", ToolboxItemFilterType.Require), Designerserializer ("Microsoft.VisualStudio.Web.WebForms.ControlCodeDomSerializer, Microsoft.VisualStudio.Web, version=8.0.0.0, Culture=neutral, publickeytoken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, version=2.0.0.0, culture=neutral , publickeytoken=b03f5f7f11d50a3a"), Defaultproperty ("ID"), Designer ("System.Web.UI.Design.ControlDesigner, System.Design, version=2.0.0.0, Culture=neutral, publickeytoken= B03F5F7F11D50A3A"), ToolboxItem ("System.Web.UI.Design.WebControlToolboxItem, System.Design, version=2.0.0.0, Culture=neutral, publickeytoken= B03F5F7F11D50A3A"), Themeable (false), bindable (true), AspNetHostingPermission (SecurityAction.InheritanceDemand, level=aspnethostingpermissionlevel.minimal), AspNetHostingPermission (SecurityAction.LinkDemand, level= Aspnethostingpermissionlevel.minimal)]

public class Test1:icomponent, IDisposable, IParserAccessor, IUrlResolutionService, Idatabindingsaccessor, Icontrolbuilderaccessor, Icontroldesigneraccessor, Iexpressionsaccessor
...{

IComponent members #region IComponent members

public event EventHandler disposed;

Public ISite Site
...{
get
            ...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}
            Set
            ...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}
        }

#endregion

IDisposable Members #region IDisposable members

public void Dispose ()
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

#endregion

Icontroldesigneraccessor Members #region icontroldesigneraccessor members

Public System.Collections.IDictionary getdesignmodestate ()
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

public void setdesignmodestate (System.Collections.IDictionary data)
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

public void Setownercontrol (control owner)
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

Public System.Collections.IDictionary UserData
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

#endregion

IParserAccessor Members #region iparseraccessor members

public void addparsedsubobject (object obj)
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

#endregion

IUrlResolutionService Members #region iurlresolutionservice members

public string resolveclienturl (string Relativeurl)
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

#endregion

Idatabindingsaccessor Members #region idatabindingsaccessor members

Public databindingcollection DataBindings
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

public bool Hasdatabindings
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

#endregion

Icontrolbuilderaccessor Members #region icontrolbuilderaccessor members

Public ControlBuilder ControlBuilder
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

#endregion

Iexpressionsaccessor Members #region iexpressionsaccessor members

Public expressionbindingcollection Expressions
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

public bool Hasexpressions
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

#endregion
    }


}

Drag onto the page, say "no public property with ID", and then look at the definition of control, copy its ID, modify it, and change it to:







using System;
using System.ComponentModel;
using System.Web.UI;
using system.web;
using System.ComponentModel.Design.Serialization;
using System.Security.Permissions;

namespace test1
... {
[Designercategory ("Code"), ToolboxItemFilter ("System.Web.UI", ToolboxItemFilterType.Require), Designerserializer ("Microsoft.VisualStudio.Web.WebForms.ControlCodeDomSerializer, Microsoft.VisualStudio.Web, version=8.0.0.0, Culture=neutral, publickeytoken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, version=2.0.0.0, culture=neutral , publickeytoken=b03f5f7f11d50a3a"), Defaultproperty ("ID"), Designer ("System.Web.UI.Design.ControlDesigner, System.Design, version=2.0.0.0, Culture=neutral, publickeytoken= B03F5F7F11D50A3A"), ToolboxItem ("System.Web.UI.Design.WebControlToolboxItem, System.Design, version=2.0.0.0, Culture=neutral, publickeytoken= B03F5F7F11D50A3A"), Themeable (false), bindable (true), AspNetHostingPermission (SecurityAction.InheritanceDemand, level=aspnethostingpermissionlevel.minimal), AspNetHostingPermission (SecurityAction.LinkDemand, level= Aspnethostingpermissionlevel.minimal)]

public class Test1:icomponent, IDisposable, IParserAccessor, IUrlResolutionService, Idatabindingsaccessor, Icontrolbuilderaccessor, Icontroldesigneraccessor, Iexpressionsaccessor
...{

string _id = "id1" ;

[Parenthesizepropertyname (true )]
[Filterable (false )]
[Themeable (false )]
[Mergableproperty (false )]
public string ID
...{
set
            ...{
_id = value;
}
             Get
            ...{
return _id;
}
        }

IComponent Members #region IComponent members

public event EventHandler disposed;

Public ISite Site
...{
get
            ...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}
            Set
            ...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}
        }

#endregion

IDisposable Members #region IDisposable members

public void Dispose ()
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

#endregion

Icontroldesigneraccessor Members #region icontroldesigneraccessor members

Public System.Collections.IDictionary getdesignmodestate ()
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

public void setdesignmodestate (System.Collections.IDictionary data)
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

public void Setownercontrol (control owner)
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

Public System.Collections.IDictionary UserData
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

#endregion

IParserAccessor Members #region iparseraccessor members

public void addparsedsubobject (object obj)
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

#endregion

IUrlResolutionService Members #region iurlresolutionservice members

public string resolveclienturl (string Relativeurl)
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

#endregion

Idatabindingsaccessor Members #region idatabindingsaccessor members

Public databindingcollection DataBindings
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

public bool Hasdatabindings
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

#endregion

Icontrolbuilderaccessor Members #region icontrolbuilderaccessor members

Public ControlBuilder ControlBuilder
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

#endregion

Iexpressionsaccessor Members #region iexpressionsaccessor members

Public expressionbindingcollection Expressions
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

public bool Hasexpressions
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

#endregion
    }


}

Then drag to the page, report other errors, regardless, in the browser view:

And then to the control of the reflection to find the implementation of the AddParsedSubObject, posted over:







using System;
using System.ComponentModel;
using System.Web.UI;
using system.web;
using System.ComponentModel.Design.Serialization;
using System.Security.Permissions;

namespace test1
... {
[Designercategory ("Code"), ToolboxItemFilter ("System.Web.UI", ToolboxItemFilterType.Require), Designerserializer ("Microsoft.VisualStudio.Web.WebForms.ControlCodeDomSerializer, Microsoft.VisualStudio.Web, version=8.0.0.0, Culture=neutral, publickeytoken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, version=2.0.0.0, culture=neutral , publickeytoken=b03f5f7f11d50a3a"), Defaultproperty ("ID"), Designer ("System.Web.UI.Design.ControlDesigner, System.Design, version=2.0.0.0, Culture=neutral, publickeytoken= B03F5F7F11D50A3A"), ToolboxItem ("System.Web.UI.Design.WebControlToolboxItem, System.Design, version=2.0.0.0, Culture=neutral, publickeytoken= B03F5F7F11D50A3A"), Themeable (false), bindable (true), AspNetHostingPermission (SecurityAction.InheritanceDemand, level=aspnethostingpermissionlevel.minimal), AspNetHostingPermission (SecurityAction.LinkDemand, level= Aspnethostingpermissionlevel.minimal)]

public class Test1:icomponent, IDisposable, IParserAccessor, IUrlResolutionService, Idatabindingsaccessor, Icontrolbuilderaccessor, Icontroldesigneraccessor, Iexpressionsaccessor
...{

string _id = "id1" ;

[Parenthesizepropertyname (true )]
[Filterable (false )]
[Themeable (false )]
[Mergableproperty (false )]
public string ID
...{
set
            ...{
_id = value;
}
             Get
            ...{
return _id;
}
        }

IComponent Members #region IComponent members

public event EventHandler disposed;

Public ISite Site
...{
get
            ...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}
            Set
            ...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}
        }

#endregion

IDisposable Members #region IDisposable members

public void Dispose ()
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

#endregion

Icontroldesigneraccessor Members #region icontroldesigneraccessor members

Public System.Collections.IDictionary getdesignmodestate ()
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

public void setdesignmodestate (System.Collections.IDictionary data)
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

public void Setownercontrol (control owner)
...{
Throw the new Exception ("Themethodor operation isn't implemented. ");
}

Public System.Collections.IDictionary UserData
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

#endregion

IParserAccessor Members #region iparseraccessor members

public void addparsedsubobject (object obj)
...{
control Child = obj ascontrol;
if ( child != null )
...{
//this. Controls.Add (child);
            }

}

#endregion

IUrlResolutionService Members #region iurlresolutionservice members

public string resolveclienturl (string Relativeurl)
...{
Throw the new Exception ("Themethodor operation isn't Implemented. " );
}

#endregion

Idatabindingsaccessor Members #region idatabindingsaccessor members

Public databindingcollection DataBindings
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

public bool Hasdatabindings
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

#endregion

Icontrolbuilderaccessor Members #region icontrolbuilderaccessor members

Public ControlBuilder ControlBuilder
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

#endregion

Iexpressionsaccessor Members #region iexpressionsaccessor members

Public expressionbindingcollection Expressions
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

public bool Hasexpressions
...{
get ...   { throw new Exception ("The" themethodor Operation is not implemented. " ); }
        }

#endregion
    }
}

Browse again, no more errors. But in the designer still the error, after analysis I think is icontroldesigneraccessor member void Setownercontrol (Control owner) Check MSDN has a word:

This member is an implementation of an explicit interface member. It can only be used when the control instance is cast to the Icontroldesigneraccessor interface. The control designer performs a design-time operation on the control using the Icontroldesigneraccessor interface.

It seems that it must be an instance of control to be displayed correctly in the designer, but my test1 instance. That is, an instance of a class that imitates its implementation cannot always be displayed correctly in the designer without inheriting from control. Unless you copy a designer. But it's also rewarding: the controls that are rendered in the designer are actually corresponding to an instance in memory. Remember this icontroldesigneraccessor interface. The controls we make may need to be well understood if they are presented in a designer with a different design interface.

Next, there is a thought: I ignore the designer, can render my control to the page? The answer is impossible. Because the control you want to render must be added to the control tree. The loading method, for example: the Controls.Add parameter must be control or its descendants. And Test1 is no way to control.

Conclusion: Under 2.0 or previous frameworks, you must inherit from control to develop Web controls.

Recommend an article: explain inheritance from the control inherited from WebControl different.

Http://www.cnblogs.com/mapserver/articles/392325.html

<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.