1

In default.aspx I have:

 <form id="form1" runat="server">
    <div>
    <asp:Button ID="clikme" runat="server" Text="click me" />    
    </div>
    </form>

In default.aspx I have:

clikme.Attributes.Add("OnClick", "javaScript: return myfunction();");

And in JScript1.js I have

function myFunction() {
    alert('this is my function');
    return false;
}

The above code does not work it shows 'Microsoft JScript runtime error: Object expected'. I can't figure out how to find a solution.

6 Answers 6

1

You are call function with wrong name myfunction() should be myFunction() as javascript is case sensitive. Also make sure you include the JScript1.js in the current aspx file. You can read this MSDN artile to learn how to include js file.

clikme.Attributes.Add("OnClick", "javaScript: return myFunction();");

To include js file

<script type="text/javascript" src="yourDirectorIfAny/JScript1.js" ></script>
Sign up to request clarification or add additional context in comments.

1 Comment

How you include js file, just put some alter in js file to ensure your js file is being included.
0

try this add

script type='text/javascript' language="javascript" to your js portion and place your function inside it..

Comments

0

try

clikme.Attributes.Add("OnClientClick", "javaScript: return myFunction();");

Comments

0

The best way is to give reference of javascript file on Aspx page itself as suggested by Adil. If you want to register some javascript methods in code behind then you can have a look at this example.

http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerclientscriptblock.aspx

Comments

0
Something like below will helpfull.. 

string script = "myFunction();";
AjaxControlToolkit.ToolkitScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", script, true);             

Comments

0

JScript is CASE SENSITIVE language.

clikme.Attributes.Add("OnClick", "javaScript: return myfunction();");
function myFunction() {
...
}

Check above lines. myFunction function must be equal.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.