Solved

Call a sub function using javascript

Posted on 2007-12-03
5
2,351 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

707 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

19 Experts available now in Live!

Get 1:1 Help Now