简体   繁体   English

为什么我的JavaScript代码无法在我的ASP.NET Web表单页面上运行?

[英]Why is my JavaScript code not working on my ASP.NET web forms page?

I am running into a problem with a form submit in my web forms page. 我在Web表单页面中遇到表单提交问题。 I have a function in place that should after confirming by selecting OK, do a document.forms.submit and send the user to another page. 我有一个功能,应该在确认后选择OK,执行document.forms.submit并将用户发送到另一个页面。

I am not getting any error for the JavaScript when attempting to debug in Chrome and I am not getting an exception at all when trying to step through the C# function. 尝试在Chrome中进行调试时,我没有收到JavaScript的任何错误,在尝试单步调试C#函数时,我没有得到任何异常。 Can someone with more experience with JavaScript input submit forms assist on this or show me a better way with the given code? 有更多JavaScript输入经验提交表单的人可以帮助解决这个问题,或者用给定的代码向我展示更好的方法吗?

protected virtual void WriteCancelDiv(string claim_id, string promotion_id)
{
    string innerHtml = "<form action=\"TestPath/claim_cancel.aspx\" method=post><input type='hidden' name='claim_id' value='" + claim_id + "'><input type='hidden' name='promotion_id' value='" + promotion_id + "'>";
    innerHtml += @"<script> 
                   debugger;
                   function Clicked(){
                       var b = confirm(""This cannot be undone.  Are you sure you want to cancel?"");
                       if (!b) {return;}
                       else {document.forms[0].submit();}
                   }
                   </script>
                   <input class=""btn btn-sm btn-primary"" type=""submit"" value=""Cancel Claim"" onClick=""Clicked();"">
                </form>";
    cancel_div.InnerHtml = innerHtml;
}

Mixing client side logic into ASP.NET web forms can be quite an arduous thing to do. 将客户端逻辑混合到ASP.NET Web表单中可能是一件非常艰巨的事情。 In your code you are injecting HTML (including a form element) into a div. 在您的代码中,您将HTML(包括表单元素)注入div。 The problem with this is that every ASP.NET web control (including your div) is already inside a huge wrapping form element that ensures the post back of these controls. 这个问题是每个ASP.NET Web控件(包括你的div)都已经在一个巨大的包装表单元素中,以确保这些控件的回发。

ASP.NET web forms renders to simple HTML on the client side and this is how it works. ASP.NET Web表单在客户端呈现为简单的HTML,这就是它的工作原理。 There is always one huge form element around everything that you see. 你看到的一切都有一个巨大的形式元素。 Unfortunately adding a form element into another form element is not valid HTML. 不幸的是,将表单元素添加到另一个表单元素中是无效的HTML。 So your approach is not going to work. 所以你的方法不会起作用。

I won't provide any code here, but give you a description on how I would go about solving this. 我不会在这里提供任何代码,但是会告诉你如何解决这个问题。

So this is what you are trying to achieve, if I am not mistaken: 如果我没有弄错的话,这就是你想要实现的目标:

When button A is clicked another button B shows up. 按钮A被点击另一按钮B显示了。 Clicking that button B first a confirmation dialog asks the used wheter he/she really wants to perform that task. 首先单击该按钮B,确认对话框询问他/她是否真的想要执行该任务。 If so, the user is redirected to another page. 如果是,则将用户重定向到另一页面。

I would do it like this: 我会这样做:

  1. You add button B as another server side control with visibility hidden. 您将按钮B添加为另一个隐藏可见性的服务器端控件。
  2. In the click event handler of button A , you set the visibility of button B to visible. 按钮A的单击事件处理程序中,将按钮B的可见性设置为可见。
  3. You can add the confirm dialog to the onClientClick attribute of button B . 您可以将确认对话框添加到按钮BonClientClick属性。
  4. In the click event handler of button B , you do a redirect to the desired page. 按钮B的单击事件处理程序中,您将重定向到所需的页面。

this line 这条线

<input class=""btn btn-sm btn-primary"" type=""submit"" value=""Cancel Claim"" onClick=""Clicked();"">

replace it with 用它替换它

<input class='btn btn-sm btn-primary' type='submit' value='Cancel Claim' onClick='Clicked();'>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM