📄 aspnet05-01.htm
字号:
</SCRIPT>
</span>
<form Runat="Server">
<asp:Button id="FirstButton" Runat="Server"
Text="Report Arguments"
OnCommand="Report_Arguments"
CommandName="Do This"
CommandArgument="With This Value"/>
<asp:Button id="SecondButton" Runat="Server"
Text="Report Arguments"
OnCommand="Report_Arguments"
CommandName="Do That"
CommandArgument="With That Value"/>
<p><asp:Label id="PassedValues" Runat="Server"/></p>
</form>
</pre>
<div class="listing"><b>Listing 5-4.</b> Code to script a pair of command
buttons.</div>
<p>As you can see, there are three different ways to distinguish buttons from
one another. They can be differentiated by their <span class="code">id</span>
values; they can be differentiated by their <span class="code">CommandName</span>
values; they can be differentiated by their <span class="code">CommandArgument</span> values. A subprogram can use any combination
of these passed values to differentiate processing requests.</p>
<p class="head2">Coding Choices</p>
<p>It is often not necessary to supply all three arguments to determine which
processing option is carried out by a subprogram. If, for instance, it is only a
matter of choosing either of two processing tasks requested by two different
buttons, then the buttons' <span class="code">ids</span> are sufficient
information for the subprogram to make the choice. It is not necessary to supply
<span class="code">CommandName</span> or <span class="code">CommandArgument</span>
values. The following subprogram is coded to distinguish between two buttons in
order to take two processing routes.</p><pre class="divcode"><span class="script"><SCRIPT Runat="Server">
Sub Do_Processing (Src As Object, Args As CommandEventArgs)
If <b>Src.id</b> = "FirstButton" Then
...<i>do this processing</i>
End If
If <b>Src.id</b> = "SecondButton" Then
...<i>do this processing</i>
End If
End Sub
</SCRIPT>
</span>
<form Runat="Server">
<asp:Button id="FirstButton" OnCommand="Do_Processing" Runat="Server"/>
<asp:Button id="SecondButton" OnCommand="Do_Processing" Runat="Server"/>
</form>
</pre>
<div class="listing"><b>Listing 5-5.</b> Referencing a source object of a
subprogram call.</div>
<p>An alternative to the above coding is to supply <span class="code">CommandNames</span> instead of <span class="code">id</span> values to
distinguish processing choices. One reason for doing this would be to make more
explicit the processing choices represented by the buttons.</p><pre class="divcode"><span class="script"><SCRIPT Runat="Server">
Sub Do_Processing (Src As Object, Args As CommandEventArgs)
If <b>Args.CommandName</b> = "Process1" Then
...<i>do this processing</i>
End If
If <b>Args.CommandName</b> = "Process2" Then
...<i>do this processing</i>
End If
End Sub
</SCRIPT>
</span>
<form Runat="Server">
<asp:Button OnCommand="Do_Processing" CommandName="Process1" Runat="Server"/>
<asp:Button OnCommand="Do_Processing" CommandName="Process2" Runat="Server"/>
</form>
</pre>
<div class="listing"><b>Listing 5-6.</b> Using a <span class="code">CommandName</span> to determine processing choices.</div>
<p>A second and more common reason for using <span class="code">CommandNames</span> is to pass along data values that are involved in
the processing. In the following example, two buttons supply the same subprogram
with different data values to be involved in the same processing.</p><pre class="divcode"><span class="script"><SCRIPT Runat="Server">
Sub Do_Processing (Src As Object, Args As CommandEventArgs)
...<i>do processing with</i> <b>Args.CommandName</b> '-- with "Data1" or "Data2"
End Sub
</SCRIPT>
</span>
<form Runat="Server">
<asp:Button OnCommand="Do_Processing" CommandName="Data1" Runat="Server"/>
<asp:Button OnCommand="Do_Processing" CommandName="Data2" Runat="Server"/>
</form>
</pre>
<div class="listing"><b>Listing 5-7.</b> Using a <span class="code">CommandName</span> to supply different data values.</div>
<p>If a subprogram offers additional processing choices, then both <span class="code">CommandName </span>and <span class="code">CommandArgument</span>
properties might be needed to isolate these choices. In the following example,
four buttons call the same subprogram requesting different sorting actions on
different data fields.</p><pre class="divcode"><span class="script"><SCRIPT Runat="Server">
Sub Do_Sorting (Src As Object, Args As CommandEventArgs)
If Args.CommandName = "Sort1" Then
If Args.CommandArgument = "SortUp" Then
...<i>sort field 1 ascending</i>
End If
End If
If Args.CommandName = "Sort1" Then
If Args.CommandArgument = "SortDown" Then
...<i>sort field 1 descending</i>
End If
End If
If Args.CommandName = "Sort2" Then
If Args.CommandArgument = "SortUp" Then
...<i>sort field 2 ascending</i>
End If
End If
If Args.CommandName = "Sort2" Then
If Args.CommandArgument = "SortDown" Then
...<i>sort field 2 descending</i>
End If
End If
End Sub
</SCRIPT>
</span>
<form Runat="Server">
<asp:Button Text="Sort 1 Ascending" Runat="Server"
OnCommand="Do_Sorting"
CommandName="Sort1"
CommandArgument="SortUp"/>
<asp:Button Text="Sort 1 Descending" Runat="Server"
OnCommand="Do_Sorting"
CommandName="Sort1"
CommandArgument="SortDown"/>
<asp:Button Text="Sort 2 Ascending" Runat="Server"
OnCommand="Do_Sorting"
CommandName="Sort2"
CommandArgument="SortUp"/>
<asp:Button Text="Sort 2 Descending" Runat="Server"
OnCommand="Do_Sorting"
CommandName="Sort2"
CommandArgument="SortDown"/>
</form>
</pre>
<div class="listing"><b>Listing 5-8.</b> Referencing a passed <span class="code">CommandArgument</span> in a subprogram call.</div>
<p>For most processing requirements, it is usually not necessary to include
these additional levels of button differentiation. Differering <span class="code">id</span> and/or <span class="code">CommandName</span> arguments are
usually sufficient. If a subprogram requires an additional <span class="code">CommandArgument</span> to differentiate choices, then you might
consider whether the single subprogram is trying to do too much.</p>
<p class="head2">Using Buttons for Database Display</p>
<p>The following example is an illustration of the use of command buttons. All
buttons call the same subprogram to select display of book types from the <span class="code">BooksDB.mdb</span> database. The subprogram issues differently
composed SQL statements to select different recordsets for display in a
GridView. The different SQL statements depend on the different <span class="code">CommandName</span> arguments passed by the buttons. This application
uses an AccessDataSource to connect to the database and a basic GridView to
display results.</p>
<div class="page">
<h3>View Book Types</h3><input style="width: 80px;" value="Database" name="ctl02" type="submit"> <input style="width: 80px;" value="Graphics" name="ctl03" type="submit"> <input style="width: 80px;" value="Hardware" name="ctl04" type="submit"><br><input style="width: 80px;" value="Software" name="ctl05" type="submit">
<input style="width: 80px;" value="Systems" name="ctl06" type="submit"> <input style="width: 80px;" value="Web" name="ctl07" type="submit">
<p>
</p><div>
<table style="border-collapse: collapse;" border="1" cellspacing="0" rules="all">
<tbody>
<tr>
<th scope="col">BookID</th>
<th scope="col">BookTitle</th>
<th scope="col">BookPrice</th></tr>
<tr>
<td>DB111</td>
<td>Oracle Database</td>
<td>69.99</td></tr>
<tr>
<td>DB222</td>
<td>Databases in Depth</td>
<td>29.95</td></tr>
<tr>
<td>DB333</td>
<td>Database Processing</td>
<td>136.65</td></tr>
<tr>
<td>DB444</td>
<td>Access Database Design</td>
<td>34.95</td></tr>
<tr>
<td>DB555</td>
<td>SQL Server 2005</td>
<td>29.99</td></tr></tbody></table></div>
<p></p></div>
<div class="figure"><b>Figure 5-6.</b> Using command buttons to issue SQL
statements for book displays.</div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -