Click here to Skip to main content
14,330,663 members
Rate this:
Please Sign up or sign in to vote.
I have been trying to figure this out for days.
I've read through many threads that mention calling javascript via code behind by using ClientScriptManager but all of them only appear to run the javascript function on page load or on submit, or button click.

Take this page for example:
Multiple Ways to Call Javascript Function from CodeBehind in ASP.Net[^]


Surely there is a way to not run the javascript on page load, but whenever the user wishes to call it from a C# or Vb function? However, I have yet to find the answer.

The issue appears to be that in order to call the aspx.cs method from a C# function, the aspx.cs method needs to be 'shared' otherwise if we are calling a instance of the class from the C# method it doesn't work. But then on the other hand, Page.ClientScript requires an object reference, so it's not possible to get it to work.

This issue is also explained in this thread:
ClientScript Issue | The ASP.NET Forums[^]

I would be really grateful if someone could show me how to do this.

What I have tried:

All the suggested answers from other threads, which run the code on page load, which is not what I am looking for.

Example:

Public Class home
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        pageTitle.Text = "Query Builder v" & System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()
        

        Dim Script As ClientScriptManager = Page.ClientScript

        If Not Script.IsClientScriptBlockRegistered(Me.[GetType](), "Alert") Then
            Script.RegisterClientScriptBlock(Me.[GetType](), "Alert", "<script type=text/javascript>alert('hi')</script>")
        End If

    End Sub

End Class
Posted
Updated 8-Oct-19 0:47am
v7

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You can't call javascript from your code behind. Think about the lifecycle of your code;

1 User requests a url
2 Your c# runs on the server and generates a stream of html to return to the client
3 The c# code finishes and the resulting html that was generated is sent to the client
4 The client browser interprets the html for visual display and interprets and runs any javascript

When your c# code is running there is nothing to interpret or execute javascript as the client browser has not received any output from your page. Your c# runs in its entirety on the server and then the javascript runs in its entirety on the client. You can't have them running at the same time as the web just doesn't work like that.

All RegisterClientScriptBlock is doing is injecting js into the html waiting to be sent to the browser so that it is executed when the browser receives it, which is step 4 which is after your c# has finished running.
   
Comments
Member 14476420 8-Oct-19 8:30am
   
Thank you for clarifying this. Much appreciated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100