📄 aspnet03-01.htm
字号:
XHTML tags, and a formatted Visual Basic date property to server output controls
coded on the page.</p>
<div class="page" style="height: 200px;"><span id="Heading">
<h3>Script-Generated Page Output</h3></span><span id="Paragraph">
<p>Today, <span style="color: red;">Saturday, March 29, 2008</span>, this output
is produced by a script that writes text characters, XHTML tags, and a Visual
Basic date function to server output controls coded on the page. The date is
always current and does not require page editing.</p></span></div>
<div class="figure"><b>Figure 3-2. </b>Page output produced by script.</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">
<SCRIPT Runat="Server">
Sub Page_Load
Heading.Text = "<h3>Script-Generated Page Output</h3>"
Paragraph.Text = "<p>Today, <span style=""color:red"">" & _
Format(DateString, "Long Date") & "</span>, " & _
"this output is produced by a script that writes " & _
"text characters, XHTML tags, and a Visual Basic " & _
"date function to server output controls coded on " & _
"the page. The date is always current and does not " & _
"require page editing.</p>"
End Sub
</SCRIPT>
</span>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Script-Generated Page Output</title>
</head>
<body>
<form Runat="Server">
<asp:Label id="Heading" Runat="Server"/>
<asp:Label id="Paragraph" Runat="Server"/>
</form>
</body>
</html>
</pre>
<div class="listing"><b>Listing 3-2. </b>Page produced by script output.</div>
<p>Script-generated information must be targeted to server controls as output
areas. In the above example, two <span class="code"><asp:Label></span>
controls serve this purpose. They are given <span class="code">id</span>
identifications so that the script, coded here in the <span class="code">Page_Load</span> subprogram, can specify where to place its output.
The script assigns a string of text characters and XHTML to the <span class="code">Text</span> property of the <span class="code">Heading</span> Label to
display as the page heading; it assigns a combination of text, XHTML, and a
server-generated date to the <span class="code">Paragraph</span> Label. Notice
that no XHTML or text information is hard-coded in the body of the page. It is
generated and written entirely by the script.</p>
<p>In this example, the string of text assigned to the output Label itself
contains a quoted string. Notice in the first line of the assignment,</p><pre class="script">Paragraph.Text = "<p>Today, <span style=""color:red"">" & _
</pre>
<p>that the embedded <span class="code"><span></span> tag includes the style
setting <span class="code">style=""color:red""</span> wherein the property setting
is enclosed inside a <i>pair</i> of quotes. Any time quoted text appears inside
a quoted string it is necessary to use pairs of quotes surrounding the inner
string to differentiate from the single quotes surrounding the outer string.</p>
<p class="head2">Script-Processed Input Data</p>
<p>Web page information can be produced by inputting user data and using it to
generate output for display through server output controls. The following
example solicits user input through a TextBox control. When the button is
clicked, a subprogram is called to perform a calculation based on the entered
value. The calculation result, along with surrounding text and XHTML, are
written to a Label output control.</p>
<div class="page" style="height: 200px;">
<h3>User-Supplied Page Input</h3>Enter your age: <input id="Age" size="5" name="Age"> <input value="Submit" name="ctl01" type="submit"> <span id="DogAge"></span></div>
<div class="figure"><b>Figure 3-3. </b>Page output produced from user input.</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">
<SCRIPT Runat="Server">
Sub Get_Dog_Years (Src As Object, Args As EventArgs)
If Age.Text <> "" Then
Dim DogYears As Integer
DogYears = Age.Text * 7
DogAge.Text = "Your age is <b>" & DogYears & "</b> in dog years!"
End If
End Sub
</SCRIPT>
</span>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>User-Supplied Page Input</title>
</head>
<body>
<form Runat="Server">
<h3>User-Supplied Page Input</h3>
Enter your age:
<asp:TextBox id="Age" Size="5" Runat="Server"/>
<asp:Button Text="Submit" OnClick="Get_Dog_Years" Runat="Server"/>
<asp:Label id="DogAge" Runat="Server"/>
</form>
</body>
</html>
</pre>
<div class="listing"><b>Listing 3-3. </b>Code for page produced from user
input.</div>
<p>For this example, three server controls are needed. An <span class="code"><asp:TextBox> </span>control is required as the user input
area; an <span class="code"><asp:Button> </span>control calls a subprogram
to produce page output; an <span class="code"><asp:Label></span> control
serves as the display target for script output. When the button is clicked, the
value entered into the TextBox is multiplied by 7 and the result is surrounded
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -