0

I am trying to call a javascript function from code behind on the button click event. below is my javascript code.

  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "strScript", "javascript:clearBox();", true);

and the function is

<script type="text/javascript">

function clearBox() {


    alert("Test");

}
</script>

I get an error "Object expected"

4
  • 1
    I see no button in your code. Commented Oct 2, 2012 at 21:14
  • 1
    ..and what seems to be the problem? or your question? Commented Oct 2, 2012 at 21:14
  • It looks like you are using inline JS. If so please don't and use an eventListener. Commented Oct 2, 2012 at 21:15
  • You get the error "Object expected" where exactly? What code is actually throwing the error? It sounds like a JavaScript error, so the C# code won't be the place to look. Whatever is emitted to the client is where you'd look. Commented Oct 2, 2012 at 21:18

2 Answers 2

4

You should use OnClientClick property of a button:

<asp:Button runat="server" OnClientClick="clearBox()" />

RegisterClientScriptBlock will only place a piece of code on a page:

<script type="text/javascript">
      javascript:clearBox();
</script>

And this will not work as it is not a valid code (because of javascript: which is used for anchors to run js from href: <a href="javascript:some_code_here()"></a>)

Another option, if you want RegisterClientScriptBlock is to use addEventListener to assign onclick handler to your button. Here is code snippet for this (cross browser and pure JS). It may be used like this:

  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "strScript", "addListener('"+button.ClientID+"', 'click', clearBox);", true);

But it is better to replace RegisterClientScriptBlock with [RegisterStartupScript](http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx) where first one may be called before all HTML is loaded and there may be no button yet.

Upd Well, thanks to hvd. He reminded me that there are labels in JS and actually javascript:clearBox() is a valid code, but it will not do what OP wants

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

5 Comments

"as it is not a valid code" -- it's an unused useless label attached to a statement, but I believe it's perfectly valid. See here for how labels work in JS, they're not often used (and for good reasons), but they are part of the language.
Hm.. need to try. But I do not think so) Let me check that with js fiddle
@hvd Uh. Sorry. I see your point. Never use that thing and actually forgot about that possibility
Perhaps worse: it will do exactly what the OP wants, but for the wrong reasons. The label will just be ignored if it is unused, so clearBox will be called as intended.
@hvd yeah, you are right. But he wants to call it on click, so "will not do what OP wants" ;)
0

Use this code:

Page.ClientScript.RegisterStartupScript(this.GetType(),  "Error", "clearBox();", true);

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.