📄 aspnet02-04.htm
字号:
...<i>rest of script</i>
<b>End If</b>
End Sub
</pre>
<div class="listing"><b>Listing 2-36. </b>Testing for available input data.</div>
<p>Script references to properties of server controls follow common <i>dotted
notation</i> practices; that is, the <span class="code">id</span> of the control
is followed by a property name, separated by a period.</p>
<table class="format" cellpadding="10">
<tbody>
<tr>
<td class="code"><pre><i>ControlId.property</i>
</pre></td></tr></tbody></table>
<div class="figure"><b>Figure 2-32.</b> General format for script references to
control properties.</div>
<p>The script reference <span class="code"><b>NameIn.Text</b></span> refers to the
value (<span class="code">Text</span> property) entered into the TextBox control
(<span class="code">id="NameIn"</span>) as user input. The <span class="code">If</span> statement tests this <span class="code">NameIn.Text</span>
property to ensure that it is not empty, or <b>null</b> (<span class="code">NameIn.Text <> ""</span>). With a name available, the script
proceeds to formulate the welcome message. This is a matter of producing a text
string for assignment to the <span class="code">Text</span> property of the <span class="code">GreetingOut</span> Label control.</p>
<p>This output string is a concatenation of fixed text, the name from the
TextBox, and a couple of Visual Basic date and time properties formatted for
display. The text string also includes XHTML tags to assist in its formatting.
The five lines of code are continuations of a single assignment statement.</p><pre class="divscript"> 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/>"
</pre>
<div class="listing"><b>Listing 2-37. </b>Concatenating a string for output
display.</div>
<p>Finally, the script reveals the GridView table (<span class="code">id="BookGrid"</span>) by settings its <span class="code">Visible</span>
property to <span class="code">True</span>.</p><pre class="divscript"> BookGrid.Visible = True
</pre>
<div class="listing"><b>Listing 2-38. </b>Revealing a hidden server control</div>
<p>The above example is a demonstration of the coding of and relationships
between server controls and script elements. It presents some common controls
and their minimal coding, along with typical ways in which scripts interact with
these controls. It is not meant as full coverage of the controls nor as
definitive methods of coding subprograms. The example should, however, give you
a sense of the general approach to coding ASP.NET pages.
</p><p class="head2">Scripting the Page_Load Event</p>
<p>The previous example is a common scenario where a user action causes a
subprogram to run. It is often the case, however, that scripts do not require
user actions to invoke. They can be run in response to events produced by the
page itself. As mentioned previously, a common trigger for scripts is the page's
load event, when the page is accessed from server storage and loaded into server
memory prior to its transmittal to the requesting browser. The following page
produces the current date and time automatically each time the page loads, that
is, when the page is <i>initially</i> loaded by the server when the page first
opens (the load event) and when the page is <i>reloaded</i> by a button click (a
post-back event). Full coding for this application is shown below.</p>
<div class="page">
<h2>Welcome to My Page</h2>Today is <span id="DateOut">Saturday, March 29,
2008</span>.<br>The time is <span id="TimeOut">8:34:10 AM</span>.<br><br><input value="Update Time" name="ctl02" type="submit"> </div>
<div class="figure"><b>Figure 2-33.</b> Producing dynamic content during page
loading.</div><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 Page_Load
DateOut.Text = Format(DateString, "Long Date")
TimeOut.Text = Format(TimeString, "Long Time")
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 Page</h2>
Today is <asp:Label id="DateOut" Runat="Server"/>.<br/>
The time is <asp:Label id="TimeOut" Runat="Server"/>.<br/>
<br/>
<asp:Button Text="Update Time" Runat="Server"/>
</form>
</body>
</html>
</pre>
<div class="listing"><b>Listing 2-39.</b> Code to produce dynamic content during
page loading.</div>
<p>Two <span class="code"><asp:Label></span> controls are defined to receive
script output. These controls are surrounded by fixed text that does not change
on loading and reloading of the page. A button is provided to update the date
and time. Notice that the button does not include an <span class="code">OnClick</span> event handler. It does not call a specific subprogram;
it merely causes the page to be reloaded.</p>
<p>Since the script should run each time the page loads, it is placed inside the
special <span class="code">Page_Load</span> subprogram, so-named for this purpose.
The subprogram does not require an argument list since it is not explicitly
called by an event handler. The subprogram is run each time the page is opened
to assign system date and time information to the Label controls. It runs, then,
the first time this page is accessed and each time the button is clicked to
reload the page.</p>
<p class="head2">Handling Post-Back Events</p>
<p>As noted, there are two distinct kinds of events that cause the <span class="code">Page_Load </span>subprogram to run. One event is an initial page load
event, occuring the <i>first</i> time a page is accessed, normally in response
to a URL request for the page. The other event is a page post-back event, a
reloading of the page in response to a user event such as a mouse click. It is
sometimes important to differentiate between these two events in coding the
<span class="code">Page_Load</span> subprogram.</p>
<p>Consider the following application. When this page is first loaded into
server memory in response to a URL request, the page is time-stamped in the
<span class="code">Page_Load</span> subprogram with the date and time it was
<i>first</i> accessed. It currently reports when you first opened this page.
This date and time should not change, though, even when the button click causes
a post-back event to reload the page and rerun the <span class="code">Page_Load</span> subprogram. The date and time should remain as
initially reported even though time moves forward.</p>
<div class="page">
<h2>Welcome to My Page</h2>You first arrived at this page on <b><span id="DateOut1">Saturday, March 29, 2008</span></b> at <b><span id="TimeOut1">8:34:10
AM</span></b>.
<p><input value="Post Back" name="ctl03" type="submit"></p></div>
<div class="figure"><b>Figure 2-34.</b> Maintaining content on page
post-back.</div>
<p>In clicking the above button you will notice that the date and time do not
change, even though their reporting appears in the <span class="code">Page_Load</span> subprogram which is run each time the button is
clicked. The reason the date and time are not updated is because the script
differentiates between the first time the page is loaded and subsequent times
the page is loaded.</p>
<p>This <span class="code">Page_Load</span> subprogram avoids updating the date
and time by testing for a post-back event. This is done by testing the page's
<span class="code"><b>IsPostBack </b></span>property.</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 Page_Load
<b>If Not Page.IsPostBack Then</b>
DateOut.Text = Format(DateString, "Long Date")
TimeOut.Text = Format(TimeString, "Long Time")
<b>End If</b>
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 Page</h2>
You first arrived at this page on
<b><asp:Label id="DateOut" Runat="Server"/></b> at
<b><asp:Label id="TimeOut" Runat="Server"/></b>.
<p><asp:Button Text="Post Back" Runat="Server"/></p>
</form>
</body>
</html>
</pre>
<div class="listing"><b>Listing 2-40.</b> Code to maintain dynamic content on page
post-back.</div>
<p>The <span class="code">Page.IsPostBack</span> condition is <span class="code">True</span> when the page is <i>reloaded</i> due to a post-back event
such as a button click to reload the page or to call a subprogram. Therefore,
the condition <span class="code">"<b>Not</b> Page.IsPostBack" </span>identifies
the situation where the page is <i>not</i> posted back. The script is run, then,
only when the page is <i>initially</i> loaded. When the button click posts back
the page, the script is <i>not</i> run. You will notice, though, that if you
reload this page from the tutorial menu or if you refresh the page with the
browser's Refresh button, a new date and time are reported. In these cases the
page is loaded anew, not posted back through a server control. As in the
previous example, the button click does not call a named subprogram; it just
posts back the page to cause the <span class="code">Page_Load</span> subprogram to
run.</p>
<p>The above examples are typical of the kinds of processing performed by
ASP.NET pages. Although these are relatively simple examples, they illustrate
the common layout and coding of pages and the sorts of processing requests they
handle. Regardless of how complex page processing may appear, it normally
involves some combination of input controls, script activation controls, output
controls, data source controls, and information display controls arranged and
activated in a similar manner to those described above.</p><br></div>
<div><input id="__EVENTVALIDATION" value="/wEWBgKQua2tBALbhM+CAgLosqfnCwKiwImNCwKfwImNCwKgwImNC4n+5hXQL68dbhCJSOizy0ilY7Pa" name="__EVENTVALIDATION" type="hidden"> </div></form></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -