• ASP .Net提交时禁用Button


    转自:http://gaterking.blog.51cto.com/69893/301691/

    今天遇到一种小情况,想要在.net的服务器控件asp:button按下去时通过OnClientClick进行客户端处理,成功就提交到后台,提交后button变灰,disabled=true。但问题来了,在 OnClientClick="return check();",js函数check如果设置button..disabled = true,将不再提交。

    上网查了很多资料,最后总结了一下,成功完成。

    1、设置asp:button属性UseSubmitBehavior="false",这样button type将变成“button”,而不是默认的“submit”;

    2、js函数中调用__doPostBack进行提交,这步很关键,__doPostBack可以让客户端的js触发服务器端的事件(页面会回发),网上很多的教程都没有这部,可能他们都不是在OnClientClick做验证处理,不需要return的原因。

    下面这两段代码可以参考下。

    1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebTest.aspx.cs" Inherits="DoubleClick.WebTest" %
    2.  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    4.  
    5.  
    6. <html xmlns="http://www.w3.org/1999/xhtml" 
    7. <head id="Head1" runat="server"
    8.     <title>Untitled Page</title
    9.     <script type="text/javascript"
    10.  
    11.     function check() { 
    12.         if (document.getElementById("TextBox1").value == "") { 
    13.             alert("空"); 
    14.             return false; 
    15.         } else { 
    16.             document.getElementById("Button4").disabled = true; 
    17.             __doPostBack('Button4', ''); 
    18.             return true; 
    19.         } 
    20.     } 
    21.     </script
    22. </head
    23. <body
    24.     <form id="form1" runat="server" 
    25.     <div
    26.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox
    27.         <asp:Button ID="Button4" UseSubmitBehavior="false" runat="server" Text="My" OnClientClick="return check();" OnClick="Button1_Click" /> 
    28.     </div
    29.     </form
    30. </body
    31. </html


     

      1. public partial class WebTest : System.Web.UI.Page 
      2.     { 
      3.         protected void Page_Load(object sender, EventArgs e) 
      4.         { 
      5.         } 
      6.  
      7.         protected void Button1_Click(object sender, EventArgs e) 
      8.         { 
      9.             Response.Write(TextBox1.Text); 
      10.             System.Threading.Thread.Sleep(2000); 
      11.         } 
      12.     } 
  • 相关阅读:
    找最后的字符
    统计指定数字和
    班级排队
    python编程题
    python函数题
    2019.6.24-2019.6.28(实训数据结构)3.数组编码求解问题
    2019.6.24-2019.6.28(实训数据结构)1.商品管理系统实训c++(实现的基本功能:初始化,创建表,插入,删除,更新,查询,链表数据与文件之间的转换)
    2019.6.24-2019.6.28(实训数据结构) 2.背包问题
    2019.6.24-2019.6.28(实训数据结构)4.树和二叉树应用项目
    2019.6.24-2019.6.28(实训数据结构)5.图的邻接矩阵表示
  • 原文地址:https://www.cnblogs.com/yzwdli/p/4054329.html
Copyright © 2020-2023  润新知