By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,077 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,077 IT Pros & Developers. It's quick & easy.

Executing VB via JavaScript

P: n/a
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 (ValidatorCallout) that requires
client-side validation to work with a custom validator I added. This
is an example of some code that works:

<asp:CustomValidator ID="CV_PartNumberExists" runat="server"
OnServerValidate="PrimeNumberCheck"
ClientValidationFunction="CheckPrime"
ControlToValidate="PartNumberText" ErrorMessage="b><br />A Part
Number is required."></asp:CustomValidator>
<script language="JavaScript">
<!--
function CheckPrime(sender, args)
{
var iPrime = parseInt(args.Value);
var iSqrt = parseInt(Math.sqrt(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(sender, args)
{
var sPartnumber = String(args.Value);
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
Share this Question
Share on Google+
3 Replies


P: n/a
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("sPartNumber") Is Nothing) Or
(Request.Params("sPartNumber") = "") 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.Params("sPartNumber") Is Nothing Or
Request.Params("sPartNumber") = "") Then

or

If Not(Request.Params("sPartNumber") Is Nothing) And
Not(Request.Params("sPartNumber") = "") Then

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

This works, in the sense that if I open the page like
this :"PartExistCheck.aspx?sPartNumber=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(sender, args)
{
var async = new XMLHttpRequest();
async.open("POST", 'PartExistCheck.aspx', true);
async.setRequestHeader('Content-Type',
'application-x-www-form-urlencoded');
async.onreadystatechange = function()
{
if(async.readyState == 4)
{
if(async.responseText == 'true')
{
args.IsValid = false;
return;
}
else
{
args.IsValid = true;
}
}
}
async.send('sPartNumber=' + 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

P: n/a
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("sPartNumber") Is Nothing) Or
(Request.Params("sPartNumber") = "") 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.Params("sPartNumber") Is Nothing Or
Request.Params("sPartNumber") = "") Then

or

If Not(Request.Params("sPartNumber") Is Nothing) And
Not(Request.Params("sPartNumber") = "") Then

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


<snip>
This works, in the sense that if I open the page like
this :"PartExistCheck.aspx?sPartNumber=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(sender, args)
{
var async = new XMLHttpRequest();
async.open("POST", 'PartExistCheck.aspx', true);
async.setRequestHeader('Content-Type',
'application-x-www-form-urlencoded');
async.onreadystatechange = function()
{
if(async.readyState == 4)
{
if(async.responseText == 'true')
{
args.IsValid = false;
return;
}
else
{
args.IsValid = true;
}
}
}
async.send('sPartNumber=' + 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

P: n/a
In comp.lang.javascript message <11*********************@i38g2000prf.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.sqrt(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.demon.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.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 3 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.