In this section, you create a simple page that uses the DataGrid control. This page will serve as the basis for the pages in subsequent sections.
To create a simple ASP.NET page with a DataGrid control
- In your Web directory, located at C:\Inetpub\wwwroot by default, create a new directory named DataAccess.
- Create a new ASP.NET file named DataAccess.aspx in the C:\Inetpub\wwwroot\DataAccess directory.
You can do this with an application such as Notepad.
Note You cannot use the Visual Studio .NET integrated development environment (IDE) to create a JScript .NET project. However, you can edit ASP.NET files in the IDE.
- Open the ASP.NET page for editing in either the Visual Studio .NET IDE or Notepad.
Note You cannot directly paste code copied from a richly formatted source (such as a Web page) into an ASP.NET page that you are editing in the Visual Studio .NET IDE. The IDE converts richly formatted text into HTML code that reproduces the look of the original text instead of pasting as unformatted text. You can use a two-step process to work around this restriction. First, paste a code example into a text-based editor (such as Notepad) to remove the rich formatting. Second, cut the code from the text-based editor and paste it into the IDE.
- Add the following code to the page:
<%@ Page Language=jscript %>
<html>
<body>
<script runat=server>
function Page_Load(sender : Object, e : EventArgs) {
// Put code in this function that you want to have loaded
// each time the page is requested from the server.
if (!IsPostBack) {
// Put code here that you want to run only the first time.
}
// Page_Load
</script>
<h3>Category List</h3>
<form runat=server>
<asp:DataGrid id="ItemsGrid" runat="server">
</asp:DataGrid>
</form>
</body>
</html>
- Save the page.
- View the page by typing the following address into the address bar of Internet Explorer:
http://localhost/DataAccess/DataAccess.aspx
When you access the page, it only displays the "Category List" header. The DataGrid control does not display data because no data has been bound to it.
Before you attempt to load data from a database, ensure that the DataGrid control works properly by using data from a well-controlled data source. In this section, you construct an ArrayList, and then you bind the ArrayList to the DataGrid control.
To bind data to a DataGrid control
- Add the following function to the script block.
function CreateTestData() : ICollection {
// Function to create a simple table.
var al : ArrayList = new ArrayList();
al.Add("orange");
al.Add("pear");
al.Add("banana");
return al;
} // CreateTestData
- Bind the data in the table to the DataGrid control by adding the following lines to the if (!IsPostBack) block of the Page_Load function:
ItemsGrid.DataSource = CreateTestData();
ItemsGrid.DataBind();
- Save the page.
- Reload the page in Internet Explorer, or type the following address in the address bar of Internet Explorer:
http://localhost/DataAccess/DataAccess.aspx
The page now displays a simple table of several fruits.