电竞比分网-中国电竞赛事及体育赛事平台

分享

Button的CausesValidation屬性的作用

 寒木蕭條 2010-10-13
Button的CausesValidation屬性的作用:
 
獲取或設(shè)置一個值,該值指示在單擊 Button 控件時是否執(zhí)行驗證。
ASP.NET
<asp:Button CausesValidation="True|False" />
屬性值
類型:System..::.Boolean
如果在單擊 Button 控件時執(zhí)行驗證,則為 true;否則為 false。默認值為 true。
實現(xiàn)
IButtonControl..::.CausesValidation

 備注
默認情況下,單擊 Button 控件時執(zhí)行頁驗證。頁驗證確定頁上與驗證控件關(guān)聯(lián)的輸入控件是否均通過該驗證控件所指定的驗證規(guī)則。
通過使用 CausesValidation 屬性,可以指定或確定當(dāng)單擊 Button 控件時,是否同時在客戶端和服務(wù)器上執(zhí)行驗證。若要禁止執(zhí)行驗證,請將 CausesValidation 屬性設(shè)置為 false。
說明:
當(dāng)使用 PostBackUrl 屬性回發(fā)至不同頁面時,應(yīng)將 CausesValidation 屬性設(shè)置為 false。在回發(fā)至不同頁面時,應(yīng)對驗證進行顯式檢查。有關(guān)示例,請參見 PostBackUrl 屬性的“備注”部分。
 
對于 reset 或 clear 按鈕,此屬性通常設(shè)置為 false,以防止在單擊其中某個按鈕時執(zhí)行驗證。
當(dāng) CausesValidation 屬性的值設(shè)置為 true 時,還可以使用 ValidationGroup 屬性來指定 Button 控件引發(fā)驗證時所對應(yīng)的驗證組的名稱。
無法通過主題或樣式表主題設(shè)置此屬性。有關(guān)更多信息,請參見 ThemeableAttribute和 ASP.NET 主題和外觀概述。
 示例
下面的代碼示例演示如何使用 CausesValidation 屬性防止發(fā)生頁驗證。注意,Validate 方法會單獨激活各個驗證控件。
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
<head id="Head1" runat="server">
    <title> Button CausesValidation Example </title>
<script runat="server">
      void SubmitButton_Click(Object sender, EventArgs e)
      {
         // Determine which button was clicked.
         switch(((Button)sender).ID)
         {
            case "CityQueryButton":
               // Validate only the controls used for the city query.
               CityReqValidator.Validate();
               // Take the appropriate action if the controls pass validation.
               if (CityReqValidator.IsValid)
               {
                  Message.Text = "You have chosen to run a query for the following city: " +
                     CityTextBox.Text;
               }
               break;
            case "StateQueryButton":
               // Validate only the controls used for the state query.
               StateReqValidator.Validate();
               // Take the appropriate action if the controls pass validation.
               if (StateReqValidator.IsValid)
               {
                  Message.Text = "You have chosen to run a query for the following state: " +
                     StateTextBox.Text;
               }
               break;
            default:
               // If the button clicked isn't recognized, erase the message on the page.
               Message.Text = "";
               break;
         }
      }
   </script>
</head>
<body>
   <form id="form1" runat="server">
      <h3> Button CausesValidation Example </h3>
      <table border="1" cellpadding="10">
         <tr>
            <td>
               <b>Enter city to query.</b> <br />
               <asp:TextBox ID="CityTextBox"
                    runat="server"/>
               <asp:RequiredFieldValidator ID="CityReqValidator"
                    ControlToValidate="CityTextBox"
                    ErrorMessage="<br />Please enter a city."
                    Display="Dynamic"
                    EnableClientScript="False"
                    runat="server"/>
            </td>
            <td valign="bottom">
               <asp:Button ID="CityQueryButton"
                    Text="Submit"
                    CausesValidation="False"
                    OnClick="SubmitButton_Click"
                    runat="server"/>
            </td>
         </tr>
         <tr>
            <td>
               <b>Enter state to query.</b> <br />
               <asp:TextBox ID="StateTextBox" 
                    runat="server"/>
               <asp:RequiredFieldValidator ID="StateReqValidator"
                    ControlToValidate="StateTextBox"
                    ErrorMessage="<br />Please enter a state."
                    Display="Dynamic"
                    EnableClientScript="False"
                    runat="server"/>
            </td>
            <td valign="bottom">
            <!--  這里把  CausesValidation="false" 默認時為true 會對整個頁面驗證 (就是驗證標(biāo)簽所做的驗證) 這里如果沒有OnClick="SubmitButton_Click" 則不會執(zhí)行驗證-->
               <asp:Button ID="StateQueryButton"
                    Text="Submit"
                    CausesValidation="false"
                    OnClick="SubmitButton_Click"
                    runat="server"/>
            </td>
         </tr>
      </table>
      <br /><br />
      <asp:Label ID="Message"
           runat="Server"/>
   </form>
</body>
</html>
 
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/huazi123654/archive/2008/12/24/3597489.aspx

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多