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?

share|improve this question

75% accept rate
How do you get the alert() call to work then? You can't really "call" anything on the browser, you can only output a <script> block as part of your page that the browser will process. Once your page is done displaying, you can't do that anymore. (AJAX notwithstanding.) – millimoose May 23 '12 at 13:43
I'm doing this in the code behind, when a dropdown selected item changes, ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "<script type='text/javascript' >alert('Error!!!');</script>", false); – user1412716 May 23 '12 at 13:44
Calling the block of code in your question should work the same way really. What happens when you try it? Does the JS code get output to the browser at all? Are there any errors in the browser's JS console? – millimoose May 23 '12 at 13:46
but how do I write all the lines in the place where it says error? I'm kind of a newbie at this, I saw some tutorials but still not an expert :/ – user1412716 May 23 '12 at 14:04
Have you tried just copy-pasting the JS code in place of the alert(), then trying to fix any errors that come up? – millimoose May 23 '12 at 14:21
show 1 more comment
feedback

2 Answers

up vote 0 down vote accepted

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;
        }
    }
share|improve this answer
I tried this and the alert worked. This is what I have now in my aspx file <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> <script type="text/javascript" src="raphael.js"> function draw() { 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"); } </script> <title></title> </asp:Content> – user1412716 May 23 '12 at 13:59
In the code behind: StringBuilder oSB = new StringBuilder(); Type cstype = this.GetType(); oSB.Append("<script language=javaScript> src=raphael.js"); oSB.Append("draw();"); oSB.Append("</script>"); Page.ClientScript.RegisterClientScriptBlock(cstype, Guid.NewGuid().ToString(), oSB.ToString(), false); But it's not working! Any ideas? – user1412716 May 23 '12 at 14:02
<script language=javaScript> src=raphael.js" ?? Why are you adding the reference to the JS File twice? StringBuilder oSB = new StringBuilder(); Type cstype = this.GetType(); oSB.Append("<script language='javaScript'>"); oSB.Append("draw();"); oSB.Append("</script>"); Page.ClientScript.RegisterClientScriptBlock(cstype,Guid.NewGuid().ToString(), oSB.ToString(), false); This should work. – Laird Streak May 23 '12 at 19:50
Make sure the script loads at the top of the .html / .aspx page this is not best practice but I have seen issues when the page is not completely loaded. – Laird Streak May 23 '12 at 19:54
feedback

Javascript can only be called on the client side. If you absolutely need to call it from your serverside, you can use a asp:HiddenField's value as a flag for when you need the javascript code executed upon returning, and then run the needed javascript if the requirements are met.

But its not a nice solution, you should probably try to separate the server and the client.

Hope this helps, in any case!

share|improve this answer
So what would you suggest to be able to do this? I just need to print some shapes in the screen depending on the status of some items that are taken from a database. I had done that with CSS but stupid IE didn't work with it. – user1412716 May 23 '12 at 13:46
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.