在asp.net中使用javascript防止服务器请求 [英] Preventing server request with javascript in asp.net

查看:94
本文介绍了在asp.net中使用javascript防止服务器请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EveryBody



我对asp.net有一个小问题。我有一个小的asp.net表单,其中包含一个文本框和一个按钮,用于将文本框文本发送到服务器。但我想只发送数字文本。所以我想用javascript评估文本,如果它不是数字不发送到服务器,但如果它是数字发送到服务器并插入到sqlserver。但每次发送到服务器。



我的asp.net代码



< pre lang =   xml>< html xmlns =   http://www.w3.org/1999/xhtml  >  
< head runat = server >
< script 类型 = text / javascript language = javascript >
function isNumeric(num){
return !isNaN(num)
}

function validateForm(){
var x = document.forms [ ctl01] [ NumberTxt]。 value ;
if (x == null || x == ){
alert( 必须填写名字);
return false ;
}
if (!isNumeric(x)){
alert( 它不是数值);
return false ;
}
}

< / script >
< / head >
< body>
< form runat = server >
< asp:TextBox ID = NumberTxt runat = server> hello < / asp:TextBox >
< asp:按钮ID = 提交 runat = server Text = 提交
OnClientClick = validateForm() onclick = Submit_Click />
< / 表格 >
< / body >
< / html >

和我的服务器端代码用于按钮


protected void Submit_Click( object sender,EventArgs e)
{
Response.Write( hello serer);
}

解决方案

尝试调用JavaScript函数的返回值。



喜欢:

 <   asp:按钮    ID   = 提交    runat   = 服务器   文本  = 提交    onClientClick   =  return validateForm();     onclick   =  Submit_Click    /  >  





开单击按钮,如果您的JavaScript函数返回true,则asp.net将执行回发,如果返回false,则不会发生回发。



希望它有所帮助。


EveryBody

I have a small problem about asp.net. I have a small asp.net form which contains one textbox and one button to send textbox text to server. but i want to send only numeric texts. so i want to evaluate text with javascript and if it wasn't numerical don't send to server but if it was numeric send to server and insert into sqlserver. but each time it sends it to server.

my asp.net code

<pre lang="xml"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" language="javascript">
        function isNumeric(num) {
            return !isNaN(num)
        }

        function validateForm() {
            var x = document.forms["ctl01"]["NumberTxt"].value;
            if (x == null || x == "") {
                alert("First name must be filled out");
                return false;
            }
            if (!isNumeric(x)) {
                alert("it is not numerical value");
                return false;
            }
        }

    </script>
</head>
<body>
   <form runat="server">
    <asp:TextBox ID="NumberTxt" runat="server">hello</asp:TextBox>
    <asp:Button ID="Submit" runat="server" Text="Submit"
        OnClientClick="validateForm()" onclick="Submit_Click" />
    </form>
</body>
</html>

and my server side code for button


protected void Submit_Click(object sender, EventArgs e)
    {
        Response.Write("hello serer");
    }

解决方案

Try calling for the return value of your JavaScript functions.

like:

<asp:Button ID="Submit" runat="server" Text="Submit" onClientClick="return validateForm();" onclick="Submit_Click" />



On the button click, if your JavaScript function returns true, then asp.net will perform a postback, if it returns false, no postback will occur.

Hope it helps.


这篇关于在asp.net中使用javascript防止服务器请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