VisualJS.NET 自定义控件开发



点击句子查看译文 显示原文      显示译文      双语对照    源址


In this article i would like to show you how to use custom JavaScript controls under VisualJS.NET. here i'm going to make a custom control based on Spinner from order to make this article easier for the interested developers, i am going to use the sample project from VisualJS.NET's download that is also available under my package.since this project includes premade custom controls like Calendar, Color picker, Slider etc. it will help us to create our custom control.


C#, JavaScript

Creating a Custom Control in a few steps

Once we have WebControls project opened in Visual Studio we need to copy a few files to the appropriate folders such as :

  • css - put a file jquery-ui-1.9.0.custom.min.css from JQueryUI/Spinner/css
  • img - put all images from JQueryUI-Spinner/css/Images
  • js - put a file jquery-ui-1.9.0.custom.min.js from JQueryUI-Spinner/js

Although using these folders is not mandatory, i would like to keep files in right folders.

You can assume your custom control as the combination of render and actions required for the expected functionality and look.for this reason we need to make the premade JQUERY Spinner component talking to VisualJS.NET in the way we wanted.Thus, we need to create a definition of the spinner at least in two parts, the JavaScript part and. NET part.JavaScript part will be the spinner.js file and. NET part is in Component class file.

JavaScript part

First of all, every custom control class inherit from VSJS_CBase in order to have base functionalities provided by VisualJS client side interface.

VSJS_Spinner.prototype = new VSJS_CBase(); 

VisualJS.NET renders objects when they are needed or can still communicate to controls like they are rendered but they will be rendered by real when their visibility makes sense.Based on this fact, we can consider the spinner object will be created after the base component is rendered and this can be checked from the internal event OnRenderCompleted.

this.OnRenderCompleted = function () {
 stop: function (event,ui) {
 this.OnChanged(this.Name, null);
 },start: 0 });

In order to transfer values between server side (.NET part) and JQueryUI-Spinner component, i had to declare methods and properties on JavaScript part.the following code shows how to declare properties and methods.Also, i wanted trigger OnChanged event when one of the PageUp or PageDown actions occur.

VSJS_Spinner.prototype.Page = function (_page) { 
 if (this._Rendered) { 
 $(this.input).spinner({ page: _page }); 
VSJS_Spinner.prototype.PageUp = function () {
 if (this._Rendered) {
 this.OnChanged(this.Name, null);

.NET part

To operate the properties and events on the server side, i added a new Component class.the Component class - Spinner is derived from VisuaJS.TextBox and is added into the Web order to integrate. Net part with the JavaScript part the constructor of the Spinner component has to call base constructor with the Spinner as control type name.VisualJS.NET searches client for VSJS_Spinner in order to create new instances of this control every time.

public Spinner() : base("Spinner") 

These next few tips will allow us to properly configure and register the component Spinner.for this purpose, we use several methods and properties of the namespace VisualJS.Kernel.the JQueryUI Spinner needs JQuery so the following code shows how to enable jQuery support.(GlobalAsaxHelper file under VisualJS folder is the best location for the below call )

VisualJS.Kernel.Settings.JQuerySupport = true; 

From the namespace VisualJS.Kernel there are methods to easily merge files such as js, css etc. i use it to merge JQueryUI Spinner files with mine.please keep in mind that all the resource files (JS, CSS etc) must be marked as"Embedded Resource"on Visual Studio -> File Properties Window -> Build Actions

//js filesComponentRegister.Merge(new string[]{"WebControls.spinner.js.jquery-ui-1.9.0.custom.min.js","WebControls.spinner.js.spinner.js"}, "WebControls.spinner.js", "js", Encoding.UTF8);//css filesComponentRegister.Merge(new string[]{ "WebControls.spinner.css.spinner.css", "WebControls.spinner.css.jquery-ui-1.9.0.custom.min.css" },"WebControls.spinner.css", "css", Encoding.UTF8);

Then i had to register the Spinner folder using RegisterFolder method.Besides, to show images needed, i replaced the path of images in a JQueryUI-Spinner CSS file.that's all what we need to configure and register the component in. NET part.

Assembly appAsm = Assembly.GetExecutingAssembly();
ComponentRegister.RegisterFolder(appAsm,"WebControls.spinner");string CSS = ComponentRegister.GetText("WebControls.spinner.css", Encoding.UTF8);
string[] resNames = appAsm.GetManifestResourceNames();foreach (string resName in resNames) 
 string fileName = resName.Replace("WebControls.spinner.img.","");
 CSS= CSS.Replace("images/" + fileName, 
 "[$$VisualJSResourceTarget$$]?name="+ resName + "&type=image");
ComponentRegister.SetText("WebControls.spinner.css",CSS, Encoding.UTF8);this.ClientLoadCheck("WebControls.spinner.js",ClientSideDocuments.Script);this.ClientLoadCheck("WebControls.spinner.css", ClientSideDocuments.CSS);

On server side i used few methods and properties from JQuery-Spinner so let's see how to declare property and method on server side.we can transfer a value of the property using method SetProperty and to call method using SetMethod method.

int page = 1;publicint Page
 get {
 return page;
 set {
 if (page!= value) 
 SetProperty("Page", value);
 page = value;
}publicvoid PageUp()
Testing the Control
  • Add to solution a new project choosing the ASP.NET Web Application template.
  • Add VisualJS.NET Support
  • Add reference to the WebControls project.
  • Add VisualJS.NET Form
  • Spinner is on the toolbox thus we can put it on the form.
  • You can play a bit changing it's property e.g. Page, using method PageUp or implementing TextChange event.