473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Executing VB via JavaScript

Let me start by saying that I am a complete idiot when it comes to
JS. However, I need help with something that apparently can only be
done this way.

I am using an ASP.NET AJAX control (ValidatorCallo ut) that requires
client-side validation to work with a custom validator I added. This
is an example of some code that works:

<asp:CustomVali dator ID="CV_PartNumb erExists" runat="server"
OnServerValidat e="PrimeNumberC heck"
ClientValidatio nFunction="Chec kPrime"
ControlToValida te="PartNumberT ext" ErrorMessage="b ><br />A Part
Number is required."></asp:CustomValid ator>
<script language="JavaS cript">
<!--
function CheckPrime(send er, args)
{
var iPrime = parseInt(args.V alue);
var iSqrt = parseInt(Math.s qrt(iPrime));

for (var iLoop=2; iLoop<=iSqrt; iLoop++)
if (iPrime % iLoop == 0)
{
args.IsValid = false;
return;
}

args.IsValid = true;
}
This is code I borrowed from another site to test this method - and it
works. My problem is that I need the JS to execute a VB function in
my project and I don't know how to do that. I want to do something
like:

function CheckValue(send er, args)
{
var sPartnumber = String(args.Val ue);
if FindExistingPN( sPartNumber)
{
args.IsValid = false;
return;
}
args.IsValid = true;
}

....where FindExistingPN is a funciton in my VB class. I have seen
some other posts about this, but none of them really gave me any
sample code that I could run. As I mentioned earlier, my JS skills
are lacking, so I am unable to create this myself.

Anyway, I would greatly appreciate any suggestions or sample code.
Thank you!

Aug 2 '07 #1
3 2119
Kirk wrote:
>
OK, I am once again banging my head against the wall. Here is what I
have so far.

I created a page that uses the passed parameter to determine what to
put in the response. This looks like:

'Get the passed parameter from the page
If Not (Request.Params ("sPartNumbe r") Is Nothing) Or
(Request.Params ("sPartNumbe r") = "") Then
This isn't directly related to your JS question (and I haven't
programmed in VB for over a decade, so I could be wrong), but it seems
to me that this ought to be:

If Not(Request.Par ams("sPartNumbe r") Is Nothing Or
Request.Params( "sPartNumbe r") = "") Then

or

If Not(Request.Par ams("sPartNumbe r") Is Nothing) And
Not(Request.Par ams("sPartNumbe r") = "") Then

Otherwise your logic will always enter the If-block when
Request.Params( "sPartNumbe r") = "" (which is probably the opposite of
what you want).
<snip>

This works, in the sense that if I open the page like
this :"PartExistChec k.aspx?sPartNum ber=230-001", it outputs True or
False based on the value passed.

Your code did just as you promised, but I am still mucking something
up. When the validation runs, I can set a breakpoint in the VB code
and see that it is executing (yay!). However, the value is not being
passed - it always comes back as [nothing]. Am I looking at the wrong
parameter?

function CheckPart(sende r, args)
{
var async = new XMLHttpRequest( );
async.open("POS T", 'PartExistCheck .aspx', true);
async.setReques tHeader('Conten t-Type',
'application-x-www-form-urlencoded');
async.onreadyst atechange = function()
{
if(async.readyS tate == 4)
{
if(async.respon seText == 'true')
{
args.IsValid = false;
return;
}
else
{
args.IsValid = true;
}
}
}
async.send('sPa rtNumber=' + escape('230-001'));
}

<snip>

The thing you are missing is the principle of asynchronous operation.
By the time the response comes back and the anonymous function() is
executed, you are no longer in the CheckPart function, in terms of
execution flow. That function has already returned (nothing, in this
case) and the program has moved on.

Here is the sequence of events in this case:

1. CheckPart is called.

2. CheckPart sends off HTTP request.

3. CheckPart returns nothing. "args" reference parameter is untouched
at this point.

4. Whatever function called CheckPart sees that the call did nothing.
It becomes distraught and goes off to the pub.

5. An unspecified amount of time passes - probably a few hundred
miliseconds.

6. Anonymous function() is called. outer if-block is entered.

7. args.IsValid is set to either true or false.

8. Anonymous function() returns nothing. "args" is now altered in the
desired sense, but the function that called CheckPart to begin with is
already off at the pub having a drink and can't do anything about it.
Unfortunately, you have just touched upon one of the most confusing
aspects of AJAX (asynchronous operation) and one of the most confusing
aspects of the javascript language (scoping) at the same time!

The key point here is that you simply must not look at the inner
function() as something that will be executed immediately*. It will be
executed (hopefully) at some point in the future, so instead of
monkeying with local data, which will be passed back to another function
that does some voodoo, it should use the information it has to do the
voodoo itself. I don't know what your code does after CheckPart
returns, so I can't really help you with that part. But chances are you
will need a pretty major restructuring of your javascript, consisting
of taking everything that CheckPart's caller was supposed to do after
CheckPart returned, and either doing it in your anonymous function(), or
putting it in a separate function that gets called by the anonymous
function.

