How to call C# function from PageSource javascript.

0
0

Hello Support.

I am using an aspNetPanel with the PageSource attribute set to an HTML document. In the HTML document I am using javascript to call a method in my Form class but the method doesn’t get called, it just stops the javascript executing (I can put an alert before and after the call and I don’t see the message from the alert call that is after the App call).

The javascript call looks like:

App.MyClassName.ShowMessage();

The C# ShowMessage() method has the [WebMethod] attribute. When I put the App.MyClassName.ShowMessage(); line into the aspNetPanel’s InitScript property the method gets called.

Can I make a method in my C# class be called from the PageSource code ?

Thank you

Andrew

  • You must to post comments
0
0

Hi Andrew,

To achieve your goal, you’ll have to move the script from the “PageSource” HTML file to the application’s Default.html file, or add it to the HtmlPanel’s InitScript property.

You can think of the HtmlPanel as a sandbox, even if you call the “this.App.Window1.HelloWorldFromHTMLSource()” method from firing the click event you’ll get “this.App” is not defined on the Console.

It’s a security measure, so you’ll have to define all the scripts from the beginning in Default.html.

You can check out these forum posts for more details:
https://wisej.com/support/question/firedataevent-from-innerhtml-of-htmlpanel
https://wisej.com/support/question/get-filled-html
https://wisej.com/support/question/topaz-wisej-integration

Or better yet, you can actually integrate your javascript library as a widget!
You can check out the documentation for more info through this link: Wisej.NET | Widget

I have also attached a modified version of your sample for you to try!

HTH,
Alaa

  • Andrew Pearce
    Hello Alaa. Thank you for your quick response. Following your suggestions I did get the HtmlPanel being able to call a javascript function in Default.html. Unfortunately I can’t manage to get the 3rd party javascript library to work in an HtmlPanel or iFramePanel, even if I include it in Default.html. It only seems to work in an aspNetPanel and, as you explained, an aspNetPanel is sandboxed and can’t call back to any C# code (even in Default.html – is that right ? (you said to define all scripts in Default.html but even doing that an aspNetPanel can’t find them)). So I am looking for some other solution to make the output of the javascript library available to my code, I will try making a call to some web services and save the value to the db. Thanks for your help.
  • You must to post comments
0
0

Hello Frank. Thank you for helping with this question. I have attached a short example project to show the problem I am having. When you run the project if you click on the ‘Call javascript in Initscript’ button it will call a javascript function which is defined in the InitScript attribute of the aspNetPanel and that javascript function will show an alert and then call a method in the Form class that will show a MessageBox. This all works.

But the next button ‘This is PageSource javascript – Call a C# method’ doesn’t work as I expect. It calls a javascript function which is defined in the page linked to the PageSource attribute of the aspNetPanel. The javascript function shows an alert (which we see) and then calls a method in the Form class – but the call doesn’t happen, the javascript fails silently. This is my problem – I would like to be able to call a C# method from javascript in the PageSource file. (I am using the PageSource file because in my real project I have to include a 3rd party javascript library which provides some functionality that returns a value and I need to get that value back to my C# code, the 3rd party javascript library works with a PageSource file so I thought that was the best way to go, if there is another way of doing this (not with an aspNetPanel, not in PageSource etc.) then I am open to any ideas).

Also in the example project there is an HTMLPanel which has some javascript in the html file that is in the HtmlSource attribute. I can’t get the button in that panel to call the javascript function – maybe this is a different problem ? If you click the ‘Call Method’ button it doesn’t seem to call the MakeCall javascript function (maybe this a problem with my limited knowledge of html/javascript).

Thanks for your help

Andrew

  • You must to post comments
0
0

Hi Andrew,

is the function declared as public static? Please also note the last paragraph in our docs about RemoteMethods

https://docs.wisej.com/docs/concepts/javascript-object-model#remote-methods

If it still fails after using the mechanism described there, please wrap up a small repro case that we can use to inspect that issue.

Thanks in advance.

Best regards
Frank

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.