After the client behavior is completed, we need to encapsulate it into a server-side control using a lot of infrastructure provided by ASP. NET Ajax Control Toolkit, that is, the base class. No, this is not professional.
Create a Visual Studio Project
This time we need to write C #CodeTherefore, Visual Studio is indispensable. Create a class library project named popupnotificationextender. Then we need to do the following:
- Add ajaxcontroltoolkit. dll obtained by compiling ASP. NET Ajax Control Toolkit to the project and copy it to the project folder.
- Add a reference to the ajaxcontroltoolkit. dll in the project.
- Create the popupnotificationextender. CS file.
- Create the popupnotificationextenderdesigner. CS file.
- Add the popupnotifnotifextenderbehavior. js file compiled in the previous section to the project.
After all the steps are completed, the solution manager in Visual Studio shows the results of the preceding five steps.
Popupnotificationextender. CS File
After completing the preparations, let's compile the core popupnotificationextender. CS file. The first is a lot of Using. Pay attention to the ajaxcontroltoolkit. Just introduce the ASP. NET Ajax Control Toolkit namespace:
// (C) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive license.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
UsingSystem;
UsingSystem. Web. UI. webcontrols;
UsingSystem. Web. UI;
UsingSystem. componentmodel;
UsingSystem. componentmodel. design;
UsingAjaxcontroltoolkit;
Declare a namespace
Then declare the namespace and notice the previous [Assembly: system. web. UI. webresource ("dflying. ajax. popupnotificationextenderbehavior. JS "," text/JavaScript ")] is used to embed the resource file, that is, the Javascript file written earlier. In Visual Studio, select popupnotificationextenderbehavior. js. In the Properties window, you can see that build action is embedded resource ,.
[Assembly: system. Web. UI. webresource (
"Dflying. Ajax. popupnotificationextenderbehavior. js",
"Text/JavaScript")]
NamespaceDflying. Ajax
{
//........
}
Declare popupnotificationextender class
The above namespace declares the popupnotificationextender class, which inherits from alwaysvisiblecontrolextender, because the client's dflying. Ajax. popupnotificationbehavior is also inherited from ajaxcontroltoolkit. alwaysvisiblecontrolbehavior:
[Designer (Typeof(Popupnotificationextenderdesigner)]
[Clientscriptresource ("Dflying. Ajax. popupnotificationbehavior","Dflying. Ajax. popupnotificationextenderbehavior. js")]
[Targetcontroltype (Typeof(Panel)]
[Requiredscript (Typeof(Blockingscripts)]
Public ClassPopupnotificationextender: alwaysvisiblecontrolextender
{
//....
}
The class declaration is very simple, but a bunch of attributes are added above. Here I will briefly explain:
- [Designer (typeof (popupnotificationextenderdesigner)]: This is used to declare the designer of the control in Visual Studio. The popupnotificationextenderdesigner class will be discussed later.
- [Clientscriptresource ("dflying. Ajax. popupicationicationbehavior", "dflying. Ajax. popupnotifextextenderbehavior. js")]: This is used to register the client JavaScript behavior of the extender. Note that the first parameter is the class name of the client and the second parameter is the resource name. Please refer to the definition at the end of the popupnotificationextenderbehavior. js file and write it carefully. Do not make any mistakes.
- [Targetcontroltype (typeof (panel)]: This is used to specify the type of ASP. NET Server-side controls that the extender can apply to. Here we are limited to panel. In actual development, friends can choose their own Based on the situation.
- [Requiredscript (typeof (blockingscripts)]: Blockingscripts is an auxiliary script in ASP. NET Ajax Control Toolkit. It is used to handle browser compatibility issues related to block display areas. Here we need assistance, so we will introduce it.
Declare attributes of extender
The Extender attributes declared here are used to wrap the attributes defined in popupnotificationextenderbehavior. js. For example, the servicepath attribute encapsulates the following two Javascript methods (for Javascript, refer to the previous article ):
Get_servicepath:Function(){
Return This. _ Servicepath;
},
Set_servicepath:Function(Value ){
If(This. _ Servicepath! = Value ){
This. _ Servicepath = value;
}
},
Return to popupnotificationextender. CS, and declare the corresponding servicepath attribute as follows:
[Extendercontrolproperty]
[Defaultvalue ("")]
Public StringServicepath
{
Get
{
ReturnGetpropertyvalue <String> ("Servicepath",String. Empty );
}
Set
{
Setpropertyvalue <String> ("Servicepath",Value);
}
}
The [extendercontrolproperty] attribute is used to specify that this attribute will be associated with the Javascript behavior attribute on the client, which serves as a bridge. [Defaultvalue ("")] is used to set the default value. Here it is an empty string.
The getpropertyvalue and setpropertyvalue methods in getter and setter are used to read/set the corresponding property values of the client JavaScript behavior.
For example, if the default value of an attribute is not null:
[Extendercontrolproperty]
[Defaultvalue (0.3f)]
Public FloatResizeeffectduration
{
Get
{
Return(Float) Getpropertyvalue ("Resizeeffectduration", 0.3f );
}
Set
{
Setpropertyvalue <Float> ("Resizeeffectduration",Value);;
}
}
The code for other attributes is similar and will not be listed here. If you need a friend, please download the source file and check it yourself.
Popupnotificationextenderdesigner. CS File
There is nothing to say about the designer file, and now it is basically blank. If you want to make your control more professional, add more. Due to space limitations, only a blank space is left here:
// (C) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive license.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
UsingSystem. Web. UI. webcontrols;
UsingSystem. Web. UI;
NamespaceDflying. Ajax
{
ClassPopupnotificationextenderdesigner: ajaxcontroltoolkit. Design. extendercontrolbasedesigner <popupicationicationextender>
{
}
}
So far, the entire extender compilation is complete!
Source codeDownload
Http://files.cnblogs.com/dflying/PopupNotificationExtender_source.zip
Next I will talk about the usage and some in-depth implementation principles.