Am unable to understand why Cannot the below javascript code is not called from code behind

I have a simple javascript block like this

  function callsCox(res) {
    alert(res);
   }

From my code behind :

 ....
 string res="COX23";
 string script = String.Format("callsCox({0})", res);
 this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Cox",script,true);

Am I missing anything? There aren't any exceptions or errors.

share|improve this question

76% accept rate
"this isn't working" is not a good description of the issue. What isn't working? What is happening that you don't expect to happen? What isn't? Errors? Exceptions? – Oded Jan 8 '12 at 13:17
Hi Oded, Have edited my question, javascript code cannot be called from codebehind method. There arent any errors or exceptions. – GJ SJ Jan 8 '12 at 13:21
first debug step should be to check the actual html coming from the server... does it contain a call to callsCox? – Pauli Østerø Jan 8 '12 at 13:33
feedback

2 Answers

up vote 2 down vote accepted

Page.ClientScript.RegisterStartupScript looks OK to me (might have missed something). Things to try

  1. Add apostrophes to the call - it's coming through as an object. Try as a string

    string script = String.Format("callsCox('{0}')", res);

  2. Is the string script Page.ClientScript.RegisterStartupScript being called after an update panel partial postback. That could effect it

  3. I have know functions not been found if they are in the same page. Try moving to an external js file. Don't asked me why this has resolved issues but it has a couple of times in the past for me.

  4. Just for debug purposes take the function out of the equation all together, Try to get the alert working like this. It will at least isolate the problem if it does work

    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Cox","alert('Does this work?')",true);

  5. View the source of the page. Is the function even written into the page (or alert from point 4). It should be. If you put a breakpoint on the this.Page.ClientScript.RegisterStartupScript method is it being hit? Seems like it might not be.

Apologies for not giving you a 'hey this is the solution' type of answer. I've had stuff like this in the past and I've found it a matter of stripping things down until the problem has been isolated. Someone else may be able to spot an immediate problem of course. Good luck.

share|improve this answer
Bucket, Tried following your steps described, strangely I cant even get the "alert", as you explained in step 4. – GJ SJ Jan 8 '12 at 13:35
@Samuel. Just replace the this.Page.ClientScript.RegisterStartupScript with the one on point 4. It should just embed the alert and the alert will come up. If that doesn't work then it can't be anything to do with the JS function. So it must be the register method at fault. I'll put an edit with another idea – Crab Bucket Jan 8 '12 at 13:43
Yes Buket, Now the alert is displayed.. only thing have changed is by replacing Cox with "script" and adding ';' to end the alert.Let me try passing variables to the Js function. – GJ SJ Jan 8 '12 at 13:45
1  
@Samuel Looks like you're getting there. It might have been the ';' – Crab Bucket Jan 8 '12 at 13:46
Finally, Thanks a lot Buket. It is very silly mistake that I have made,All I need is a ";" here String.Format("callsCox('{0}');", res);......Finally it went through.Thanks a lot for sharing your time your steps really helped me to drill through. – GJ SJ Jan 8 '12 at 13:52
feedback

This works for me:

public static void ShowAlert(Page page, String message)
{
    String Output;
    Output = String.Format("alert('{0}');",message);
    page.ClientScript.RegisterStartupScript(page.GetType(), "Key", Output, true);
}
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
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.