Udostępnij za pośrednictwem


Walkthrough: JScript IntelliSense

In this walkthrough you will use IntelliSense to support client-script development in Visual Studio. IntelliSense makes language references easy to access. When coding, you do not have to leave the code editor to search for language elements, such as syntax or parameter lists. Instead, you can stay in editing mode, find the information that you need, and insert language elements directly into your code. Visual Studio supports IntelliSense for Microsoft JScript and other ECMAScript languages such as JavaScript.

Note

This document refers specifically to Jscript. However, IntelliSense in Visual Studio and Visual Web Developer works with all ECMAScript languages, which includes JavaScript.

Visual Studio supports IntelliSense for the following features:

  • DHTML Document Object Model (DOM) elements.

  • Intrinsic objects.

  • User-defined variables, functions, and objects.

  • External file references.

  • XML code comments.

  • ASP.NET AJAX objects.

For more information about IntelliSense functionality in Visual Studio, see Using IntelliSense. For information about IntelliSense for JScript, see JScript IntelliSense Overview.

Prerequisites

In order to complete this walkthrough, you need the following components:

  • Visual Studio 2010 or Microsoft Visual Web Developer Express.

Creating the Web Site and Page

To begin, you will create an ASP.NET Web site and include supporting objects, references, and script. If you have already created a Web site (for example, by following the steps in Walkthrough: Creating a new Web Site), you can use that Web site and go to the next section of this walkthrough. Otherwise, create a new Web site and page by using the following steps.

To create the Web application

  1. Open Visual Studio 2010 or Microsoft Visual Web Developer Express.

  2. On the File menu, click New Web Site.

    The New Web Site dialog box is displayed.

  3. Under Visual Studio installed templates, click ASP.NET Web Site.

  4. From the Location list, select File System.

  5. In the box next to the Location list, type the name of the folder where you want to keep the pages of the Web site.

    For example, type the following folder name: C:\IntellisenseWebSite1

  6. From the Language list, click Visual Basic or Visual C#, and then click OK.

    The programming language that you select will be the default for creating the server-based code for the Web site. In this walkthrough, the choice is not important, because you will be working only with client script, which runs in the browser.

    Visual Web Developer creates the folder and a new page named Default.aspx. By default, when a new page is created, Visual Web Developer displays the page in Source view, where you can see the page's HTML elements.

Adding Controls and JScript Code to the Page

You will now add controls and JScript code to the Default.aspx page.

To add controls and JScript code

  1. Click the Design tab to switch to Design view.

  2. From the AJAX Extensions tab in the Toolbox, drag a UpdatePanel control onto the page.

  3. From the Standard tab in the Toolbox, drag the following controls onto the page:

    • A Label control for the title.

    • A Button control to calculate the volume.

    • A Label control and a TextBox control for a caption and input.

    • A Label control and a TextBox control for a caption and output.

  4. Set the Text property of the controls to the following values:

    • Button1: Calculate

    • Label2: Radius Input

    • Label3: Volume Output

  5. Click the Source tab to switch to Source view.

  6. Add the following highlighted script reference to the asp:ScriptManager element:

    <asp:ScriptManager ID="ScriptManager1" runat="server">
      <Scripts>
        <asp:ScriptReference Path="JScript.js" />
      </Scripts>
    </asp:ScriptManager>
    
  7. In Solution Explorer, right-click the Web site name and then click Add New Item.

    The Add New Item dialog box is displayed.

  8. Under Visual Studio installed templates, select JScript File and then click Add.

    Visual Studio creates and opens a new file that is named JScript.js.

    Note

    The name of the .js file must match the name that appears in the script reference in the asp:ScriptManager element.

  9. Add the following code to the JScript.js file:

    function calcVolume(areaValue)
    {
      /// <summary>Determines the volume of a cicle based on an area parameter.</summary>
      /// <param name="area" type="Number">The area of the circle.</param>
      /// <returns type="Number">Returns a number that represents the area.</returns>
      var volumeVal;
      volumeVal = Math.pow(areaValue,3);
      return volumeVal;
    }
    
  10. Save and close the JScript.js file.

