Create a SlideShow class in ASP. NET

Source: Internet
Author: User
Tags prototype definition

I. Introduction

On the company website, we often need a mechanism based on Web slides to demonstrate our own products. Of course, you can use common JavaScript to develop such slides. However, with the help of the ASP. net ajax framework, this development work will be greatly simplified. In this example, we want to develop such a simple slide using the Web page method and client script extension technology. End users can play and pause slides, loop playback, and manual control.

2. Create an ASP. net ajax-Enabled website

Start Visual Studio 2005, and select the menu item "file | new website ...", Use the template "ASP. net ajax-Enabled Website" to create a new website, and name the project SlideShow and select Visual C # As the built-in language ). After that, the System should automatically add a reference to the necessary assembly-System. Web. Extension. dll. In addition, you will notice that a ScriptManager Server Control is automatically added to the page. Note that this server control is the control center of the entire ASP. net ajax framework.

Then, add an HTML table with two rows and one column, add a label in the first row, and add six HTML button controls in the second row. 1. shows the general layout of the web form Default. aspx.

3. Create a SlideShow class

Right-click the project to add a new java Script file and name it JScript. js. Here, we will create the SlideShow class, which will be responsible for completing all slide operation tasks-such as playing, holding, and navigation slides. Note that the SlideShow class is developed based on the script extension technology of the ASP. net ajax client. The specific implementation code is as follows:

 
 
  1. Type. registerNamespace ("Demo ");
  2. // Constructor and private variable Declaration
  3. Demo. SlideShow=Function(){
  4. This. _ slides=NewArray ();
  5. This. _ delay=2000;
  6. This. _ currentIndex=0;
  7. This. _ pause=False;
  8. }
  9. // Prototype Definition
  10. Demo. SlideShow. prototype=
  11. {
  12. Get_Slides: function (){
  13. Return this. _ slides;
  14. },
  15. Set_Slides: function (value ){
  16. This. _ slides=Value;
  17. },
  18. Get_Delay: function (){
  19. Return this. _ delay;
  20. },
  21. Set_Delay: function (value ){
  22. This. _ delay=Value;
  23. },
  24. Get_CurrentIndex: function (){
  25. Return this. _ currentIndex;
  26. },
  27. Set_CurrentIndex: function (value ){
  28. If (value <0 ){
  29. ThisThis. _ currentIndex= This. _ slides. length-1;
  30. Return;
  31. }
  32. If (value> = this. _ slides. length ){
  33. This. _ currentIndex=0;
  34. }
  35. Else {
  36. This. _ currentIndex=Value;
  37. }
  38. },
  39. Get_IsPaused: function (){
  40. Return this. _ pause;
  41. },
  42. Set_IsPaused: function (value ){
  43. This. pause=Value;
  44. },
  45. Pause: function (){
  46. This. _ pause=True;
  47. },
  48. Play: function (){
  49. This. _ pause=False;
  50. Window. setTimeout ("slideshow. ShowImage ()",
  51. This. get_Delay ());
  52. },
  53. ShowFirst: function (){
  54. This. _ currentIndex=0;
  55. This. ShowImage ();
  56. },
  57. ShowLast: function (){
  58. ThisThis. _ currentIndex= This. _ slides. length-1;
  59. This. ShowImage ();
  60. },
  61. ShowNext: function (){
  62. VarNewIndex=This. _ CurrentIndex + 1;
  63. This. set_CurrentIndex (newIndex );
  64. This. ShowImage ();
  65. },
  66. ShowPrevious: function ()
  67. {
  68. VarNewIndex=This. _ CurrentIndex-1;
  69. This. set_CurrentIndex (newIndex );
  70. This. ShowImage ();
  71. },
  72. ShowImage: function (){
  73. VarImg= $ Get ("Image1 ");
  74. If (Img. style. visibility= "Hidden "){
  75. Img. style. visibility="Visible";
  76. }
  77. VarSlides=This. Get_Slides ();
  78. VarCurIndex=This. Get_CurrentIndex ();
  79. Img. src=Slides[CurIndex];
  80. If (this. get_IsPaused () = false)
  81. {
  82. This. set_CurrentIndex (curIndex + 1 );
  83. This. Play ();
  84. }
  85. }
  86. }
  87. // Registration type
  88. Demo. SlideShow. registerClass ("Demo. SlideShow ");
  89. // Create a global SlideShow instance
  90. VarSlideshow=NewDemo. SlideShow ();

At the beginning of the code, we first register a new namespace called Demo. Then, create the SlideShow class. The constructor of the SlideShow class declares four private member variables. Among them, the _ slides variable points to an array containing the slide image URL; The _ delay variable indicates the interval between two adjacent slides in milliseconds ); the _ currentIndex variable stores the index values of the current slide in the _ slides array. Finally, the _ pause variable indicates whether the slide is paused (true) or running (false ).

Next, in the SlideShow class prototype, we define the getter/setter method associated with the previous four attributes, that is, Slides, Delay, CurrentIndex, and IsPaused. Other methods are relatively basic, so we only introduce the set_CurrentIndex () method (). This set_CurrentIndex () attribute method is used to check the index value provided to it. If the value exceeds the upper and lower boundary of the slides array, it will adjust the value to 0 or the length of the array minus 1 based on the actual situation ). This is critical, so that the slides can be played cyclically.

Next, the Pause () method simply sets the member Variable _ pause to true-which can control how the slides are paused.
The Play () method is used to Play the slides. It first sets the _ pause variable to false, and then calls the setTimeout () method of the JavaScript Object windows. The setTimeout () method accepts two parameters: the code to be executed after a specific time delay; the corresponding time span after the code is executed is measured in milliseconds ). In this example, the latency value comes from the get_Delay () attribute. The setTimeout () method calls the ShowImage () method.

The ShowImage () method is used to display the core work of an image. It references the CurrentIndex and Slides attributes, and then sets the src attribute of the image label to the appropriate image corresponding to the Slides array. Note that Image1 is the ID of An Image Tag-we will add it later. In addition, pay attention to the usage of the $ get () method, which is equivalent to the document. getElementById () method. Then, add 1 to the CurrentIndex value and call the Play () method again. In this way, an infinite loop will be formed, and the slides will be continuously played.

The last four methods-ShowFirst (), ShowLast (), ShowNext (), and ShowPrevious () are used to simply adjust the value of the _ currentIndex member variable and call ShowImage () method to display a slide.

After the class is created, we use the registerClass () method to register it to the ms ajax framework. Finally, declare a global instance variable of the SlideShow class.

Finally, open the Web page Default. aspx, select the ScriptManager control, set its EnablePageMethods attribute to true, and add the JScript. js file to its script set. The above describes how to create the SlideShow class.

  1. Analysis on ASP. NET Web Security
  2. Session State of ASP. NET
  3. Analysis on the attribute ASP. NET of IsPostBack
  4. ASP. NET architecture and security mechanism
  5. Overview ASP. NET Crystal Reports

Related Article

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.