⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aspnet05-01.htm

📁 tutorialssss.... for everybody!!!!!!!!!!!!!!!!!!!
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><!-- saved from url=(0076)http://msconline.maconstate.edu/tutorials/aspnet20/ASPNET05/aspnet05-01.aspx --><title>ASP.NET Tutorial</title>


<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link href="aspnet05-01_data/stylesheet.css" type="text/css" rel="stylesheet">
<style>TABLE#Products TD {
	FONT-SIZE: 12pt; FONT-FAMILY: times new roman
}
TH {
	FONT-SIZE: 12pt; FONT-FAMILY: times new roman
}
</style>

<meta content="MSHTML 6.00.2900.2180" name="GENERATOR"></head><body onscroll="document.all.PageScroll.value=document.body.scrollTop" onload="document.body.scrollTop=document.all.PageScroll.value">
<form id="ctl00" name="ctl00" action="aspnet05-01.aspx" method="post">
<div><input id="__EVENTTARGET" name="__EVENTTARGET" type="hidden"> <input id="__EVENTARGUMENT" name="__EVENTARGUMENT" type="hidden"> <input id="__VIEWSTATE" value="/wEPDwULLTIxMTEzMDQxNDQPZBYCAgEPZBYCAigPPCsADQIADxYEHgtfIURhdGFCb3VuZGceC18hSXRlbUNvdW50AgVkDBQrAAMWCB4ETmFtZQUGQm9va0lEHgpJc1JlYWRPbmx5aB4EVHlwZRkrAh4JRGF0YUZpZWxkBQZCb29rSUQWCB8CBQlCb29rVGl0bGUfA2gfBBkrAh8FBQlCb29rVGl0bGUWCB8CBQlCb29rUHJpY2UfA2gfBBkpW1N5c3RlbS5EZWNpbWFsLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkfBQUJQm9va1ByaWNlFgJmD2QWDAIBD2QWBmYPDxYCHgRUZXh0BQVEQjExMWRkAgEPDxYCHwYFD09yYWNsZSBEYXRhYmFzZWRkAgIPDxYCHwYFBTY5Ljk5ZGQCAg9kFgZmDw8WAh8GBQVEQjIyMmRkAgEPDxYCHwYFEkRhdGFiYXNlcyBpbiBEZXB0aGRkAgIPDxYCHwYFBTI5Ljk1ZGQCAw9kFgZmDw8WAh8GBQVEQjMzM2RkAgEPDxYCHwYFE0RhdGFiYXNlIFByb2Nlc3NpbmdkZAICDw8WAh8GBQYxMzYuNjVkZAIED2QWBmYPDxYCHwYFBURCNDQ0ZGQCAQ8PFgIfBgUWQWNjZXNzIERhdGFiYXNlIERlc2lnbmRkAgIPDxYCHwYFBTM0Ljk1ZGQCBQ9kFgZmDw8WAh8GBQVEQjU1NWRkAgEPDxYCHwYFD1NRTCBTZXJ2ZXIgMjAwNWRkAgIPDxYCHwYFBTI5Ljk5ZGQCBg8PFgIeB1Zpc2libGVoZGQYAQUFY3RsMDgPPCsACQEIAgFkJPoQ4ptELvXIOqqSvBDs2OlW5sQ=" name="__VIEWSTATE" type="hidden"> </div>
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['ctl00'];
if (!theForm) {
    theForm = document.ctl00;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>

<script src="ButtonControl_files/aspnet05-01.htm" type="text/javascript"></script>
<input id="PageScroll" style="visibility: hidden; position: absolute;" name="PageScroll"> 
<div class="body">
<div class="divhead">&lt;asp:Button&gt; Control</div>
<p>Server scripts are activated by events surrounding the Web page. The usual 
way to call these scripts is through event handlers attached to controls. The 
<span class="code"><b>&lt;asp:Button&gt;</b></span> control is the most common 
method to activate scripts. It displays a clickable button that calls a 
subprogram or performs submission of the form on which it is contained. The 
general format for this control is shown below.</p>
<table class="format" cellpadding="10">
  <tbody>
  <tr>
    <td class="code"><pre>&lt;<b>asp:Button</b> <b>id</b>="<i>id</i>" <b>Runat</b>="Server"
  <b>CommandArgument</b>="<i>argument</i>"
  <b>CommandName</b>="<i>command</i>"
  <b>PostBackUrl</b>="<i>url</i>"
  <b>Text</b>="<i>string</i>"
  
  <i>property</i>="<i>value</i>"...
  
    <b>OnClick</b>="<i>subprogram</i>"
    <b>OnCommand</b>="<i>subprogram</i>"
<b>/&gt;</b>
</pre></td></tr></tbody></table>
<div class="figure"><b>Figure 5-1.</b> General format for <span class="code">&lt;asp:Button&gt;</span> control.</div>
<p>An <span class="code"><b>id</b></span> is not required unless the button's 
identity is referenced by the called subprogram. The <span class="code"><b>Text</b></span> attribute provides the label for the button. The 
<span class="code"><b>OnClick</b></span> event handler identifies a subprogram to 
run when the button is clicked. The <span class="code"><b>OnCommand</b></span>, 
<span class="code"><b>CommandName</b></span>, and <span class="code"><b>CommandArgument</b></span> properties call a subprogram to pass 
along data or control information needed to perform processing.</p>
<p>Clicking a button performs a post-back operation. The page is reloaded, the 
<span class="code">Page_Load</span> subprogram, if coded, is run, followed by the 
subprogram named in the <span class="code">OnClick</span> event handler. Normally, 
post-back is to the same page. However, a different page, named in the <span class="code"><b>PostBackUrl</b></span> property can be called.</p>
<p class="head2">Calling a Subprogram</p>
<p>Normally a button's purpose is to call a subprogram by supplying the 
subprogram's name in an <span class="code">OnClick</span> event handler coded in 
the button. The following button operates in this fashion. The called subprogram 
simply assigns a text string to an accompanying Label control.</p>
<div class="page"><br><input value="Call Button_Click" name="ctl01" type="submit"> 
<span id="Message1"></span><br><br></div>
<div class="figure"><b>Figure 5-2.</b> Using a button to call a subprogram.</div><pre class="divcode"><span class="script">&lt;SCRIPT Runat="Server"&gt;

Sub Button_Click (Src As Object, Args As EventArgs)
  Message.Text = "The Button_Click subprogram was run."
End Sub

&lt;/SCRIPT&gt;
</span>
&lt;form Runat="Server"&gt;

&lt;asp:Button Text="Call Button_Click" <b>OnClick="Button_Click"</b> Runat="Server"/&gt;
&lt;asp:Label id="Message" Runat="Server"/&gt;

&lt;/form&gt;
</pre>
<div class="listing"><b>Listing 5-1.</b> Coding a Button control.</div>
<p>Recall that a subprogram called by a button has the special signature <span class="code">(<i>source</i> <b>As Object</b>, <i>arguments</i> <b>As 
EventArgs</b>)</span>. Programmer-supplied names are provided for the <span class="code"><i>source</i></span> and <span class="code"><i>arguments</i></span> 
variables in the argument list. These tutorials use the names <span class="code">(<b>Src</b> As Object</span>, <span class="code"><b>Args</b></span> As 
EventArgs); other identifiers can be used. It is common to see the names <span class="code">"sender"</span> and <span class="code">"e"</span> used for the two 
arguments: <span class="code">(sender As Object, e As EventArgs)</span>.</p>
<p>The <span class="code"><i>source</i></span> argument identifies the button that 
is clicked to call the subprogram; <span class="code">arguments</span> refer to 
any supporting data items passed to the subprogram. In the case of a normal 
subprogram call, neither <span class="code"><i>source</i></span> nor <span class="code"><i>arguments</i></span> comes into play. If, however, the subprogram 
needs to refer to the clicked button, say to style it as visual indication that 
it was clicked, then the <span class="code"><i>source</i></span> argument is used. 
In the following example, a button has its colors and label changed to indicate 
it has been clicked.</p>
<div class="page"><br><input id="MyButton2" value="Click Me" name="MyButton2" type="submit"> 
<span id="Message2"></span><br><br></div>
<div class="figure"><b>Figure 5-3.</b> Scripting a button style.</div><pre class="divcode"><span class="script">&lt;%@ Import Namespace="System.Drawing" %&gt;

&lt;SCRIPT Runat="Server"&gt;

Sub Button_Click (<b>Src</b> As Object, Args As EventArgs)
  <b>Src</b>.BackColor = Color.FromName("#FF0000")
  <b>Src</b>.ForeColor = Color.FromName("#FFFFFF")
  <b>Src</b>.Font.Size = FontUnit.Parse("11pt")
  <b>Src</b>.Text = "I've been clicked!"
  Message.Text = "The button '" &amp; <b>Src</b>.id &amp; "' was clicked."
End Sub

&lt;/SCRIPT&gt;
</span>
&lt;form Runat="Server"&gt;

&lt;asp:Button id="MyButton" Text="Click Me" Runat="Server"
  OnClick="Button_Click"/&gt;
&lt;asp:Label id="Message" Runat="Server"/&gt;

&lt;/form&gt;
</pre>
<div class="listing"><b>Listing 5-2.</b> Code to script button styling.</div>
<p>The <span class="code"><i>source</i></span> argument (<span class="code">Src</span> in this example) is the reference used to set server style 
properties of the button along with its <span class="code">Text</span> property. 
The <span class="code">id</span> property of the button (<span class="code">Src.id</span>) is used in a message produced for the output Label. 
Note that it is not necessary to assign the button an <span class="code">id</span>. The <span class="code">Src</span> reference identifies the 
button whether explicitly identified or not.</p>
<p class="head2">Scripting Command Buttons</p>
<p>A Button can be configured as a <b>command</b> button to pass control 
information to a subprogram so that a <i>single</i> script can service 
<i>multiple</i> buttons and take appropriate action for the particular button 
that is clicked. Buttons used in this way are coded with <span class="code">OnCommand</span> (rather than <span class="code">OnClick</span>) event 
handlers. They include <span class="code">CommandName</span> and <span class="code">CommandArgument</span> properties to supply processing control 
information to the subprogram.</p>
<p>A subprogram called with <span class="code">OnCommand</span> has a different 
signature from one called with <span class="code">OnClick</span>. Rather than 
<span class="code">EventArgs</span>, the argument list uses <span class="code"><b>CommandEventArgs</b></span>. It is through this argument that the 
passed <span class="code">CommandName</span> and <span class="code">CommandArgument</span> values are received.</p>
<p>The following example demonstates a subprogram called by a command button. 
The subprogram reports values associated with the arguments passed by the 
button.</p>
<div class="page" style="height: 150px;"><br><input id="MyButton" value="Report Arguments" name="MyButton" type="submit"> 

<p><span id="PassedValues"></span>&nbsp;</p></div>
<div class="figure"><b>Figure 5-4.</b> Using a command button to call a 
subprogram.</div><pre class="divcode"><span class="script">&lt;SCRIPT Runat="Server"&gt;

Sub Report_Arguments (<b>Src</b> As Object, <b>Args</b> As <b>CommandEventArgs</b>)

  PassedValues.Text  = "&lt;b&gt;Src.id&lt;/b&gt; = " &amp; <b>Src.id</b> &amp; "&lt;br/&gt;"
  PassedValues.Text &amp;= "&lt;b&gt;Args.CommandName&lt;/b&gt;= " &amp; _
                        <b>Args.CommandName</b> &amp; "&lt;br/&gt;"
  PassedValues.Text &amp;= "&lt;b&gt;Args.CommandArgument&lt;/b&gt;= " &amp; _
                        <b>Args.CommandArgument</b> &amp; "&lt;br/&gt;"

End Sub

&lt;/SCRIPT&gt;
</span>
&lt;form Runat="Server"&gt;

&lt;asp:Button id="MyButton" Runat="Server"
  Text="Report Arguments"
  OnCommand="Report_Arguments"
  <b>CommandName</b>="Do Something"
  <b>CommandArgument</b>="With This Value"/&gt;

&lt;p&gt;&lt;asp:Label id="PassedValues" Runat="Server"/&gt;&lt;/p&gt;

&lt;/form&gt;
</pre>
<div class="listing"><b>Listing 5-3.</b> Code to script a command button.</div>
<p>When the button is clicked, subprogram <span class="code">Report_Arguments</span> is called. Because the subprogram is called 
through an <span class="code">OnCommand</span> event handler, the button also 
sends its <span class="code">CommandName</span> and, if present, <span class="code">CommandArgument</span> values to the subprogram.</p>
<p>The subprogram identifies the clicked button through its <span class="code"><i>source</i></span> argument. The subprogram also receives through 
its <span class="code"><i>arguments</i></span> list any <span class="code">CommandName</span> and <span class="code">CommandArgument</span> values 
that are passed. In this example, <span class="code">Args.CommandName</span> and 
<span class="code">Args.CommandArgument</span> give these values. Both are 
programmer-supplied values to pass processing control information to the 
subprogram; they have no intrinsic values that are meaningful to the 
subprogram.</p>
<p>The main reason for using an <span class="code">OnCommand</span> event handler 
is to be able to differentiate between multiple buttons calling the same 
subprogram. This capability is illustrated by the following two buttons which 
call the same subprogram. Here, different buttons produce different reports on 
their passed arguments.</p>
<div class="page" style="height: 150px;"><br><input id="FirstButton" value="Report Arguments" name="FirstButton" type="submit"> 
<input id="SecondButton" value="Report Arguments" name="SecondButton" type="submit"> 
<p><span id="PassedValues1"></span></p></div>
<div class="figure"><b>Figure 5-5.</b> Using command buttons to call a 
subprogram.</div><pre class="divcode"><span class="script">&lt;SCRIPT Runat="Server"&gt;

Sub Report_Arguments (Src As Object, Args As CommandEventArgs)

  PassedValues.Text  = "&lt;b&gt;Src.id&lt;/b&gt; = " &amp; <b>Src.id</b> &amp; "&lt;br/&gt;"
  PassedValues.Text &amp;= "&lt;b&gt;Args.CommandName&lt;/b&gt;= " &amp; _
                        <b>Args.CommandName</b> &amp; "&lt;br/&gt;"
  PassedValues.Text &amp;= "&lt;b&gt;Args.CommandArgument&lt;/b&gt;= " &amp; _
                        <b>Args.CommandArgument</b> &amp; "&lt;br/&gt;"
	
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -