从ASP.NET调用Javascript函数

时间:2022-04-09 15:59:21

My code is:

我的代码是:

<body>
<script type="text/javascript">
    function change() {
        alert("Hello");
    }
</script>
<form id="form1" runat="server">
<div>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="change"/>
</div>
</form>
</body>

The error I get is: CS1061: 'ASP.webform1_aspx' does not contain a definition for 'change' and no extension method 'change' accepting a first argument of type 'ASP.webform1_aspx' could be found.

我得到的错误是:CS1061:'ASP.webform1_aspx'不包含'change'的定义,并且没有可以找到接受'ASP.webform1_aspx'类型的第一个参数的扩展方法'change'。

What is the problem?

问题是什么?

3 个解决方案

#1


1  

It should be OnClientClick:-

它应该是OnClientClick: -

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return change();"/>

OnClick event is used to bind server side method on button click.

OnClick事件用于在按钮单击时绑定服务器端方法。

<script type="text/javascript">
    function change() {
        alert("Hello");
        return false;
    }
</script>

Not that the return false will stop the postback cause by the button.
And for this you should also mention return before your function call on button like OnClientClick="return change();"

并不是返回false将停止按钮的回发原因。为此你还应该在函数调用onClientClick =“return change();”之前提到return。

#2


3  

It should be OnClientClick:-

它应该是OnClientClick: -

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="change"/>

OnClick event is used to bind server side method on button click.

OnClick事件用于在按钮单击时绑定服务器端方法。

#3


1  

OnClick="change" it means that you should Create Method name as change on server Side

OnClick =“更改”意味着您应该在服务器端创建方法名称作为更改

Some thing like this

有点像这样

protected void change(object sender,EventArgs e)
{

}

On Server Side

在服务器端

if(!Page.IsPostBack)
{
Button1.Attributes.Add("onclick","change();")
}

on .aspx

<asp:Button ID="Button1" runat="server" Text="Button" />

#1


1  

It should be OnClientClick:-

它应该是OnClientClick: -

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return change();"/>

OnClick event is used to bind server side method on button click.

OnClick事件用于在按钮单击时绑定服务器端方法。

<script type="text/javascript">
    function change() {
        alert("Hello");
        return false;
    }
</script>

Not that the return false will stop the postback cause by the button.
And for this you should also mention return before your function call on button like OnClientClick="return change();"

并不是返回false将停止按钮的回发原因。为此你还应该在函数调用onClientClick =“return change();”之前提到return。

#2


3  

It should be OnClientClick:-

它应该是OnClientClick: -

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="change"/>

OnClick event is used to bind server side method on button click.

OnClick事件用于在按钮单击时绑定服务器端方法。

#3


1  

OnClick="change" it means that you should Create Method name as change on server Side

OnClick =“更改”意味着您应该在服务器端创建方法名称作为更改

Some thing like this

有点像这样

protected void change(object sender,EventArgs e)
{

}

On Server Side

在服务器端

if(!Page.IsPostBack)
{
Button1.Attributes.Add("onclick","change();")
}

on .aspx

<asp:Button ID="Button1" runat="server" Text="Button" />