gpt4 book ai didi

javascript - asp net webforms禁用提交按钮

转载 作者:行者123 更新时间:2023-11-29 09:52:44 24 4
gpt4 key购买 nike

我在 Webforms 中遇到了一个小问题。我试图在提交时禁用提交按钮,以防止重复发布。问题是,如果在回发期间禁用提交按钮,则不会调用代码隐藏中的 onclick 方法。回发仍然发生,但不会调用按钮的 onclick 方法。有办法解决这个问题吗?

这里是问题的一个小演示:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="WebApplication2._default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-2.0.3.min.js"></script>

<script>
function disableButton() {
//$('#<%=btn.ClientID%>').attr('disabled', 'diabled');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal runat="server" ID="ltrDate" />
<asp:Button runat="server" ID="btn" OnClientClick="disableButton();" Text="Hit me!" OnClick="Unnamed_Click" />
</div>
</form>
</body>
</html>

代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Unnamed_Click(object sender, EventArgs e)
{
Thread.Sleep(1000);
ltrDate.Text = DateTime.Now.ToString();
}
}
}

如果运行此代码,它工作正常,但如果//从 javascript 方法中删除,按钮的 onclick 方法将不再被调用。 (回发仍然正常)

/马丁

更新:这似乎是一个老问题..

I never got this to work, because when the button is disabled client-side, its information is no longer posted back to the server, so its server-side click event does not fire. I suspect that's the behavior you're seeing. If you do manage to make this work, I'd love to know how. Ian Olsen Wednesday, June 09, 2004

最佳答案

更简单的解决方案:

<asp:Button ID="btnFind" runat="server" Text="Find"
OnClientClick="if(this.alreadClicked == true) {this.disabled = true;
this.value = 'Wait...';} else {this.alreadClicked = true;}" EnableViewState=false
</asp:Button>

您无论如何都禁用该按钮并向用户提供反馈“等待...”,但必须使用 EnableViewState=false 以便在页面刷新时按钮重置为原始状态。

关于javascript - asp net webforms禁用提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19024402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com