asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Visual Studio 2005 Hands-On Tutorial - Part 3
By David Catherman
Rating: 3.6 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article


    Introduction

    This is the third part in a series of hands-on tutorials that will take you through all the steps to build a complete application using the new SQL Server 2005 database and Visual Studio 2005 IDE tools. This time you will be working with a Web interface to the application.

    The .NET WebLinks project is a collection of Internet sources for information about .NET 2.0, Visual Studio 2005 and SQL Server 2005. Links to these articles are collected into a searchable database and categorized by subject. The project will allow all the .NET enthusiasts to add to the database, so that it will grow to become a very valuable tool for the .NET community.

    Part 1 defined the database using SQL Server 2005 and business logic using datasets.

    Part 2 is a Windows interface to the database, allowing individuals to import links from their Favorites directory, edit the links and upload to the master database.

    Part 3 is a Web based interface to allow the general community access to search the database for the desired links.

    Part 4 will define the Web services for the project, and how to consume them.

    Part 3 - Web Interface Tutorial

    Note: This tutorial builds on the work completed in Part 1 but not Part 2. If you did not complete the tutorial in Part 1, you may download the source code for it here and continue with this tutorial.

    The ASPX engine in Visual Studio 2005 has been greatly improved with many tools added to automate much of the mundane code. Much of the web page can be generated without writing any code.

    1. Open Visual Studio 2005 development environment by selecting it under All Programs / Visual Studio 8 / Visual Studio 2005.

    2. If you have not done so yet, use the following procedure to setup a properly structured Solution with multiple projects.

      From the File Menu, select New, Project...

    3. In the left pane choose Visual Studio Solutions under Other Project Types, and in the right pane, Blank Solution. Name the new solution "WebLinks" and choose a directory where you wish to store the solution.

    Create the Web User Interface

    1. In Visual Studio, right click on the solution and Add a New Web

    2. Select an ASP.NET Web Site, using the File System, and choose the folder where you want it (create a new folder called Web under the Weblinks directory.)

    3. Visual Studio usually opens the default form in HTML Source mode. Using the tab in the lower left corner, switch to the Design view. With version 2.0, Microsoft has come much closer to allowing complete design control with out needing to edit the HTML.

    4. The goal is to create a web page that is similar to the Windows Forms version. There will be a treeview on the left to select the topic, a grid view showing the list of article titles, and a Design view to show the information about each article. Start by dragging a table from the HTML controls section of the Toolbox and dropping it on the form. This will allow the control of the layout of the form.

      (Note: you can save time by pasting in the code below into the <Table> section in the Source view instead of the following steps.)

    5. The Table control defaults to 3 columns and 3 rows but only two columns are needed. This one place where the Design view does not have enough control, so switch back to Source view. You will see the table with 3 rows (marked as <tr> and inside of the rows are 3 columns marked <td>. Delete the <td></td> set for the 3rd column in each row.

    6. Switch back to Design view. Highlight (select) all of the rows and columns and set the border color to Silver

    7. Drag a Div control from the HTML section of the Toolbox to each cell of the table (6 of them total).

    8. Select each Div and change the Height and Width properties by clicking in the Style property and using the Position tab of the Style Builder as follows:

      30x200 30x500
      200x200 200x500
      200x200 200x500

    9. Select the middle two Div controls and change the Overflow property (on the layout tab of the Style Designer) to "Always use Scrollbars".

      The following code should result if you view it in Source view.

      <table style="width: 283px; height: 239px">
          <tr>
              <td bordercolor="silver" >
                  <div style="width: 200px; height: 30px">
                  </div>
              </td>
              <td bordercolor="silver">
                  <div style="width: 500px; height: 30px">
                  </div>
              </td>
          </tr>
          <tr>
              <td bordercolor="silver">
                  <div style="width: 200px; height: 200px; overflow: scroll;">
                  </div>
              </td>
              <td bordercolor="silver">
                  <div style="width: 500px; height: 200px; overflow: scroll;">
                  </div>
              </td>
          </tr>
          <tr>
              <td bordercolor="silver">
                  <div style="width: 200px; height: 150px">
                  </div>
              </td>
              <td bordercolor="silver">
                  <div style="width: 500px; height: 150px">
                  </div>
              </td>
          </tr>
      </table>

      (As you switch back and forth between Design view and Source view, notice that the same controls that are selected in one view will remain selected in the other view.)

    10. Back in Design view, start filling each Div with the proper control. In the Upper left cell, drag a Label control from the Standard section of the Toolbar. Set the properties: Font size to X-Large, and the Text to ".NET WebLinks".

    11. In the upper right cell, drag a Label, TextBox, and Button control. Set the Label to Text of "Search", make the TextBox have a width of 350 and set the Text of the button to "Go".

    12. In the middle left cell, drop a TreeView control (from the Navigation section).

    13. The treeview cannot be bound to a regular data source, but only a XML or SiteMap source view. So for now the treeview must be populated from code. Right click on the Design view and select View Code.

    14. Add a reference to the Biz project. Right click on the project and select Add Reference. In the dialog, select the Projects tab and the Biz project.

    15. In the code window, past the following code inside the Default class:

      Partial Class _Default
          Inherits System.Web.UI.Page

          Dim ds As New Biz.WebLinksDataSet

          Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
                  Handles Me.Load
              If Not IsPostBack() Then
                  Dim dt As New Biz.WebLinksDataSet.TopicDataTable
                  dt = ds.Topic.GetData
                  FillTreeview(dt)
              End If
          End Sub

          Protected Sub FillTreeview(ByVal dt As Biz.WebLinksDataSet.TopicDataTable)
              For Each row As Biz.WebLinksDataSet.TopicRow _
                      In dt.Select("ParentTopicID Is null")
                  Dim newNode As New TreeNode(row.TopicName, row.TopicID)
                  FillChildNodes(dt, newNode, row.TopicID)
                  TreeView1.Nodes.Add(newNode)
              Next
          End Sub

          Protected Sub FillChildNodes(ByVal dt As Biz.WebLinksDataSet.TopicDataTable, _
                  ByVal Node As TreeNode, ByVal ID As Integer)
              For Each row As Biz.WebLinksDataSet.TopicRow In dt.Select("ParentTopicID=" & ID)
                  Dim newNode As New TreeNode(row.TopicName, row.TopicID)
                  FillChildNodes(dt, newNode, row.TopicID)
                  Node.ChildNodes.Add(newNode)
              Next
          End Sub

      End Class

      The page load builds a DataTable, populates it with a method from the dataset defined in the Business section. The FillTreeView method fills the top level of the treeview and recursively calls the FillChildNodes method to complete the treeview.

    16. Build the project (from the top line Build menu). Right click on the screen and select View In Browser to test the screen so far.

    17. Add a ObjectDataSource to the bottom of the form from the Data section of the Toolbox and name it ObjectDataSource1. Click the smart tag (small right arrow) and select Configure Data Source.

      Note: some versions have a DataSource object and a second dialog to allow you to choose the type of data source. Otherwise, skip to the next step.

    18. From the first dialog, select Object type to connect to our business tier dataset.

    19. In the Configure Data Source wizard, from the list, select the TopicLinkTableAdapter from the Biz project.

    20. Click the Next button and select the GetByTopicID method as the Select operation. The other operations will fill in automatically from the Table Adapter.

    21. Click Next and proceed to the Define Parameters screen. For the Parameter source type, select Control from the dropdown list.

    22. In the ControlID dropdown list, select the Treeview1 control. Because the treeview was constructed with the Text property of the TopicName and the Value property of the TopicID, the control will show the value of the TopicID of the selected node. This will be passed as a parameter to the Object Data Source which will retrieve the records for the GridView from the method selected.

    23. In the middle right cell, drag and drop a GridView control from the Data section of the Toolbox. The popup smart task list will prompt you to select a data source. Pick the ObjectDataSource1 just created.

    24. The Wizard throws in all the columns of the table. Select the Edit Columns option from the Smart Task List.

    25. Notice the other options on this list that save a lot of development time. You can add paging, sorting, editing, etc. without writing any code. Check the Enable Selection option so the user can select the desired article from the list.

    26. In the list of selected fields in the lower left section, delete all the columns except the LinkTitle field and the Select column.

      The Design view should now look something like this:

    27. Add a new Object Data Source to the bottom of the screen and name it ObjectDataSource2. (Actually you can put it anywhere, but the bottom of the screen is out of the way.)

    28. Choose Configure DataSource, select the Biz.WebLinksDataSet+LinkDataTable and in the next screen, set the Select operation to GetByLinkID method.

    29. In the final screen, fill the parameter from a ControlID and select the GridView1.

      Regardless of which columns are in the GridView, the selected value will be the primary key of the table to which it is bound. But what we need is the LinkID. This could be solved by writing a couple lines of code to pass the correct parameter when the row in the grid is selected. An easier way is to modify the query to return the LinkID in the primary key field. There is not need for the TopicLinkID field and the LinkID will be unique when selecting by TopicID, so it will work as a primary key.

    30. From the Data section of the Toolbox, drag a DetailsView control to the bottom right cell of the table. Set the Data Source to the ObjectDataSource2 just created.

    31. From the smart tag, select Edit Fields and remove all the fields except LinkTitle, Description, URL, ArticleDate, and AvgRating.

    32. Size the Design view to fill the cell and save your project. Right click and select View in Browser to see the results. Selecting a node in the treeview will show a list of titles in the grid and selecting a row in the grid will show the details for the link in the Design view control. Close the browser and work on the last cell in the table.

    33. In the lower left cell, add a label with the text "Rate This Article", and a Radio button list

    34. Select the Radio button list and from the smart tag (or the items collection in the property panel) select Edit Items. Create 5 items with the text and value of 1-5.

    35. Set the AutoPostBack property to true and double click on the Radio button list to create a code stub to handle the SelectedIndexChanged event. Add the following code to submit the vote on the article.

      Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, _
              ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged
          Dim LinkID As Integer = GridView1.SelectedDataKey.Value
          ds.SubmitRating(LinkID, RadioButtonList1.SelectedValue)
      End Sub

    36. Double click on the Open Article button to build a stub for the Click event. Add the following code to open the selected web site.

      Protected Sub OpenButton_Click(ByVal sender As Object, ByVal e As _
              System.EventArgs) Handles OpenButton.Click
          System.Diagnostics.Process.Start(DetailsView1.Fields.Item(0).ToString)
      End Sub

    37. To handle the search by key word function, add another ObjectDataSource object to the bottom of the Design View form and give it an DataSourceID of ObjectDataSource3.

    38. For the business object, choose Biz.WebLinksDataSetTableAdapters.TopicLinkTableAdapter and for the Select method, choose GetByKeyWords.

    39. On the Parameters screen, select the source of Control and the ControlID of TextBox1 and click Finish.

    40. To implement the Search code, double click on the Go button to build a stub for the click event and switch the Grid to use the new data source with the following code:

      Protected Sub SearchButton_Click(ByVal sender As Object, _
              ByVal e As System.EventArgs) Handles SearchButton.Click
          If GridView1.DataSourceID <> "ObjectDataSource3" Then
              GridView1.DataSourceID = "ObjectDataSource3"
          End If
      End Sub

    41. When the user clicks back on the tree view, you need to switch the Grid View data source back to the original data source with the following code:

      Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, _
              ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
          If GridView1.DataSourceID <> "ObjectDataSource1" Then
              GridView1.DataSourceID = "ObjectDataSource1"
          End If
      End Sub

    42. Build the project and open in a browser to test. It should look something like this:

    About the Author

    David Catherman - CMI Solutions

    Email: DCatherman (at) CMiSolutions (dot) com

    David Catherman has 20+ years designing and developing database applications with specific concentration for the last 4-5 years on Microsoft .NET and SQL Server. He is currently Application Architect and Senior Developer at CMI Solutions using Visual Studio 2005 and SQL Server 2005. He has several MCPs in .NET and is pursuing MCSD.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jul 21, 2005 - N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005 - Part 1
    While the .NET Framework made building ASP.NET applications easier then it had ever been in the past, .NET 2.0 builds on that foundation in order to take things to the next level. This article shows you to how to construct an N-Tier ASP.NET 2.0 Web application by leveraging the new features of ASP.NET 2.0 and SQL Server 2005.
    [Read This Article]  [Top]
    Apr 28, 2005 - New Files and Folders in ASP.NET 2.0
    With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
    [Read This Article]  [Top]
    Mar 10, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2, Cont'd
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 9, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 3, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1, Cont'd
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Mar 2, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Feb 16, 2005 - Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
    In ASP.NET 2.0 and Visual Studio 2005, you can quickly program custom authentication pages with the provided Membership Login controls. In this article, Dina Fleet Berry examines the steps involved in using the Login control with a custom SQL Server membership database.
    [Read This Article]  [Top]
    Dec 29, 2004 - ClickOnce Deployment in .NET Framework 2.0
    In this article, Thiru Thangarathinam examines .NET 2.0's new ClickOnce deployment technology that is designed to ease deployment of Windows forms applications. This new technology not only provides an easy application installation mechanism, it also eases deployment of upgrades to existing applications.
    [Read This Article]  [Top]
    Dec 15, 2004 - A Sneak Peek at ASP.NET 2.0's Administrative Tools
    With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
    [Read This Article]  [Top]
    Nov 17, 2004 - The ASP.NET 2.0 TreeView Control
    Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers