0

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.

What is the problem?

3 Answers 3

3

It should be OnClientClick:-

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

It should be OnClientClick:-

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

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

<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();"

Comments

1

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

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" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.