📄 aspnet02-04.htm
字号:
</pre>
<div class="listing"><b>Listing 2-32.</b> Code for a Button control.</div>
<p>A button's <span class="code"><b>Text</b></span> property provides the label
displayed on the button. Of course, the label assigned should have meaning to
the user, suggestive of what happens when the button is clicked.</p>
<p>The purpose of a Button control is to call a subprogram. This is done by
coding an <span class="code"><b>OnClick</b></span> event handler naming the
subprogram to call when the button is clicked. In this example, a subprogram
named <span class="code">Display_Output</span> is called.</p>
<p>Incidentally, if you code only the XHTML, fixed text, and form controls for
an ASP.NET page, and the controls include a Button with an <span class="code">OnClick</span> event handler, you will generate a compiler error when
the page is opened in the browser. This error is the result of not having coded
the subprogram named in the <span class="code">OnClick</span> handler. If you just
want to view the layout of the page prior to adding the script, be sure to
remove any <span class="code">OnClick</span> handlers. Then you can view the page
layout. Later, after adding the script portion of the page, replace the <span class="code">OnClick</span> handlers.</p>
<p class="head3">The <asp:GridView> Control</p>
<p>Along with a greeting to the user, the example page displays a list of books
and prices. This list is presented as a table with three columns for the book
ID, the book name, and the book price. This information is drawn directly from a
database table containing this and other information. It comes from the <span class="code">Books</span> table of the <span class="code">BooksDB.mdb</span>
database that is used in examples throughout these tutorials.</p>
<p>Among the many ways to display information from external data sources is the
<span class="code"><<b>asp:GridView</b>></span> control. This control is
designed especially for displaying information from a database organized into
rows and columns within a table. It has a great assortment of property settings
to control its layout and visual appearance; however, minimal coding can produce
a default table as used in this example. The general format for a GridView in
its default configuration is shown below.</p>
<table class="format" cellpadding="10">
<tbody>
<tr>
<td class="code"><pre><<b>asp:GridView</b> <b>Runat</b>="Server"
<b>id=</b>"<i>id</i>"
<b>DataSourceID=</b>"<i>data source</i>"
<b>/></b>
</pre></td></tr></tbody></table>
<div class="figure"><b>Figure 2-30.</b> General format for default <span class="code"><asp:GridView></span> control.</div>
<p>A GridView automatically displays as many columns as there are data fields
retrieved from the database; it displays as many rows as there are records
returned from the database; it produces column headings taken from the field
names in the database. It presents this information in the form of an XHTML
table with collapsed borders.</p><pre class="divcode"><asp:GridView id="BookGrid" DataSourceID="BookSource" Runat="Server"
Visible="False" />
</pre>
<div class="listing"><b>Listing 2-33.</b> Code for a GridView control.</div>
<p>An <span class="code"><b>id</b></span> value is required only if the GridView
is referenced from a script. In the current example this is the case, so an
<span class="code">id</span> is coded. In its full configuration, a GridView has
numerous properties that can be set to control its display. In the current
example, its <span class="code"><b>Visible</b></span> property is initially set to
<span class="code">"False"</span> so that the GridView does not appear on the page
until after user input is completed.</p>
<p>In order for a GridView to display database information, it must be
associated with a data source control. This control connects to the database and
retrieves a set of records for display. The data source control is bound to the
GridView through the GridView's <span class="code"><b>DataSourceID</b></span>
property. In the current example, the property setting <span class="code">DataSourceID="BookSource"</span> points to this named data source
control as the source of the GridView's displayed information.
</p><p class="head3">The <asp:AccessDataSource> Control</p>
<p>The data source control used to interface with Microsoft Access databases is
the <span class="code"><<b>asp:AccessDataSource</b>></span> control. Its
general format to retrieive a set of records from a database table is shown
below.</p>
<table class="format" cellpadding="10">
<tbody>
<tr>
<td class="code"><pre><<b>asp:AccessDataSource</b> <b>Runat</b>="Server"
<b>id=</b>"<i>id</i>"
<b>DataFile=</b>"<i>database path</i>"
<b>SelectCommand=</b>"<i>SQL SELECT statement</i>"
<b>/></b>
</pre></td></tr></tbody></table>
<div class="figure"><b>Figure 2-31.</b> General format for <span class="code"><asp:AccessDataSource></span> control.</div>
<p>The <span class="code"><b>id</b></span> assigned to the control must match that
given in the <span class="code">DataSourceID</span> property of the display
control for which this is the data source. The <span class="code"><b>DataFile</b></span> property gives the server path to the
database; the <span class="code"><b>SelectCommand</b></span> property is an SQL
<span class="code">SELECT</span> statement to retrieve a set of records from the
database. This is all the information needed to produce a recordset that can be
bound to a GridView or to other output controls for display of database
information.</p><pre class="divcode"><asp:AccessDataSource id="BookSource" Runat="Server"
DataFile="c:\eCommerce\Databases\BooksDB.mdb"
SelectCommand="SELECT BookID, BookName, BookPrice FROM Books
WHERE BookSale = True
ORDER BY BookID"/>
</pre>
<div class="listing"><b>Listing 2-34.</b> Code for an AccessDataSource
control.</div>
<p>In the current example, an AccessDataSource links to the <span class="code">BooksDB.mdb</span> database. This database is located on the server's
<span class="code">c:</span> drive in the <span class="code">Databases</span>
subdirectory of the <span class="code">eCommerce</span> directory. An SQL <span class="code">SELECT</span> statement retrieves three fields of information from
all records in the <span class="code">Books</span> table where a book is on sale;
the records are returned in sorted order by the <span class="code">BookID</span>
field. In this example, seven records are extracted from the table and made
available through this control.</p>
<p class="head3">Binding a Data Source to a Control</p>
<p>When a page is first opened, the AccessDataSource immediately issues its
<span class="code">SelectCommand</span> to retrieve the specified records from the
database. This recordset is then available for binding to any display control
that identifies this data source in its <span class="code">DataSourceID</span>
property. Binding involves displaying the data values contained in the data
source through a display control coded on the page. In this case, it is the
GridView that binds to the data source, formatting the returned recordset as a
displayed table.</p>
<p>Although the example GridView is fully loaded and formatted for display when
the page opens, it should not be displayed until after the user enters a name
and clicks the button. So, the GridView's <span class="code">Visible</span>
property is initially set to <span class="code">"False"</span> to keep it hidden
from view. It is revealed programmatically in the subprogram that is called to
display the welcome message.</p>
<p>At this point, all of the XHTML, fixed text, and server controls have been
coded for the page. All that remains is to code the script to make it all
work.</p>
<p class="head2">ASP.NET Script Coding</p>
<p>The script portion of a page is coded at the top of the page, prefixed by any
needed page directives and enclosed inside <span class="code"><script></span> tags. This general outline is fleshed out for
the example page as shown in Listing 2-35. You might be surprised to see that
very little scripting is needed to animate the application. This is because much
of the work is behind the scenes, encapsulated inside server controls to save
you from having to code processing details. There is only one subprogram
containing three Visual Basic statements needed to perform all processing.</p><pre class="divcode"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<span class="script">
<%@ Page Language="vb" Debug="True" %>
<SCRIPT Runat="Server">
Sub Display_Output (Src As Object, Args As EventArgs)
If NameIn.Text <> "" Then
GreetingOut.Text = "Hello, <b>" & NameIn.Text & "</b>." _
& " Today is " & Format(DateString, "Long Date") _
& " and the time is " & Format(TimeString, "Long Time") & "." _
& " Check out our special prices on computer books listed" _
& " below:<br/>"
BookGrid.Visible = True
End If
End Sub
</SCRIPT>
</span>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Welcome Page</title>
</head>
<body>
<form Runat="Server">
<h2>Welcome to My Store</h2>
<p>Enter your name in the box below and click the button for our special
offers.</p>
Name: <asp:TextBox id="NameIn" Runat="Server"/>
<asp:Button Text="See Specials" On_Click="Display_Output" Runat="Server"/>
<p><asp:Label id="GreetingOut" Runat="Server"/></p>
<asp:AccessDataSource id="BookSource" Runat="Server"
DataFile="c:\eCommerce\Databases\BooksDB.mdb"
SelectCommand="SELECT BookID, BookName, BookPrice FROM Books
WHERE BookSale = True
ORDER BY BookID"/>
<asp:GridView id="BookGrid" DataSourceID="BookSource" Runat="Server"
Visible="False"/>
</form>
</body>
</html>
</pre>
<div class="listing"><b>Listing 2-35.</b> Script to produce dynamic content for a
Web page.</div>
<p>A page directive is included at the top of the page giving the optional <span class="code">Language</span> specification and permitting <span class="code">Debug</span> error messages to appear at the developer's PC.</p>
<p>The <span class="code">Display_Output</span> subprogram, being called by a
button click, has the necessary signature for a button-activated subprogram.
Although no script reference is made to the source (<span class="code">Src</span>)
object making the call or to any arguments (<span class="code">Args</span>) being
passed, this signature still must be supplied for any button-activated
subprogram.</p>
<p>Script processing accomplishes two things. First, it produces the text
welcome message integrating the user's name from the TextBox; then it reveals
the GridView table. Both of these actions are contingent on the user entering a
name; therefore, the script is enclosed inside a Visual Basic <span class="code">If...End If</span> decision structure to run only if a name is
available in the TextBox.</p><pre class="divscript">Sub Display_Output (Src As Object, Args As EventArgs)
<b>If NameIn.Text <> "" Then</b>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -