简体   繁体   English

javaScript函数在我的ASP.Net控件上不起作用

[英]javaScript function doesnt work on my ASP.Net control

I have a validator control which when I add java Script function to it it doesn't work and a button which I try to change it Text it doesn't work too. 我有一个验证器控件,当我向其中添加Java脚本函数时,该控件不起作用;我尝试对其进行更改的按钮也不起作用。 the code is Bellow 代码是贝娄

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">

        function clientValidate(source, args)
        {
            if (parseInt(args.Value) < 10)
            {
                return source.IsValid = false;

            }
            else
            {
                return source.IsValid = true;
            }
        }

        function Alert(ctr)
        {
            alert("Hello");
            ctr.Text = "clicked";
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Value Must not be Below 10" ClientValidationFunction="clientValidate" OnServerValidate="CustomValidator1_ServerValidate" ControlToValidate="txt" Display="Dynamic" ForeColor="#FF5050"></asp:CustomValidator>
            &nbsp;&nbsp;
            <asp:TextBox ID="txt" runat="server" AutoPostBack="true" ></asp:TextBox> 
            &nbsp; 
            <asp:Button ID="btn1" runat="server" Text="Button" OnClientClick="Alert(btn1)" />

        </div>
    </form>
</body>
</html>

One thing to say is that the Alert script work but the text of button didn't get changed. 要说的一件事是Alert脚本可以工作,但是button的文本没有更改。

An other thing is the server side event of validator is in separate cs file. 另一件事是验证器的服务器端事件在单独的cs文件中。

Thanks for help. 感谢帮助。

client side does not know about Text and its only accessible on server. 客户端不知道文本,并且只能在服务器上访问。 you should use client side attribute instead. 您应该改为使用客户端属性。

use this: 用这个:

 function Alert(ctr)
    {
        alert("Hello");
        document.getElementById(ctr).value= "clicked";
    }

and

 <input type="button" id="btn1" value="Button" onClick="Alert('btn1')"/>

using of js functions prevents from doing postback on server. 使用js函数可防止在服务器上进行回发。

Your usage of arguments in the client validation function is incorrect. 您在客户端验证功能中使用的参数不正确。 Correct usage is like this. 正确用法是这样的。

function clientValidate(source, args)
{
//sender is <span...>err</span> extended properties include controltovalidate
//args is {Value: 123, IsValid: true}
    if (parseInt(args.Value) < 10)
    {
        args.IsValid = false; //not source and no return
        //other logic

    }
    else
    {
        //args.IsValid = true; //redundant. **true** is default value
        //return source.IsValid = true; //see above
        //other logic
    }
}

As to Alert function. 至于Alert功能。 The function is correct and may work. 该功能正确并且可以正常工作。 The call is incorrect. 通话不正确。 Call the function like this. 这样调用该函数。

<asp:Button ID="btn1" runat="server" Text="Button" OnClientClick="Alert(this)" />

Note this argument in the call. 注意调用中的this参数。

Update for comment / extra question 更新评论/其他问题

Custom Alert function may take more arguments. 自定义Alert功能可能需要更多参数。 Something like this. 这样的事情。

 function Alert(ctr, msg, evt) { if (evt) evt.preventDefault(); switch (ctr.tagName.toLowerCase()) { case 'input': case 'textarea': ctr.value = msg; break; case 'div': ctr.innerHTML = msg; break; default: alert(msg); break } return false; } 
 <input type="button" value="Click Me" onclick="return Alert(this,'clicked',event);" /><br /> <div onclick="Alert(this,'div is clicked')">Click div</div> 

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

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