Skip to content

How to run an external script from a C# script?

G

Asked Jan 30, 2009, 12:57 AM EST4 answers
Share
LinkedInX
Hello,

I have a script written in C# (.NET) and within this script I have an IF statement and when this IF statement is TRUE I want to execute the following external script that I placed just under the </head> of my page:

<script id='name' type='text/javascript' src='https://xx' integration='jscart-wizard'></script>

The external script is currently executed when I click on a button that passes some values to the external script

How can I do that?


Thank you in Advance

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    You can do this, I give you two senarios:

    Senario1:

    //Javascript

    function PrintSomeWords(var param)
    {
         alert(param);
    }

    //C sharp ASP.Net

    public void CallPrintSomeWords(var param)
    {
        Page.RegisterStartupScript("SriptID","<script language=JavaScript>CallPrintSomeWords('" + param.ToString() + "');</script>");
    }

    Senario 2:

    the same thing but using

    window.execScript("CallPrintSomeWords('" + param.ToString() + "')", "JScript");
    instead of

    Page.RegisterStartupScript("SriptID","<script language=JavaScript>CallPrintSomeWords('" + param.ToString() + "');</script>");
    0
  • 0
    Hi and thank you for the help..

    I'm kind of a newbie so forgive me if I don't get things straight away..

    so what I did now is I've put a

    (1) TOP of my page

    <script language="javascript">
    function PrintSomeWords(var param)
        {
         alert(param);
        }
    </script>


    (2) then in <script runat="server">
    public void CallPrintSomeWords(var param)
        {
    Page.RegisterStartupScript("SriptID","<script language=JavaScript>CallPrintSomeWords('" + param.ToString() + "');</script>");
        }

    (3) After my IF statement (when it is TRUE)
    I'm calling the CallPrintSomeWords();

    (4) Under </head>
    <script id='name' type='text/javascript' src='https://xx' integration='jscart-wizard'></script>


    Am I missing something?
    How exactly can I pass the values that my button would eitherwise do (these values are retrieved from my DB and passed on to the button using a Session Variable and having the value of the button written like this: value='<%Response.Write(Session["Selected_add_to_cart_name"]);%>'

    It also gives me a 'Newline in constant' error
    Code
    Page.RegisterStartupScript('SriptID',"<script language=JavaScript>CallPrintSomeWords('" + param.ToString() + "');</script>");

    Thanks again!
    0
  • Bechir Bejaoui
    0
    I know you have a liitle bit confusion try this very simple code separatly and I'm sure that you will understand the mechanism

    HTML / XML
    <html>
      <head>
        <script language="C#" runat="server">
          public void Page_Load(Object sender, EventArgs e)
          {
    
            if (!this.IsStartupScriptRegistered("Startup"))
            {
              // Form the script to be registered at client side.
              String scriptString = "<script language=JavaScript> function DoClick() {";
              scriptString += "showMessage2.innerHTML='<h4>Welcome to Microsoft .NET!</h4>'}";
              scriptString += "function Page_Load(){ showMessage1.innerHTML=";
              scriptString += "'<h4>RegisterStartupScript Example</h4>'}<";
              scriptString += "/";
              scriptString += "script>";
              this.RegisterStartupScript("Startup", scriptString);
            }
          }
         </script>
      </head>
      <body topmargin="20" leftmargin="10" onload="Page_Load()">
         <form id="myForm" runat="server">
            <span id="showMessage1"></span>
            <br>
            <input type="button" value="ClickMe" onclick="DoClick()">
            <br>
            <span id="showMessage2"></span>
         </form>
      </body>
    </html>
    0
  • 0
    Hello,

    I tried using your script but it didn't help me in terms of auto-submitting my button.

    The following code is the button that I currently have that passes some information onto an external shopping cart:
       




    And the following is the script that displays the external shopping cart on my page:




    Finally, This is how I tried to implement my button with your given script:

     public void Page_Load(Object sender, EventArgs e)   
       {  
            if (!this.IsStartupScriptRegistered("Startup"))    
               {         
               // Form the script to be registered at client side.         
               String scriptString = " function DoClick() {";
               scriptString += "showMessage2.innerHTML= '   
       '}";         
               scriptString += "function Page_Load(){ showMessage1.innerHTML=";         
               scriptString += "'
    RegisterStartupScript Example
    '}<";         
               scriptString += "/";         
               scriptString += "script>";         
               this.RegisterStartupScript("Startup", scriptString);       
               }     
        }


    But all this does is Print/Write my Shopping Cart button on the page once your given script's button is pressed.
    I however wish to execute my button once i.e. your given script's button is pressed/or when an IF argument is TRUE

    So I don't want my button to behave as a Click Button but instead I want to figure out how to extract that code from the Shopping Cart button and execute it myself from within my C#.

    Hope this makes sense

    Thanks again, I've been searching for this answer for long now
    0