Trying to emit Javascript using scriptmanager.registerclientscriptblock in a ASP.NET page

1.3k Views Asked by At

I have an ASP.net page that has a scriptmanager and an update panel on it. I need to call a javscript function from the codebehind on an server side event. i have tried using three methods to spit out javascript.

1) Clientscript.registerclientscriptblock()

2) Scriptmanager.registerClientscriptblock()

3) and also response.write()

None of the above seem to write to the page at all. what could be reason that all of the above methosd are failing?

what would be better method to call javascript now? my failed usage :

1) ClientScript.RegisterClientScriptBlock(Me.GetType(), "firstscript", "alert('test1')")

2) ScriptManager.RegisterClientScriptBlock(Me.Page, Me.GetType(), "testscript", "alert('test2')", True)

3) Response.Write("<script language='Javascript' type='text/javascript'>alert('test 3')</script>")

I also used Response.Write("-test-") to see if it actaully writes to the page. but it doesn't. Any insight would be much helpful.

2

There are 2 best solutions below

0
Protector one On

You could try RegisterStartupScript (here's a nice question about the differences between RegisterStartupScript and RegisterClientScriptBlock), but since your Response.Write doesn't work, something appears to be very wrong.
Are you sure you're testing your code correctly?

1
Shroeder On

In the Code Behind.

Create a ReadOnly Property on the page with your UpdatePanel.

Public Readonly Property xxx As UpdatePanel
Get
   Return upd1<--- this is your updatepanel object
end get
End Property

ScriptManager.RegisterStartupScript(xxx,me.gettype(),"Name", "Actual Javascript Here", true)

This should help you out