Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. calling javascript from c#

calling javascript from c#

Scheduled Pinned Locked Moved ASP.NET
helpcsharpjavascripttoolsquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ekynox
    wrote on last edited by
    #1

    hi guys, I am stuck on the issue of being able to call a javascrip function from my c# code. This is my javascript: <SCRIPT LANGUAGE="JScript"> function showPosition() { var oElement = document.all.resultPanel; return oElement.offsetLeft; } </SCRIPT> The javascript itself works fine, the problem comes down to how do I call my javascript in c#. This was my attempt: private void CallJavascript() { string popupScript = "<SCRIPT LANGUAGE='JScript'>" + "function showPosition(){ var oElement = document.all.resultPanel; return oElement.offsetLeft;}</SCRIPT>"; Page.RegisterStartupScript("PopupScript", popupScript); Label1.Text = popupScript.ToString(); //this is wrong } Can someone please help me correct the CallJavascript() method. thanks

    J 1 Reply Last reply
    0
    • E ekynox

      hi guys, I am stuck on the issue of being able to call a javascrip function from my c# code. This is my javascript: <SCRIPT LANGUAGE="JScript"> function showPosition() { var oElement = document.all.resultPanel; return oElement.offsetLeft; } </SCRIPT> The javascript itself works fine, the problem comes down to how do I call my javascript in c#. This was my attempt: private void CallJavascript() { string popupScript = "<SCRIPT LANGUAGE='JScript'>" + "function showPosition(){ var oElement = document.all.resultPanel; return oElement.offsetLeft;}</SCRIPT>"; Page.RegisterStartupScript("PopupScript", popupScript); Label1.Text = popupScript.ToString(); //this is wrong } Can someone please help me correct the CallJavascript() method. thanks

      J Offline
      J Offline
      jagan123
      wrote on last edited by
      #2

      Page.ClientScript.RegisterStartupScript(me.GetType(), "dummyKey", "function name()", true);

      E 1 Reply Last reply
      0
      • J jagan123

        Page.ClientScript.RegisterStartupScript(me.GetType(), "dummyKey", "function name()", true);

        E Offline
        E Offline
        ekynox
        wrote on last edited by
        #3

        jagadeeshkumar2106 wrote:

        Page.ClientScript.RegisterStartupScript(me.GetType(), "dummyKey", "function name()", true);

        thanks for posting however i still dont get this ClientScript.RegisterStartupScript method. If my javascript is return a value how does this get handled in ClientScript.RegisterStartupScript ?

        J 1 Reply Last reply
        0
        • E ekynox

          jagadeeshkumar2106 wrote:

          Page.ClientScript.RegisterStartupScript(me.GetType(), "dummyKey", "function name()", true);

          thanks for posting however i still dont get this ClientScript.RegisterStartupScript method. If my javascript is return a value how does this get handled in ClientScript.RegisterStartupScript ?

          J Offline
          J Offline
          jagan123
          wrote on last edited by
          #4

          i think its better to keep in a hidden fiel value

          E 1 Reply Last reply
          0
          • J jagan123

            i think its better to keep in a hidden fiel value

            E Offline
            E Offline
            ekynox
            wrote on last edited by
            #5

            A hidden field can be something similar to the input element as suggested in the example below ? <script runat="server"> public void Page_Load(Object sender, EventArgs e) { // Define the name and type of the client scripts on the page. String csname2 = "ButtonClickScript"; Type cstype = this.GetType(); // Get a ClientScriptManager reference from the Page class. ClientScriptManager cs = Page.ClientScript; // Check to see if the client script is already registered. if (!cs.IsClientScriptBlockRegistered(cstype, csname2)) { StringBuilder cstext2 = new StringBuilder(); cstext2.Append("<script type=\"text/javascript\"> function DoClick() {"); cstext2.Append("var oElement = document.all.Panel1; Form1.Message.value= oElement.offsetLeft;} </"); cstext2.Append("script>"); cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false); } } </script> <html > <head> <title>ClientScriptManager Example</title> </head> <body> <form id="Form1" runat="server"> <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" /> <asp:Panel ID="Panel1" runat="server" style="position:absolute;top: 375px;left: 628px;height: 422px;width: 513px; border-color:Red;border-width:thick;border-style:solid"></asp:Panel> </form> </body> </html> edit: Example is based from MSDN article http://msdn2.microsoft.com/en-us/library/z9h4dk8y.aspx[[^](http://msdn2.microsoft.com<br mode= "New Window")] edit: sorry I worked out what a hidden field is thanks to google http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlinputhidden(VS.71).aspx[^]

            modified on Thursday, December 06, 2007 8:33:21 AM

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups