📄 aspnet05-01.htm
字号:
<p>Script and control coding for this output is shown below. Notice that the
buttons have <span class="code">CommandName</span> values indicating the <span class="code">BookType</span> field from the database to be used for selecting
subsets of records.</p><pre class="divcode"><span class="script"><SCRIPT Runat="Server">
Sub Show_Books (Src As Object, Args As CommandEventArgs)
Dim SQLString As String
SQLString = "SELECT BookID, BookTitle, BookPrice FROM Books " & _
"WHERE BookType = '" & <b>Args.CommandName</b> & "'"
BookSource.SelectCommand = SQLString
End Sub
</SCRIPT>
</span>
<form Runat="Server">
<h3>View Book Types</h3>
<asp:Button Runat="Server"
Text="Database"
OnCommand="Show_Books"
CommandName=<b>"Database"</b>
Width="80"/>
<asp:Button Runat="Server"
Text="Graphics"
OnCommand="Show_Books"
CommandName=<b>"Graphics"</b>
Width="80"/>
<asp:Button Runat="Server"
Text="Hardware"
OnCommand="Show_Books"
CommandName=<b>"Hardware"</b>
Width="80"/><br/>
<asp:Button Runat="Server"
Text="Software"
OnCommand="Show_Books"
CommandName=<b>"Software"</b>
Width="80"/>
<asp:Button Runat="Server"
Text="Systems"
OnCommand="Show_Books"
CommandName=<b>"Systems"</b>
Width="80"/>
<asp:Button Runat="Server"
Text="Web"
OnCommand="Show_Books"
CommandName=<b>"Web"</b>
Width="80"/>
<asp:AccessDataSource id="BookSource" Runat="Server"
DataFile="../Databases/BooksDB.mdb"
SelectCommand="SELECT BookID, BookTitle, BookPrice FROM Books
WHERE BookType = 'Database'"/>
<p><asp:GridView DataSourceID="BookSource" Runat="Server"/></p>
</form>
</pre>
<div class="listing"><b>Listing 5-9.</b> Code to issue SQL command depending on
CommandName of clicked button.</div>
<p>The AccessDataSource is originally coded with a <span class="code">SelectCommand</span> to retrieve a subset of records for initial
GridView display. Subsequently, the GridView displays other subsets of records
corresponding with button clicks.</p>
<p><span class="code">BookType</span> values are received by subprogram <span class="code">Show_Books</span> through the <span class="code">CommandName</span>
property of its <span class="code">CommandEventArgs</span> argument (<span class="code">Args.CommandName</span>). These command names, which are also the
<span class="code">BookType</span> values appearing in the database, are
concatenated within a <span class="code">SELECT</span> statement to select records
with these <span class="code">BookType</span> values. For instance, if the first
button is clicked (<span class="code">CommandName="Database"</span>), the
following <span class="code">SQLString </span>value is produced.</p><pre class="divcode"><span class="script">SQLString = "SELECT BookID, BookTitle, BookPrice FROM Books " & _
"WHERE BookType = '" & <b>Args.CommandName</b> & "'"
</span>
-- becomes --
SELECT BookID, BookTitle, BookPrice FROM Books WHERE BookType = 'Database'
</pre>
<div class="listing"><b>Listing 5-10.</b> SQL statement produced by clicking a
button.</div>
<p>When this <span class="code">SQLString</span> variable is script-assigned to
the AccessDataSource's <span class="code">SelectCommand</span> property, the data
source is immediately bound to the GridView to display these selected
books.</p><a name="POST">
<p class="head2">Posting to a Different Page</p></a>
<p>Normally, a button click posts back to the same page on which the button
appears. This is the default behavior of the button's <span class="code">OnClick</span> event handler. You can, instead, post to a different
page and still be able to access controls on the original page. Posting to a
different page takes place through the button's <span class="code">PostBackUrl</span> property. This property gives the URL of the page
to which to post.</p>
<p>In the following example, a page contains two controls, a Button and a Label.
The button includes the <span class="code">PostBackUrl</span> property to post to
page <span class="code">PostPage.aspx</span> located in the same directory. When
<span class="code">PostPage.aspx</span> opens, it accesses the button <span class="code">id</span> and label <span class="code">Text</span> from the previous
(posting) page.</p>
<div class="page">
<h4>Using PostBackUrl</h4>Button: <input id="PostingButton" onclick='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("PostingButton", "", false, "", "PostPage.aspx", false, false))' value="PostPage.aspx" name="PostingButton" type="submit"><br><br>Label: <span id="PostedValue" style="border-style: ridge; padding-right: 10px; padding-left: 10px; padding-top: 3px;">Text
value to post</span> <br><br></div>
<div class="figure"><b>Figure 5-7.</b> Posting to a different page.</div>
<p>Code for the posting page is shown below with a Button containing the <span class="code">PostBackUrl</span> property and a Label containing a text string for
posting to <span class="code">PostPage.aspx</span>.</p><pre class="divcode"><h4>Using PostBackUrl</h4>
Button: <asp:Button id="PostingButton" Text="PostPage.aspx" Runat="Server"
<b>PostBackUrl="PostPage.aspx"</b>/><br/><br/>
Label: <asp:Label id="PostedValue" Text="Text value to post" Runat="Server"
BorderStyle="Ridge" Style="padding-left:10px; padding-right:10px"/>
</pre>
<div class="listing"><b>Listing 5-11.</b> Code to post to a different page.</div>
<p>The <span class="code">PostBackUrl</span> page, <span class="code">PostPage.aspx</span>, includes a script to <i>locate</i> the Button
and Label controls on the posting page and to report their <span class="code">id</span> and <span class="code">Text</span> values, respectively.</p><pre class="divcode"><span class="script"><SCRIPT Runat="Server">
Sub Page_Load
Dim FoundButton As Button
FoundButton = PreviousPage.FindControl("PostingButton")
PostButton.Text = FoundButton.id
Dim FoundLabel As Label
FoundLabel = PreviousPage.FindControl("PostedValue")
PostValue.Text = FoundLabel.Text
End Sub
</SCRIPT>
</span>
<form Runat="Server">
<h3>This is PostPage.aspx</h3>
<p>The button with the id
"<asp:Label id="PostButton" Font-Bold="True" Runat="Server"/>"
posted the text value
"<asp:Label id="PostValue" Font-Bold="True" Runat="Server"/>"
from the previous page.
<p><a href="PostingPage.aspx">Return</a></p>
</form>
</pre>
<div class="listing"><b>Listing 5-12.</b> Code to retrieve and displays values
from posting page.</div>
<p class="head3">The FindControl() Method</p>
<p>The page to which a posting is made through the <span class="code">PostBackUrl</span> property of a button does <i>not</i> have direct
access to controls on the posting page. That is, a script on <span class="code">PostPage.aspx</span> cannot directly reference the Button or the
Label on the posting page. It cannot, as can be done on a page posted back to
itself, use <span class="code">PostingButton.id</span> or <span class="code">PostedValue.Text</span> to directly retrieve the <span class="code">id</span> and <span class="code">Text</span> properties of these
controls on the posting page.</p>
<p>The technique that must be used involves first <i>finding</i> the controls of
interest on the previous page and then referencing properties of these
<i>found</i> controls. The posting page is identified through the <span class="code"><b>PreviousPage</b></span> property; controls on this page are
located with its <span class="code"><b>FindControl()</b></span> method. The <span class="code">FindControl()</span> method of <span class="code">PreviousPage</span>
has the general formats shown in Figure 5-8.</p>
<table class="format" cellpadding="10">
<tbody>
<tr>
<td class="code"><pre><b>Dim</b> <i>FoundControl</i> <b>As</b> <i>ControlType</i>
<i>FoundControl</i> = <b>PreviousPage</b>.<b>FindControl</b>("<i>id</i>")
or
<b>Dim</b> <i>FoundControl</i> <b>As</b> <i>ControlType</i> = <b>PreviousPage</b>.<b>FindControl</b>("<i>id</i>")
</pre></td></tr></tbody></table>
<div class="figure"><b>Figure 5-8.</b> General format to locate a control on a
posting page.</div>
<p>First, an object of the type to be located is declared. When locating a
Button, for instance, the object is declared as type <span class="code">Button</span>; when locating a Label, the object is declared as type
<span class="code">Label</span>. Any ASP.NET control type can be declared to match
the control type to be located.</p>
<p>Second, this declared object is assigned the control of interest from the
<span class="code">PreviousPage</span> using the <span class="code">FindControl("<i>id</i>")</span> method to identify the control
through its <span class="code">id</span>. Once "found" and assigned to the locally
declared object, properties of a control appearing on the posting page can be
referenced through this local object. This technique is used in the <span class="code">Page_Load </span>subprogram on the <span class="code">PostPage.aspx</span> page.</p><pre class="divscript">Sub Page_Load
Dim FoundButton As Button
FoundButton = PreviousPage.FindControl("PostingButton")
PostButton.Text = FoundButton.id
Dim FoundLabel As Label
FoundLabel = PreviousPage.FindControl("PostedValue")
PostValue.Text = FoundLabel.Text
End Sub
</pre>
<div class="listing"><b>Listing 5-13.</b> Script to locate and report control
properties from posting page.</div>
<p>Object reference <span class="code">FoundButton</span> (any programmer-supplied
name can be used) is declared as a <span class="code">Button</span> object. It is
then assigned the Button with <span class="code">id="PostingButton"</span> located
on the <span class="code">PreviousPage</span> (posting page). Likewise for the
Label. Object reference <span class="code">FoundLabel </span>is declared as a
<span class="code">Label</span> object. The Label with <span class="code">id="PostedValue"</span> on the <span class="code">PreviousPage</span>
is assigned to this script object. Thereafter, properties associated with these
controls appearing on the posting page are accessible through their surrogate
references on the current page. In this example, <span class="code">FoundButton.id</span> accesses the <span class="code">id</span>
property of the Button on the posting page; <span class="code">FoundLabel.Text</span> accesses the <span class="code">Text</span>
property of the Label on the posting page. These two values are displayed in
Labels appearing on the posted page.</p>
<p>You need to become familiar with the <span class="code">FindControl()</span>
method. It has many uses under ASP.NET besides locating controls on pages which
issue <span class="code">PostBackUrls</span>. It is routinely used to locate
controls embedded inside other controls on the same page.</p><br></div>
<div><input id="__PREVIOUSPAGE" value="2-SrZOYhdUR1345CkIwLTbZs_f_i1ZSWvl_MoomJU20WvF7ktnAKTUO2ZV0TamsG0" name="__PREVIOUSPAGE" type="hidden"> <input id="__EVENTVALIDATION" value="/wEWDgKPj4LwCQLbhM+CAgKiwImNCwK098LfAwK4ookeAs6Tw5MHArfg0bUMAp/AiY0LAqDAiY0LAqXAiY0LAqbAiY0LAqPAiY0LAqTAiY0LAq3JurAJ/8IPhcaDS+PI8keTYfBXp/nsg7o=" name="__EVENTVALIDATION" type="hidden"> </div></form></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -