Wednesday, March 28, 2012

Calling a simple JS Function in the "Atlas way"

How could I, from an server-side control (like a Button), call an JS Function?
In the "old way", I used theRegisterClientSideScriptBlock to do this, but it does not work in the Atlas.

Thanks in Advance for the attention,
Fract4L

RegisterClientSideScriptBlock isn't working anymore for you with Atlas? Could you post a code sample showing the problem and describe what error you're getting if any?


I haven't tested this with Atlas but I assume it should work. This might even be some deprecated method of calling a JS funcion from a .NET control but I would always add the following to the page_load.

Button1.Attributes.Add("OnClick", "HelloWorld();")

Not sure again if this will work in Atlas, but it does add the js event function to the button itself.

JoeWeb


hello.

use the overload that has a boolean as its last parameter. this method will generate the correct script block in all cases (i think that you're script string has <script type='javascriopt'> on it, right? if you fo this, it won't work in atlas because only the scripts that have the type attribute set to text/javascript will be correctly loaded by the platform).

regarding the bt click handling on the client side, you can also use the new onclientclick property intesad of using the attributes collection.


Hello all,

TheRegisterClientScriptBlock was deprecated in favor of the classClientScript, that has all the funcionallities of the old method and some new ones.


Either way, thanks in advance for your fast responses and I hope that this question could help others.

Fract4L

Here is the code:

1protected void btnAccess_Click(object sender, EventArgs e)2{3// this works fine ;-)4StringBuilder sb =new StringBuilder();5sb.Append("<script type=\"text/javascript\">");6sb.Append("alert('test');");7sb.Append("</" +"script>");8ClientScript.RegisterClientScriptBlock(this.GetType(),"test", sb.ToString());9}

hello.

i'd remove the lines <script> and </script> and add ,true to the registerclientscriptblock method.

No comments:

Post a Comment