Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"

share|improve this question
1  
I see no button in your code. – James Montagne Oct 2 '12 at 21:14
1  
..and what seems to be the problem? or your question? – Thousand Oct 2 '12 at 21:14
It looks like you are using inline JS. If so please don't and use an eventListener. – PeeHaa Oct 2 '12 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. – David Oct 2 '12 at 21:18
add comment (requires an account with 50 reputation)

2 Answers

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

share|improve this answer
"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. – hvd Oct 2 '12 at 21:20
Hm.. need to try. But I do not think so) Let me check that with js fiddle – FAngel Oct 2 '12 at 21:22
@hvd Uh. Sorry. I see your point. Never use that thing and actually forgot about that possibility – FAngel Oct 2 '12 at 21:23
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 Oct 2 '12 at 21:27
@hvd yeah, you are right. But he wants to call it on click, so "will not do what OP wants" ;) – FAngel Oct 2 '12 at 21:30
show 1 more commentadd comment (requires an account with 50 reputation)

Use this code:

Page.ClientScript.RegisterStartupScript(this.GetType(),  "Error", "clearBox();", true);
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.