code_junkie

How can I call a javascript function from inside a method?

Nov 11th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. public class bgchange : IMapServerDropDownBoxAction
  2. {
  3. void IServerAction.ServerAction(ToolbarItemInfo info)
  4. {
  5. Some code...
  6.  
  7. [WebMethod]
  8. public static void DoSome()
  9. {
  10.  
  11. }
  12.  
  13. public class bgchange : IMapServerDropDownBoxAction
  14. {
  15.  
  16. void IServerAction.ServerAction(ToolbarItemInfo info)
  17. {
  18. ...my C# code to perform on dropdown selection...
  19. //now I want to trigger some javascript...
  20. // Define the name and type of the client scripts on the page.
  21. String csname1 = "PopupScript";
  22. Type cstype = this.GetType();
  23.  
  24. // Get a ClientScriptManager reference from the Page class.
  25. ClientScriptManager cs = Page.ClientScript;
  26.  
  27. // Check to see if the startup script is already registered.
  28. if (!cs.IsStartupScriptRegistered(cstype, csname1))
  29. {
  30. String cstext1 = "alert('Hello World');";
  31. cs.RegisterStartupScript(cstype, csname1, cstext1, true);
  32. }
  33. }
  34.  
  35. <asp:ScriptManager ID="scriptManager1"
  36. runat="server" EnablePageMethods="true" />
  37.  
  38. Page.ClientScript.RegisterStartupScript(
  39. this.GetType(),
  40. "callDoSome",
  41. "PageMethods.DoSome(Callback_Function, null)",
  42. true);
  43.  
  44. <script language="javascript" type="text/javascript">
  45. function Callback_Function(result, context) {
  46. alert('WebMethod was called');
  47. }
  48. </script>
  49.  
  50. public void ServerAction(ToolbarItemInfo info) {
  51. string jsfunction = "alert('Hello');";
  52. Map mapctrl = (Map)info.BuddyControls[0];
  53. CallbackResult cr = new CallbackResult(null, null, "javascript", jsfunction);
  54. mapctrl.CallbackResults.Add(cr);
  55. }
  56.  
  57. string script = "<script language='javascript'>n";
  58. script += "javascriptFunctionHere();n";
  59. script += "</script>";
  60.  
  61. Page.RegisterStartupScript("ForceClientToCallServerMethod", script);
  62.  
  63. public partial class _Default : System.Web.UI.Page
  64. {
  65. protected void Page_Load(object sender, EventArgs e)
  66. {
  67. Page.ClientScript.RegisterStartupScript(
  68. this.GetType(),
  69. "helloworldpopup",
  70. "alert('hello world');",
  71. true);
  72. }
  73. }
Add Comment
Please, Sign In to add comment