简体   繁体   English

在后面的代码上调用Javascript函数

[英]Calling Javascript function on code behind

I am new to Asp.Net, I want my login page to be validate on Button Click using Java script but on .cs file. 我是Asp.Net的新手,我希望我的登录页面能够在Button Click上使用Java脚本进行验证,但是在.cs文件上。 Here is my code: 这是我的代码:

<script type="text/javascript">
    function CheckisEmpty()
    {
        var username = document.getElementById('txtUserName').innerHTML;
        var password = document.getElementById('txtPwd').innerHTML;

        if (username != '' || password != '')
        {

            return true;
        }
        else
        {
            alert("Feild Cannot Be Left Blank");
            return false;
        }

    }
</script>

<asp:Button ID="btnLogin" Text="Login" runat="server" Width="120px" OnClick="btnLogin_Click" OnClientClick="return CheckisEmpty();" CssClass="button"></asp:Button>

btnLogin.Attributes.Add("OnClick", "CheckisEmpty()");

Where am I wrong? 我哪里错了?

on your aspx page add this attribute on your button onClientClick="CheckisEmpty()" 在您的aspx页面上,在您的按钮上添加此属性onClientClick="CheckisEmpty()"

No need to do on .cs page. 无需在.cs页面上进行操作。

This way, first the js function will be executed, If the validation is successful, return true from the js function , else return false and give some notification to the user. 这样,首先执行js函数,如果验证成功,则从js函数return true ,否则return false并向用户发出一些通知。 When false is returned, The process will not continue to your cs function and thus page will not be post-back. 返回false时,进程将不会继续执行cs功能,因此页面不会回发。

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

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