當有“Button1.Attributes.Add("onclick", "return confirm('你確定要儲存修改嗎?')");”時,驗證控制項失效的解決方案

來源:互聯網
上載者:User

標籤:log   fun   ova   reg   null   tcl   影響   指令碼   rip   

同一個頁面用Js和伺服器驗證控制項OnClientClick提交問題

實現功能:

        點擊Button按鈕的OnClientClick事件,不會影響伺服器驗證控制項的驗證功能
                                                (驗證不通過,則不提交伺服器頁面)
背景:
       1. 在同一個頁面有伺服器驗證控制項(RegularExpressionValidator),多個
       2. 有Javascript函數,實現驗證和其他功能
       3. Button按鈕,有兩個事件OnClick,OnClientClick
方法:

HTML頁面


     驗證控制項:

    <asp:TextBox ID="txt_Test" runat="server" Width="120px" MaxLength="8"></asp:TextBox>
<asp:RegularExpressionValidator ID="validator_Test" runat="server" ControlToValidate="txt_Test"
 ErrorMessage="數字" ValidationExpression="^/d+$"></asp:RegularExpressionValidator>

   

     按鈕:

 <asp:ImageButton ID="btnPutin" runat="server" Text=" 提 交 " OnClick="btnPutin_Click"
  OnClientClick=" if(CheckClientValidate()) { return CheckNullValue()}" ImageUrl="../images/submit1.gif" />

    

      指令碼:

<script type="text/javascript">

     //返回,伺服器驗證控制項是否成功
     //Author:  ADD by Aprillee at 2007-12-27
  //Example: OnClientClick="if(CheckClientValidate()) { return CheckNullValue()}"
  function CheckClientValidate()

  {
     Page_ClientValidate();

      if (Page_IsValid)

      {
          return true;

      }

       else

      {
          return false;
       }
  }
// 以下為自訂指令碼功能
function CheckNullValue()
{
       alert(‘Test‘);
 }
</script>

---------------------本文來自 shfongfeng 的CSDN 部落格 ,全文地址請點擊:5985600?utm_source=copy 

當有“Button1.Attributes.Add("onclick", "return confirm('你確定要儲存修改嗎?')");”時,驗證控制項失效的解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.