Call JavaScript Function from C# .aspx .net app

22705
15
Jump to solution
05-01-2020 07:13 PM
jaykapalczynski
Frequent Contributor

I have an aspx C# .net app that was written apart from me

I added a map component outside the FORM tags.

I am trying to find a trigger that AFTER the page loads where I can fire off a JavaScript Function....I am stuck and have tried a hundred different Google searches...

Is there an after Page Load that I can reference from the aspx page?

OR

How can I call a Separate JS file Function that resides in the Solution from C# .net after some sort of trigger from the .net page

PLEASE HELP

0 Kudos
15 Replies
by Anonymous User
Not applicable

Hi jay kapalczynski‌,

It seems you got complicated requirement.

For your case of gvTrappers_SelectedIndexChanged server side code, the necessary data from your client side javascript is session["NWTL_ID_String"] value from server side.

I might save hat session value into hiddenfield control into your asp.net form.

And that for selectedindexchanged event, I will just make a call from client side code, and retrieve the hiddenfield value.

Sound good?

I normally trying to avoid calling or injecting js snippet from server side like Page.ClientScript.RegisterClientScriptBlock method.

0 Kudos
jaykapalczynski
Frequent Contributor

Not exactly....

I can get the session variable and place it in a hidden control no issues

I can retrieve the controls value no issues but thats not my problem

My issue is that I need to call a function in an external JS page from C#, not a page load trigger, not a button trigger, not a dom ready trigger. 

After the selectedindexchanged event fires I need to call a JS function in an external JS page and pass that value to a JS function that resides in an external JS page

Below is the only way I know how to run JS code from within C#

BUT I need a way to call a JS function that is NOT in the .cs page

ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript2", "alert('TestString');", true);

Can I use the below to call  a function in the JS Page itself?

ScriptManager.RegisterStartupScript(this, GetType(), "index", "javascript: myFunction();", true);

0 Kudos
by Anonymous User
Not applicable

Hi jay kapalczynski‌,

From js page, either aspx or html or js files, you don't need to use ScriptManager if you want to call myFunction() js.

But in aspx file, if you really need that scriptmanager you can use inline script. 

And you can even mix with C# function call which is written in cs file or can be written csharp code in aspx.

Below is for your reference.

https://support.microsoft.com/en-au/help/976112/introduction-to-asp-net-inline-expressions-in-the-ne... 

0 Kudos
jaykapalczynski
Frequent Contributor

  Hi  Than Aung‌   first off thank you for your continued support with this...thinking that I am getting close to an end and will be defeated....while understanding the issues related to server and client side programming....

I totally agree with the url you posted.  BUT again this is when the JS is in the HTML page.  That is NOT my case

I am referring to the situation when the JS is an external page.  I do not see anywhere n the URL link you provided where they call and external JS page....all the JS is in the aspx page

Successful but cannot be used:

1.  As stated I can use the ScriptManager and convert all my JS code to an escaped string and run it through there...

2.  As stated I can call the external JS code from a Button in xxxx.aspx page

3.  As stated I can call the external JS page via page load complete and the dojo ready function in the JS page

            But this cannot be used in my case because the way the designer made the aspx page

4.  As you stated I can call JS code BUT that code has to be in <script> tags in the xxxx.aspx page

Although I am trying to call a JS function in a seperate JS page from the xxxx.aspx.cs page.  

I am assuming that this cannot be done as no solution has presented itself.

Trying to call the JS function from within the xxxx.aspx.cs page ... NO GO

        in the xxx.aspx page

<script type="text/javascript" lang="javascript" src="js/index.js">

        in the xxx.aspx.cs page

protected void gvTrappers_SelectedIndexChanged(object sender, EventArgs e)
{
    // CALL EXTERNAL JS FUNCTION that is in js/index.js
    myFunction();  

}‍‍‍‍‍‍

I tried this at the end of the xxxx.aspx page

Trying to load my js page and then call a function in it....NO GO

    <script type="text/javascript" lang="javascript" src="js/index.js">
        myFunction(); 
    </script>‍‍‍
0 Kudos
jaykapalczynski
Frequent Contributor

Than Aung‌  I cannot thank you enough for all your thoughts and support....I had a difficult case here as I was restrained by the developer that created this website and think that through your thoughts and steering I was able to come up with a solution....OF course this could not have happened without your guidance.....I cannot thank you enough....peace.

I think I may have found a solution that works for me....

My main issue was that I could not rely on the page load complete event to reference my javascript code because of the way the web site was designed.  But the dojo/ready was always an option if I could control when I called it....

After some testing I think this is the solution....right now I am simply calling this on the page load complete. which as stated before was an issue....

I should be able to control when this is called....whether it be on a text box focus, combo box change etc...etc

I NOW have absolute control when I load the JS page using the dojo/ready function to start my code.

protected void Page_Load(object sender, EventArgs e)
{
   Page.LoadComplete += new EventHandler(Page_LoadComplete);
}


void Page_LoadComplete(object sender, EventArgs e)
{
    string myScript2 = "\n<script type=\"text/javascript\" language=\"Javascript\" src=\"js/index.js\">\n";
    myScript2 += "\n\n </script>";
    Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript2, false);
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I should now be able to do this like I was trying to do !!!!!!  Still relaying on when the script loads

protected void gvNew_SelectedIndexChanged(object sender, EventArgs e)
{
   string myScript2 = "\n<script type=\"text/javascript\" language=\"Javascript\" src=\"js/index.js\">\n";
   myScript2 += "\n\n </script>";
   Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript2, false);
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
by Anonymous User
Not applicable

No worries jay kapalczynski‌, 

That's good idea by registering startupscript .

0 Kudos