"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
<head id="Head1" runat="server">
    
<title>CheckBox CheckedChanged Example</title>
<script runat="server">

      
void Check_Clicked(Object sender, EventArgs e) 
      {

         
// Calculate the subtotal and display the result in currency format.
         
// Include tax if the check box is selected.
         Message.Text = CalculateTotal(checkbox1.Checked).ToString("c");

      }

      
void Page_Load(Object sender, EventArgs e)
      {

         
// Display the subtotal without tax when the page is first loaded.
         if(!IsPostBack)
         {

            
// Calculate the subtotal and display the result in currency format.
            Message.Text = CalculateTotal(false).ToString("c");

         }

      }

      
double CalculateTotal(bool Taxable)
      {

         
// Calculate the subtotal for the example.
         double Result = 1.99 + 2.99 + 3.99;

         
// Add tax, if applicable.
         if(Taxable)
         {
            Result 
+= Result * 0.086;
         }

         
return Result; 

      }

   
</script>

</head>

<body>

   
<form id="form1" runat="server">

      
<h3>CheckBox CheckedChanged Example</h3>

      Select whether to include tax 
in the subtotal.

      
<br /><br />

      
<table border="1" cellpadding="5">

         
<tr>

            
<th colspan="2">

               Shopping cart

            
</th>

         
</tr>

         
<tr>

            
<td>

               Item 
1

            
</td>

            
<td>

               $
1.99

            
</td>

         
</tr>

         
<tr>

            
<td>

               Item 
2

            
</td>

            
<td>

               $
2.99

            
</td>

         
</tr>

         
<tr>

            
<td>

               Item 
3

            
</td>

            
<td>

               $
3.99

            
</td>

         
</tr>

         
<tr>

            
<td>

               
<b>Subtotal</b>

            
</td>

            
<td>

               
<asp:Label id="Message" runat="server"/>

            
</td>

         
</tr>

         
<tr>

            
<td colspan="2">

               
<asp:CheckBox id="checkbox1" runat="server"
                    AutoPostBack
="true"
                    Text
="Include 8.6% sales tax"
                    TextAlign
="Right"
                    OnCheckedChanged
="Check_Clicked"/>

            
</td>

         
</tr>

      
</table>

   
</form>

</body>

</html>


注意三点

点击是触发事件OnCheckedChanged="Check_Clicked"

如果AutoPostBack="True"不设置为true,则不会即使触发

CheckBox的checked属性放回true和false

 

相关文章:

  • 2021-12-16
  • 2022-12-23
  • 2021-10-30
  • 2021-08-03
  • 2021-06-20
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-02
  • 2021-12-29
  • 2021-06-05
  • 2022-02-08
  • 2021-12-14
  • 2022-12-23
相关资源
相似解决方案