c# - 防止 Asp.NET 按钮提交表单

标签 c# asp.net forms paypal

所以,我有一个 Paypal 表单,效果非常好。但是,我现在需要添加一个优惠券字段,有人可以在其中输入代码,并根据后端的回复获得折扣。

这一切都非常有效,但是我在添加选项以了解检查之前您的代码是否有效时遇到了问题。目前,我的表单(经过简化)如下所示:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"
    id="payPalForm" runat="server">
    <asp:TextBox runat="server" ID="txtCode" />
    <asp:Button Text="Validate" OnClick="ValidateDiscount" runat="server" />
    <asp:Label ID="txtDesc" runat="server" />

    <input type="submit" name="Submit" value="Pay up!" />

    <input type="hidden" name="cmd" value="_cart" />
    <input type="hidden" name="upload" value="1" />
    ...
</form>

后端具有以下功能:

protected void ValidateDiscount(object sender, EventArgs e)
{
    this.txtDesc.Text = "fetch from database using: " + txtCode.Text;
}

我的问题是,每当我单击“验证”按钮时,都会提交表单,而我最终会访问 Paypal 网站。我首先将 Jquery 与 preventDefault() 一起使用,但这实际上阻止了我的服务器端函数触发。我也试过放一个标准的 <button><input type='button'>标记,但我无法让它触发我的服务器端功能。

有没有办法让验证按钮不提交表单,或者我应该从表单中删除操作并在单击提交按钮时手动提交表单?

最佳答案

您已将表单操作设置为发布到 PayPal。

这是 Action :

action="https://www.sandbox.paypal.com/cgi-bin/webscr"

这是你在表单标签中的位置:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"
id="payPalForm" runat="server">

从您的表单标签中删除它,它应该回发到您的应用程序。

关于c# - 防止 Asp.NET 按钮提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33549781/

相关文章:

c# - 如何修复错误 "Cannot load PowerShell snap-in Microsoft.PowerShell.Diagnostics because of the following error: Could not load file or assembly"

javascript - 使用 JS 后备在单选按钮检查上显示 div

html - "+"和 "%A0"之间的区别 - urlencoding?

php - 从另一个页面更新 JavaScript 变量的值

asp.net - 默认提供静态文件index.html

c# - 编译一个dlib的.dll文件

c# - 解析值 : . 路径 '' 时遇到错误意外字符,第 1 行,位置 1

c# - 在代码中保存 SQL Server 登录 (C#)

asp.net - 如何在使用 docker 容器化的 asp.net Core 中的项目之间共享类定义

c# - 将连接字符串从 app.config 移动到 C# 中的代码