简体   繁体   English

如何在 asp 按钮上禁用回发 (System.Web.UI.WebControls.Button)

[英]How to disable postback on an asp Button (System.Web.UI.WebControls.Button)

I have an asp button.我有一个asp按钮。 It's server-side so I can only show it for logged in users, but i want it to run a javascript function and it seems when it's runat="server" it always calls the postback event.它是服务器端的,所以我只能为登录的用户显示它,但我希望它运行一个 javascript 函数,它似乎在 runat="server" 时总是调用回发事件。

I also have a regular button ( <input... >) not running at server and it works fine...我还有一个常规按钮( <input... >)没有在服务器上运行,它工作正常...

How can I make this button only run the javascript and not postback?我怎样才能让这个按钮只运行javascript而不是回发?

完成后让您的 javascript 返回 false。

<asp:button runat="server".... OnClientClick="myfunction(); return false;" />
YourButton.Attributes.Add("onclick", "return false");

或者

<asp:button runat="server" ... OnClientClick="return false" />

You can use jquery click action and use the preventDefault() function to avoid postback您可以使用 jquery 单击操作并使用preventDefault()函数来避免回发

<asp:button ID="btnMyButton" runat="server" Text="MyButton" />


$("#btnMyButton").click(function (e) {
// some actions here
 e.preventDefault();
}

Consider this.考虑一下。

<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequest);
function BeginRequest(sender, e) {
    e.get_postBackElement().disabled = true;

}
     </script>

