Solved

Call a sub function using javascript

Posted on 2007-12-03
5
2,367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Suggested Courses

630 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