If you need an example of converting a synchronous call to an
asynchronous one, I'll post one (but first you should take a crack at it
and see what you can do).

Jeremy

*It is actually *possible* to make your request operate in a synchronous
fashion, which would make your code here work. HOWEVER, this is
generally accepted to be a Real Bad Idea and a cardinal programming sin.
I'll tell you how to do it, but you shouldn't. Replace the last
parameter to async.open - change true to false. Now your request will
block until it is complete. The problem is, this could potentially be
forever (if the server dies, for example) and it will freeze up the browser.


Aug 2 '07 #2
On Aug 2, 6:32 pm, Jeremy <jer...@pinacol .comwrote:
Kirk wrote:
OK, I am once again banging my head against the wall. Here is what I
have so far.
I created a page that uses the passed parameter to determine what to
put in the response. This looks like:
'Get the passed parameter from the page
If Not (Request.Params ("sPartNumbe r") Is Nothing) Or
(Request.Params ("sPartNumbe r") = "") Then

This isn't directly related to your JS question (and I haven't
programmed in VB for over a decade, so I could be wrong), but it seems
to me that this ought to be:

If Not(Request.Par ams("sPartNumbe r") Is Nothing Or
Request.Params( "sPartNumbe r") = "") Then

or

If Not(Request.Par ams("sPartNumbe r") Is Nothing) And
Not(Request.Par ams("sPartNumbe r") = "") Then

Otherwise your logic will always enter the If-block when
Request.Params( "sPartNumbe r") = "" (which is probably the opposite of
what you want).


<snip>
This works, in the sense that if I open the page like
this :"PartExistChec k.aspx?sPartNum ber=230-001", it outputs True or
False based on the value passed.
Your code did just as you promised, but I am still mucking something
up. When the validation runs, I can set a breakpoint in the VB code
and see that it is executing (yay!). However, the value is not being
passed - it always comes back as [nothing]. Am I looking at the wrong
parameter?
function CheckPart(sende r, args)
{
var async = new XMLHttpRequest( );
async.open("POS T", 'PartExistCheck .aspx', true);
async.setReques tHeader('Conten t-Type',
'application-x-www-form-urlencoded');
async.onreadyst atechange = function()
{
if(async.readyS tate == 4)
{
if(async.respon seText == 'true')
{
args.IsValid = false;
return;
}
else
{
args.IsValid = true;
}
}
}
async.send('sPa rtNumber=' + escape('230-001'));
}
<snip>

The thing you are missing is the principle of asynchronous operation.
By the time the response comes back and the anonymous function() is
executed, you are no longer in the CheckPart function, in terms of
execution flow. That function has already returned (nothing, in this
case) and the program has moved on.

Here is the sequence of events in this case:

1. CheckPart is called.

2. CheckPart sends off HTTP request.

3. CheckPart returns nothing. "args" reference parameter is untouched
at this point.

4. Whatever function called CheckPart sees that the call did nothing.
It becomes distraught and goes off to the pub.

5. An unspecified amount of time passes - probably a few hundred
miliseconds.

6. Anonymous function() is called. outer if-block is entered.

7. args.IsValid is set to either true or false.

8. Anonymous function() returns nothing. "args" is now altered in the
desired sense, but the function that called CheckPart to begin with is
already off at the pub having a drink and can't do anything about it.

Unfortunately, you have just touched upon one of the most confusing
aspects of AJAX (asynchronous operation) and one of the most confusing
aspects of the javascript language (scoping) at the same time!

The key point here is that you simply must not look at the inner
function() as something that will be executed immediately*. It will be
executed (hopefully) at some point in the future, so instead of
monkeying with local data, which will be passed back to another function
that does some voodoo, it should use the information it has to do the
voodoo itself. I don't know what your code does after CheckPart
returns, so I can't really help you with that part. But chances are you
will need a pretty major restructuring of your javascript, consisting
of taking everything that CheckPart's caller was supposed to do after
CheckPart returned, and either doing it in your anonymous function(), or
putting it in a separate function that gets called by the anonymous
function.

If you need an example of converting a synchronous call to an
asynchronous one, I'll post one (but first you should take a crack at it
and see what you can do).

Jeremy

*It is actually *possible* to make your request operate in a synchronous
fashion, which would make your code here work. HOWEVER, this is
generally accepted to be a Real Bad Idea and a cardinal programming sin.
I'll tell you how to do it, but you shouldn't. Replace the last
parameter to async.open - change true to false. Now your request will
block until it is complete. The problem is, this could potentially be
forever (if the server dies, for example) and it will freeze up the browser.- Hide quoted text -

- Show quoted text -
Jeremy,

Thanks for the detailed explanation. You are correct - I really need
some more education on these methods before I get this deep into these
issues. I appreciate your warning about doing things the wrong way
just to get things working. I will research the techniques you
described further (you have given me some good buzz words to Google).

Thank you again for all of your help!

Aug 3 '07 #3
In comp.lang.javas cript message <11************ *********@i38g2 000prf.goo
glegroups.com>, Thu, 2 Aug 2007 12:24:44, Kirk <lo****@hotmail .com>
posted:
>Let me start by saying that I am a complete idiot when it comes to
JS.
...
...
var iSqrt = parseInt(Math.s qrt(iPrime));

The second quote above proves the correctness of the first one.

IMHO, your fundamental problem is that you are trying to run before you
can walk.

You need to take the time to understand the fundamentals of the language
before attempting anything more complex; the above shows that you have
not yet understood Javascript variable types.

When posting, please do not let your posting agent line-wrap the code;
manually wrap at about 72 characters, re-testing before you post. If
you will be asking here often, write within 72 characters per line.

In your primality checking, there's no need to test any even divisor
other than two; try two; then start at three & step in twos. Consider
the Sieve of Eratosthenes.

IMHO, if iPrime cannot be too big and/or if repeated tests are to be
done, it could be worth generating a list of the smaller primes and
test-dividing only by those. The largest number that can be held
exactly (in the usual manner) is 2^53, so you only need, at most, primes
up to 94906265. If iPrime <= 10^12, you only need the 78498 primes
below 10^6, which can certainly be handled (I just did).

<URL:http://www.merlyn.demo n.co.uk/js-misc1.htm>, upgraded to count.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2466
by: alanstew | last post by:
With the body tag calling out 'window onload', a function with a 'window.open' fails at the 'window.open' line. If I cut out the body tag, the function executes as normal. At first I thought it was the entire function failing, but I tested with alerts and found that it was only the 'window.open' that fails to execute. The function is being called by a link, and I suspected some problem with the body alink/vlink but after cutting that out...
20
1737
by: JulioHM | last post by:
Hello, Not sure if this is the right discussion group to post this, but here it goes. For some god-forsaken reason (which I can't find out either) MSIE stopped executing any JavaScripts. In any page which contains JavaScript code, that code won't be executed. There are no error messages, no warnings, nothing. It simply does'nt work. I'm pretty sure this could be caused by some stupid installation of another program
3
2458
by: Mike | last post by:
Hi, I am trying to resize a HTML table through Javascript. When the user control loads the first time, the table is resized, but then it doesn't anymore. I am using the following code in the Load event of the web user control: if ( ) this.Page.RegisterStartupScript("CallBothGrids", "<script language=javascript> { showBothGrids(); }</script>"); else
5
2486
by: reycri | last post by:
Hi, I need to be able to do this: var func = new Function("var me = <selfRef>; alert(me.params);"); func.params = "This is a test parameter"; window.setTimeout(func, 500); Basically, I need to add properties to a function object and access them within the function when it is executing. Therefore, I need to be
0
1362
by: jesper_lofgren | last post by:
Hello, I have a asp:updatepanel where i have a javascript that should run when a special event accour in the code. I use registerclientscriptblock method to add the javascript. Everything works well if i take away the updatepanel, but when i use updatepanel the javascript is ignored, not executing. Anyone have some idea ?
15
7030
by: rage3324 | last post by:
I am posting html onto my main page between div tags using xmlhttprequest and innerhtml. The html I am posting has javascript inside which I am executing using the eval() function. However, the problem I face is that the javascript uses innerhtml as well and will not work for some reason. I have tested the javascript and it is definitely executing, it just will not change the innerhtml. To be more clear, the innerhtml is trying to access...
2
1934
by: Mic | last post by:
Hi, How can I hide a button before executing a javascript function and make it visible again after execution of the javascript function? What I need to do is: VB Page_Load: 1) Hide Button1: Button1.Visible = False 2) Execute JavaScript (I'm using
7
2226
by: robin1983 | last post by:
Hi, good morning everyone, i have a file called attendence.php The problem is that some part of code is executing properly and half of the code is not and i dont get any warning or error message. For more information i m giving the whole code below. please give me the solution, the code is executing upto line no 95 (echo $halfday;) and after this line not a single code is executing. i am not able to get solution. So plaease help me in solving...
3
1932
by: leehanson | last post by:
I have a timer function that displays to the user the current number of seconds left for the current question. It all works fine, however when the timer is ticking down, and the user starts to drag the window, it stops. It seems to entirely stop javascript from executing. This issue is not seen in IE. Can anyone explain why Firefox stops executing JS when the window is moved/mouse is down over the blue top of the firefox window. I pray...
0
1566
Frinavale
by: Frinavale | last post by:
I have a peculiar problem... Background: I have a function that I don't want the user to execute more than once while they are waiting for it to process; therefore, I disable all of the controls on the page via some JavaScript before the request is sent. This function takes some time to execute because it has to communicate with hardware that is rather slow. This slowness combined with a little bit of lag sometimes results in a ...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.