473,382 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 (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
3 2096
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
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...
20
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...
3
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...
5
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...
0
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...
15
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...
2
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...
7
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...
3
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...
0
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.