The others are right that you need your callback to return false;其他人是正确的,您需要回调返回 false; however I'd like to add that doing it by setting the onclick is an ugly old way of doing things.但是我想补充一点,通过设置 onclick 是一种丑陋的旧做事方式。 I'd recommend reading about unobtrusive javascript .我建议阅读有关不显眼的 javascript的内容。 Using a library like jQuery could make your life easier, and the HTML less coupled to your javascript (and jQuery's supported by Microsoft now!)使用像jQuery这样的库可以让您的生活更轻松,并且 HTML 与您的 javascript 的耦合更少(并且 jQuery 现在得到 Microsoft 的支持!)

ASP.NET always generate asp:Button as an input type=submit . ASP.NET 总是生成asp:Button作为input type=submit
If you want a button which doesn't do a post, but need some control for the element on the server side, create a simple HTML input with attributes type=button and runat=server .如果您想要一个不发布帖子的按钮,但需要对服务器端的元素进行一些控制,请创建一个简单的 HTML 输入,其属性type=buttonrunat=server

If you disable click actions doing OnClientClick=return false , it won't do anything on click, unless you create a function like:如果您禁用执行OnClientClick=return false的点击操作,它不会在点击时执行任何操作,除非您创建如下函数:

function btnClick() {
    // do stuff
    return false;
}

另外对于接受的答案,您可以使用 UseSubmitBehavior="false" MSDN

In my case, I solved adding return in the onClientClick:就我而言,我解决了在 onClientClick 中添加返回:

Code Behind代码背后

function verifyField(){
  if (document.getElementById("inputId").checked == "") {
    alert("Fill the field");
    return false;
  }
}

Design Surface设计表面

<asp:Button runat="server" ID="send" Text="Send" onClientClick="return verifyField()" />

You don't say which version of the .NET framework you are using.您没有说您使用的是哪个版本的 .NET 框架。

If you are using v2.0 or greater you could use the OnClientClick property to execute a Javascript function when the button's onclick event is raised.如果您使用的是 v2.0 或更高版本,则可以在引发按钮的 onclick 事件时使用 OnClientClick 属性执行 Javascript 函数。

All you have to do to prevent a server postback occuring is return false from the called JavaScript function.为了防止发生服务器回发,您所要做的就是从被调用的 JavaScript 函数中返回false

With Validation有验证

In this example I used two controls, ddl and txtbox , have a happy coding在这个例子中,我使用了两个控件, ddltxtbox ,有一个愉快的编码

 asp:ScriptManager ID="script1" runat="server" /asp:ScriptManager

    asp:UpdatePanel ID="Panel1" runat="server"
       ContentTemplate

// ASP BUTTON
asp:Button ID="btnSave" runat="server" Text="Save" class="btn btn-success" OnClientClick="return Valid()" OnClick="btnSave_Click"


   /ContentTemplate    
    /asp:UpdatePanel    

  <script type="text/javascript">
        function Valid() {

            if ($("#ctl00_ContentPlaceHolder1_YOUR CONTROL NAME").val() == 0) {
                alert("Please select YOUR TEXT");
                $("#ctl00_ContentPlaceHolder1_YOUR CONTROL NAME").focus();
                return false;
            }

            if ($("#ctl00_ContentPlaceHolder1_YOUR CONTROL NAME").val().length == 0) {
                alert("Please Type YOUR TEXT");
                $("ctl00_ContentPlaceHolder1_YOUR CONTROL NAME").focus();
                return false;
            }
            return true;
        }
</script>

you can use the code:您可以使用以下代码:

 <asp:Button ID="Button2" runat="server" Text="Pulsa" OnClientClick="this.disabled=true" UseSubmitBehavior="False"/>

if nee submit如果需要提交

 ... <form id="form1" runat="server" onsubmit="deshabilita()"> ... <script type="text/javascript"> function deshabilita() { var btn = "<%= Button1.ClientID %>"; if (confirm("Confirme postback")) { document.getElementById(btn).disabled = true; return true; } return false; } </script>

You can use JQuery for this您可以为此使用 JQuery

<asp:Button runat="server" ID="btnID" />

than in JQuery比在 JQuery 中

$("#btnID").click(function(e){e.preventDefault();})

You just need to return false from the你只需要从

 function jsFunction() { try { // Logic goes here return false; // true for postback and false for not postback } catch (e) { alert(e.message); return false; } }
 <asp:button runat="server" id="btnSubmit" OnClientClick="return jsFunction()" />

JavaScript function like below. JavaScript 函数如下所示。

Note* : Always use try-catch blocks and return false from catch block to prevent incorrect calculation in javaScript function.注意* :始终使用 try-catch 块并从 catch 块返回 false 以防止 javaScript 函数中的错误计算。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Web.UI.WebControls.TableCell的替代品 - Alternative to System.Web.UI.WebControls.TableCell 如何在System.Web.UI.WebControls.Menu中选择菜单项时传递命令参数 - How to pass command argument while selecting a Menu Item in System.Web.UI.WebControls.Menu 无法从“字符串”转换为“ System.Web.UI.WebControls.TreeNode” - cannot convert from 'string' to 'System.Web.UI.WebControls.TreeNode' System.Web.UI.WebControls.Xml是否使用XslCompiledTransform? - Does System.Web.UI.WebControls.Xml use XslCompiledTransform? 我想禁用asp.net Button的回发,但是jQuery仍将执行相同的工作吗? - I want to Disable postback of asp.net Button but will still be do the same job by jQuery? asp:中继器footerTemplate中的按钮无效回发 - asp:Button inside repeater footerTemplate invalid postback 无法将类型“ System.Web.UI.WebControls.LoginView”隐式转换为“登录” - Cannot implicitly convert type “System.Web.UI.WebControls.LoginView” to “Login” 无法将类型&#39;string&#39;隐式转换为&#39;System.Web.UI.WebControls.Unit&#39; - Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.Unit' 将varchar值&#39;System.Web.UI.WebControls.CheckBox&#39;转换为数据类型位时转换失败 - Conversion failed when converting the varchar value 'System.Web.UI.WebControls.CheckBox' to data type bit &#39;System.Web.UI.WebControls.ImageButton&#39;没有名为&#39;UpdateProgress&#39;的公共属性 - 'System.Web.UI.WebControls.ImageButton' does not have a public property named 'UpdateProgress'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM