• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2376
  • Last Modified:

Call a sub function using javascript

I have a hyperlink with destination url="javascript:Process()". Then on my aspx page, I have a sub function in the script section:

Sub Process()
code here...
End Sub

I also have a javascript in the head section, which i would like to initiate the "Sub Process()" When the hyperlink is clicked. The javascrpt I have so far is this:

<SCRIPT language=JavaScript type=text/javascript>
            function Process(){
                <% Process() %>
                }
</script>

When the hyperlink is clicked I need it to do the Process() sub. I cannot use a linkbutton, or else I would have already done that. Thank you.
0
mlg101
Asked:
mlg101
  • 3
1 Solution
 
ChumadCommented:
Is this supposed to be VB.NET code?

Sub Process()
code here...
End Sub

If so, that's server code only - javaScript wouldn't be able to run it since it runs only on the client.  What you want isn't possible... unless I'm totally missing something here :)
0
 
ChumadCommented:
As a side note, you *could* get javaScript to execute something on the server but it'll require some re-working as well as AJAX.
0
 
sybeCommented:
Hmm, so on the client you have code running. And then you wan to call a function that is located on the server.

There is only one way to do that: make the client do a (http-) request to the server. Pass the parameters in the request, and send the result back to the client. The simplest way to do it is to submit a form, and refresh the whole page. But it is also possible to to only refresh the page partly. That is what AJAX does.

There are many ways to apply AJAX functionality. The all involve the xmlhttprequest object. If you do some searching on that, you will find plenty of examples. But AJAX isn't very easy. There are downloadable kits that which will help you.

Anyway, are you sure that there is no other solution? Why can't you run the function on the client?



0
 
mlg101Author Commented:
What I'm trying to do is call the process sub (yes is VB), from a hyperlink control. The hyperlink does not have an onclick property, so I cant use that.

The reason is that I'm using ASP.NET menu controls, populated by a sitemap, so I have to specify the url to link to in the sitemap, (eg., <sitemapnode url="", title="", etc.). There is no way to put a linkbutton in the sitemap, to get it on the menu control. Does that make more sense as to waht I'm trying to do?
0
 
ChumadCommented:
Can you reference the page again and pass in the instructions then in page_load check for them and run the process? For example:

Have the URL point back to yourpage (assume it's yourpage.aspx):

url="http://yourpage.aspx?process=1"

Then in page_load, do a check for process=1 on the url:

if Request.Params("process") <> Null then
   Process();
End

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now