Solved

Call a sub function using javascript

Posted on 2007-12-03
5
2,359 Views
Last Modified: 2008-02-01
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
Comment
Question by:mlg101
  • 3
5 Comments
 
LVL 8

Expert Comment

by:Chumad
ID: 20398638
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
 
LVL 8

Expert Comment

by:Chumad
ID: 20398644
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
 
LVL 28

Expert Comment

by:sybe
ID: 20398652
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
 
LVL 1

Author Comment

by:mlg101
ID: 20398722
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
 
LVL 8

Accepted Solution

by:
Chumad earned 500 total points
ID: 20398773
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp day pilot 3 18
Decoding Special HTML Characters using Javascript? 2 23
ASP.NET MVC -Add authentication 2 19
Select case on click 3 13
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now