一個簡單的WEB應用 VS2005

  1. 下載Visual Studio 2005 Web Application Projects 插件.
  2. 啓動Visual Studio 2005.
  3. File]-->[New]-->[Project].
  4. 選擇 [Visual Basic] 的Project Type
  5. 選擇[ ASP.NET Web Application]的Template

    Visual Studio 會創建一個單頁的WEB應用程序,並且設計器已經建立好了初步的代碼框架

  6. 點擊底部的[Design]即可切換到 WYSIWYG (所見即所得)視圖.
  7. 點擊設計界面,入如下Enter your name: .
  8. 從工具箱拖一個Textbox控件到設計界面, 放在剛剛的 Enter your name: 後面.
  9. 從工具箱拖一個Button 控件到設計界面.放在Textbox的下一行
  10. 從工具箱拖一個Label 控件到設計界面, 放在Button的下一行.

      頁面佈局
    1. 選中頁面上的內容,按 DELETE 清除.
    2. Layout菜單中選擇 Insert Table.
    3. Rows上鍵入 2.
    4. Columns上鍵入 2.
    5. 單擊OK.

    6.  可以在表格單元中加入任意的控件,會起到很好的對齊作用,因此,表格是控制WEB頁面佈局很重要的機制 !

    事件驅動

    1. 雙擊Button控件以編輯 Click事件
    2. 鍵入代碼:Label1.Text = "Welcome " & TextBox1.Text
    3. 按F5 運行.
    4. 在 User Name 文本框中輸入名字.
    5. 單擊Button控件

       

    使用SESSION

    1. In Visual Studio 2005, in the Solution Explorer, right-click Default.aspx, and then click View Code.
    2. On a line after Inherits System.Web.UI.Page, enter the following code.
      Public previousUser As String
      
    3. Modify the code for the Button1_Click event handler as follows.
      Label1.Text = "You were " & previousUser & ", and now you're " & TextBox1.Text
      previousUser = TextBox1.Text
      
    4. Press F5 to run the application.
    5. Enter Fred and click the button.
    6. Enter Jim and click the button.

      Notice that the previousUser value is never showing up. It is always saying You were , and now you're Jim. What is going on?

    7. Modify the code for the Button1_Click event handler as follows.
      Label1.Text = "You were " & Session("previousUser") & ", and now you're " & TextBox1.Text
      Session("previousUser") = TextBox1.Text
      
    8. Press F5 to run the application.
    9. Enter Fred and click the button.
    10. Enter Jim and click the button.

      Now you are getting the correct behavior (see Figure 5). By using Session variables, the site is able to remember information across user interactions.

       

    使用數據庫

     you need to download and install SQL Express and the sample Northwind database.

    1. In the Solution Explorer, on the Project menu, click Add New Item.
    2. For Template, select WebForm, and then click Add.
    3. Click the Design tab at the bottom of the code editor window.
    4. From the Data section of the Toolbox, drag a GridView control onto the designer.
    5. From the GridView Tasks smart tag, click Choose Data Source, and then click <New data source>.
    6. In the Choose a Data Source Type dialog box, select Database, and then click OK.
    7. Click New Connection.
    8. For Server Name, enter localhost if you have SQL Server installed. If you have SQL Express installed, enter ./sqlexpress.
    9. For Select or enter a database, enter Northwind.
    10. Click Test Connection to ensure that you can connect to the database.
    11. Click OK.
    12. Click Next.
    13. In the Save the Connection String dialog box, click Next.
    14. In the Name drop-down list, click Orders.
    15. For Columns, select *.
    16. Click Next.
    17. Click Test Query to ensure that communication with the database functions as expected.
    18. Click Finish.  
    19. Right-click the GridView control, and then click Auto Format.
    20. Choose one of the formats, such as Professional.
    21. Right-click the GridView control, and then click Properties.
    22. In the Properties Panel, set the AllowPaging property to True.
    23. Set the AllowSorting property to True.
    24. Press F5 to run the Web application.
    25. The application should appear as shown in Figure 6.

         

      Click here for larger image

      You can click column headings to sort on that column. You can click on the page numbers at the bottom to page through the results.

    ASP.NET 2.0 的其他重要特性

    Master Pages

    With Desktop applications, it is easy to simply put the appropriate menus and toolbars on individual forms. With Web sites, however, the navigation is typically more global. A single navigation bar is often displayed on all pages of the site. ASP.NET provides the capability to do this through Master pages. You simply add a Master page as an item to your solution. You use tables on the Master page to control where the header, navigation bar, footer, main page content, and so on should go. Then, you have a particular content page reference the Master page. The page content will appear inside the Master page, in the content area.

    Figure

  11.  

    Authentication and Authorization

    It is very common to have a site, or portion of a site, that requires a user name and password in order to gain access. ASP.NET 2.0 provides extensive support for this scenario. First, ASP.NET gives you a site administration tool that includes a Wizard that will walk you through configuring sections of your site for membership

  12. Security Setup Wizard

     

    小結

    Web development is different from Desktop development. However, the ASP.NET programming model provides a number of striking similarities. When building Web sites, you simply wire up event handlers for controls, just like you would do with desktop applications. Managing state is different, because pages are created and destroyed on each request, but ASP.NET does provide the Session variable to let you persist information in memory on the server. ASP.NET also includes simple-to-use yet powerful controls for common scenarios, such as building data-centric Web sites. Finally, ASP.NET includes full wizards to walk you through site configuration for complex tasks, such as enabling authentication and authorization.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章