Search code examples
c#javascriptasp.netraphaelcode-behind

Calling Javascript (Raphael.js library) from C# codebehind in ASP.NET


I'm trying to call some function from my code behind in C#. I was searching how to do this and was able to call an alert to display when I wanted it to. However, I don't know how to call other things!

This is what I need to call from the code behind:

var paper = Raphael("paper1", 800, 800);
var Plan = paper.path("M100, 100 a 50, 50 0 0,1 50, 50 l -50 0 l 0, -50");
Plan.attr({ fill: "#FF6600" });

I've tried these on a plain HTML file but I'm not able to use it. I'm also using a master page and most of the examples I've found have been without master pages so I'm pretty lost on this.

Anyone can help?


Solution

  • Create a Javascript function in the .aspx page and then call the function from code behind like so:

    Function in html Code

    function dostuff()
    {
      // code here
    }
    
    C# code in code behind
     protected void callmethod()
        {
            StringBuilder oSB = new StringBuilder();
             Type cstype = this.GetType();
    
    
            try
            {
                oSB.Append("<script language=javaScript>");
                oSB.Append("dostuff();");
                oSB.Append("</script>");
                Page.ClientScript.RegisterClientScriptBlock(cstype, Guid.NewGuid().ToString(), oSB.ToString(), false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                oSB = null;
            }
        }