简体   繁体   English

如何防止使用JavaScript执行服务器端功能?

[英]How to prevent execution of serverside function using javascript?

I am trying to test for a textbox value. 我正在尝试测试文本框值。

If the value matches my criteria do a javascript function 如果值符合我的标准,请执行javascript函数

ELSE 其他

do textchanged event in code behind. 在后面的代码中执行textchanged事件。

I have a jquery function that is triggered using javascript on change 我有一个jQuery函数,在更改时使用javascript触发

JAVASCRIPT JAVASCRIPT

function test() {
  if ($('#<%= txt.ClientID%>').val() == '1') {
      return false;
    }
};

ASP.NET ASP.NET

<asp:TextBox ID="txt" runat="Server" AutoPostBack="true" onchange="test(this)" />

VB.NET VB.NET

Protected Sub txt_TextChanged(sender As Object, e As EventArgs) Handles txt.TextChanged
...
End Sub

While debugging the function clearly returns false however the textchanged event still causes a postback. 在调试该函数时,显然会返回false,但是textchanged事件仍然会导致回发。

THE MARKUP RENDERS AS 标记渲染商

onchange="test(this);setTimeout('__doPostBack(\'ctl00$ctl00$ctl00$Master$Content$txt\',\'\')', 0)"

QUESTION

How can the textchanged event be restricted to only execute when certain client side criteria are met? 如何将textchanged事件限制为仅在满足某些客户端条件时才执行?

try to return false inline of textbox as below:- 尝试返回错误的文本框内联,如下所示:

function test() {
    if ($('#<%= txt.ClientID%>').val() == '1') {
        return false;
    }
    return true;
};


<asp:TextBox ID="txt" runat="Server" AutoPostBack="true" onchange="if(!test(this)) return false;" />

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

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