Viewing JScript IntelliSense

JScript IntelliSense is used to display details about many types of client objects, such as Dynamic HTML (DHTML) Document Object Model (DOM) elements, intrinsic objects, and user-defined objects. You can also use IntelliSense to display information about XML-commented script and about Microsoft Ajax Library objects.

To view JScript IntelliSense

  1. Switch to or open the Default.aspx page and switch to Source view.

  2. Add the following script element at the end of the form element:

    <script type="text/javascript">
    
    </script>
    
  3. Inside the script element, type the following script:

    var displayTitle = document.
    

    When you type the period (.), the editor displays IntelliSense options that are appropriate to the document object.

  4. Scroll to the getElementById method and click the method or press ENTER to add the getElementById method to the script.

  5. Complete the line of script by typing the name of an element to find, so that the line appears as follows:

    var displayTitle = document.getElementById("Label1");
    
  6. Add the following line of script to display the title in Label1:

    displayTitle.innerHTML = "Calculate Volume";
    
  7. Add the following incomplete function to the end of the script element:

    function calcArea(radiusParam)
    {
    
    }
    
  8. In the calcArea function, type the following script:

      var areaVal = Math.
    

    When you type the period (.), the editor displays IntelliSense options that are appropriate to the Math intrinsic object.

  9. Scroll to the PI property and press ENTER to add the PI property to the script.

  10. Complete the script so that the function appears as follows:

    function calcArea(radiusParam)
    {
      var areaVal = Math.PI * radiusParam * radiusParam;
      return areaVal;
    }
    
  11. Add the following incomplete function to the end of the script element:

    function displayVolume()
    {
    
    }
    
  12. In the displayVolume function, type the following script:

      radiusVal = $get("TextBox1").value;
      var areaVal = calcArea(
    

    When you type the opening parenthesis, the editor displays IntelliSense information about the parameter value for the calcArea function that you created earlier.

  13. Complete the line of script so that the script appears as follows:

      var areaVal = calcArea(radiusVal);
    
  14. Continue adding to the displayVolume function by typing the following script:

      var volumeVal = calcVolume(
    

    When you type the opening parenthesis, the editor uses IntelliSense to display XML code comments that are based on the calcVolume function that you created earlier.

  15. Complete the displayVolume function so that the function appear as follows:

    function displayVolume()
    {
      radiusVal = $get("TextBox1").value;
      var areaVal = calcArea(radiusVal);
      var volumeVal = calcVolume(areaVal);
      $get("TextBox2").value = areaVal;
    }
    

    Note

    The $get method is an ASP.NET AJAX function that provides a shortcut to the getElementById method of the Sys.UI.DomElement class.

  16. Type the following line of script at the end of the script element:

    $addHandler(
    

    When you type the opening parenthesis, the editor displays IntelliSense for the ASP.NET AJAX $addHandler method.

  17. Complete the line of script so that the script appears as follows:

    $addHandler($get("Button1"), "click", displayVolume);
    

    Make sure that this line is inside the </script> tag but not inside the displayVolume function.

    Note

    The $addHandler method is an ASP.NET AJAX function that provides a shortcut to the addHandler method of the Sys.UI.DomEvent class.

  18. Save the page and then press CTRL+F5 to run it.

  19. Enter a value for the radius and then click the button.

    The result of the calculations that you created in JScript is displayed in the second text box.

Next Steps

This walkthrough has illustrated how to work with JScript IntelliSense. You might want to learn more about how to work with client script in Visual Studio. For details, see Adding Client Capabilities and AJAX. For more information about JScript IntelliSense, see JScript IntelliSense Overview.

See Also

Tasks

Dynamically Assigning Script References

Concepts

JScript IntelliSense Overview

List Members

Other Resources

Using IntelliSense

About the DHTML Object Model

JScript Objects

SRC Attribute | src Property