Results 1 to 26 of 26

Thread: [RESOLVED] Javascript - pass variable to VB.net(2003)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Resolved [RESOLVED] Javascript - pass variable to VB.net(2003)

    Hi all

    I can pass a variable top javascript thus:-
    Code:
    {
            
                 var MapProvider = '<%= session("MapProvider") %>';
    BUT

    How can I get Javascript to pass a variable back?

    regards

    Barry

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Javascript - pass variable to VB.net(2003)

    You pass data to web applications through either the query string or as part of an HTTP post request within the body. As Javascript is only executed after the page has been sent, the only sending a value back is to make another request to the server containing the data.

    This can be achieved using a hidden frame or iFrame and setting form values within the page and submitting it. A better way to do it however is through the use of the XMLHTTPRequest object, available in both Firefox and Javascript and in Opera as far as I am aware. This technology more commonly known as AJAX, is used to update data on the page without the user having to make a request by submitting a form or clicking a link.

    Before you consider using this, what is it you are trying to do?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: Javascript - pass variable to VB.net(2003)

    So far:- I've created a text field that both VB AND Javascript can ACCESS (and this has been my problem.

    Code:
    Response.Write("<INPUT style=""WIDTH: 24px; HEIGHT: 22px"" readonly  type=""text"" maxLength=""2"" size=""1""  name=""txtZoom""    value = " & Session("MapZoom") & ">")
    The Javascript places a zoom level into it OK and it works for this requirement.

    BUT it is Messy - surely there is a better way??????.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Javascript - pass variable to VB.net(2003)

    Is the value of the variable likely to change? If not then there is no need to insert its value into the page.

    There are many better ways of doing this. Don't use classic ASP for a start As i said in my previous post, if you need to fetch and set variable values on the server, you should look into AJAX. But I can't really see what you are using the Javascript for.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: Javascript - pass variable to VB.net(2003)

    AJAX - not an option at $399.00!!!!!

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Javascript - pass variable to VB.net(2003)

    What are you using the Javascript for? Using response.write to produce HTML is bad. You should use the same style you used in your first post.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: Javascript - pass variable to VB.net(2003)

    I tried:-

    Code:
     var MapProvider = ...........; 
    <%= session("MapProvider") %> = MapProvider ;
    It didn't work.

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Javascript - pass variable to VB.net(2003)

    You are not understanding the way in which ASP works. Javascript has nothing to do with ASP. You cannot assign a value to a server side variable from a client side script (not in the way you are trying to do it).

    Treat each as a separate program running on a separate PC, because that is in effect what they are. In order to communicate, you either need the user to initiate a request by clicking a link or submitting a form or have the Javascript initiate a request independently (AJAX).
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: Javascript - pass variable to VB.net(2003)

    OK - but I'm n ow even more confused

    Code:
    var MapProvider = '<%= session("MapProvider") %>';
    WORKS

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Javascript - pass variable to VB.net(2003)

    The output of your ASP script (i.e: the executed VBScript) is the HTML page and the Javascript code. So once executed, the ASP will produce this Javascript code:
    Code:
    var MapProvider = 'value from asp';
    When executed the value from your ASP script is then assumed by the client side Javascript. The ASP script has long since executed and no longer exists, the memory in which the variable was stored has been released and is probably being used to store someone elses map provider.

    Think about the Javascript the following line would produce:
    Code:
    <%= session("MapProvider") %> = MapProvider ;
    Once the VBScript has been executed you get:
    Code:
    value of map provider = MapProvider ;
    This would produce an error in your Javascript and demonstrates what I stated earlier, that you cannot set server side variable from a client side script.

    This is the typical life cycle of an HTTP request to a ASP script:
    1. User makes a rquest for your page through their web browser.
    2. The HTTP Request is sent through the Internet and is received by the server hosting the script.
    3. The web server software (probably IIS) dispatches the request to the ASP engine.
    4. The ASP engine finds the script and executes it. The output of the script is sent back to the web server software.
    5. The script output is then taken and sent back to the client as a HTTP response.
    6. The users web browser receives the response. This could be a HTML page, Javascript, an image or any other kind of file.
    7. The browser takes the appropriate action to render the output. Should the output be HTML it is rendered in the browser. If it contains any Javascript, the Javascript is then executed.

    By the time the output of the ASP script has been sent to your browser and it has started executing your Javascript, the runtime execution context of the script has long since been disposed of. It is for this reason that you cannot set variables inside your ASP script from a Javascript.
    Attached Images Attached Images  
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: Javascript - pass variable to VB.net(2003)

    OK - I understand that. Since $400 for Ajax is a non starter it looks like I'll have to stick to my "messy" code and "dump" the variable on the web form for retrieval by the vb form code. My problem was getting a field(variable) on the form that BOTH VB and Javascript could access. My "bad practice" of writing html from VB is the only way I know of doing it.

    If someone knows a better way ...........

  12. #12
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Javascript - pass variable to VB.net(2003)

    Quote Originally Posted by T120
    If someone knows a better way ...........
    Code:
    <input style="WIDTH: 24px; HEIGHT: 22px" readonly="readonly"  type="text" maxLength="2" size="1"  name="txtZoom"    value = "<%= Session("MapZoom") %>" />
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  13. #13
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: Javascript - pass variable to VB.net(2003)

    Quote Originally Posted by T120
    OK - I understand that. Since $400 for Ajax is a non starter it looks like I'll have to stick to my "messy" code and "dump" the variable on the web form for retrieval by the vb form code. My problem was getting a field(variable) on the form that BOTH VB and Javascript could access. My "bad practice" of writing html from VB is the only way I know of doing it.

    If someone knows a better way ...........
    AJAX is Asynchronous JavaScript and XML, it is free. AJAX is just a technique used by web developers to use javascript to dynamically change their web page, usually with a partial refresh, or none at all.

    It is FREE. What ever you see for $400 is probably a library someone is charging for. So to solve your specific problem, the general technique to pass variables for asp/php to javascript is to generate the javascript with asp/php and that way you can pass the variables that way. Other than that AJAX is your only option, probably through a webservice, because you won't need to refresh you page if go that route.

    You can get a free AJAX library if you do like 10 seconds of research on google to do this.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: Javascript - pass variable to VB.net(2003)

    Thanks for that - I'll certainly have another look for FREE AJAX

  15. #15
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: Javascript - pass variable to VB.net(2003)

    Quote Originally Posted by T120
    Thanks for that - I'll certainly have another look for FREE AJAX

    please watch the videos, you have no idea.

    http://ajax.asp.net/default.aspx?tabid=47

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: [RESOLVED] Javascript - pass variable to VB.net(2003)

    BUT AJAX is only good from framework 2.0 onwards - I'm on 1.1

  17. #17
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Javascript - pass variable to VB.net(2003)

    Mate mate mate. Go back and read post 13 carefully again.

    AJAX is not a product! It's a design technique. You can implement the server-side of AJAX on any platform under the sun, as long as it supports HTTP. It is just a "buzzword" name for using the XMLHttpRequest object in JavaScript to asynchronously retrieve data from the server in XML. That is all it is.

    As for topic: if you are wondering how to pass variables between the client-side and the server-side, you're thinking about it the wrong way. The server-side and client-side are two completely separate programs on completely separate machines! I cannot emphasise this enough. Don't generate client-side script dynamically; write the two individually. The only thing either should know about the other is the client-side's knowledge of the server-side's API.

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: [RESOLVED] Javascript - pass variable to VB.net(2003)

    I can see this is getting frustrating for both of us!!!

    I searched for AJAX yesterday and ended up at Microsoft. One of the prerequisites was Framework 2.0. As soon as I saw that I gave up looking further.

  19. #19
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Javascript - pass variable to VB.net(2003)

    Yes. I wish Microsoft hadn't called their AJAX framework AJAX.NET. It does rather confuse things.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: [RESOLVED] Javascript - pass variable to VB.net(2003)

    So Sunny Adelaide - back to square one and dumping script results in hidden html fields.

    Unless you canpoint me to the correct place and example of paswing variables back and forth using AJAX (NOT AJAX.NET)

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: [RESOLVED] Javascript - pass variable to VB.net(2003)

    So Sunny Adelaide - back to square one and dumping script results in hidden html fields.

    Unless you can point me to the correct place and example of passing variables back and forth using AJAX (NOT AJAX.NET)

  22. #22
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Javascript - pass variable to VB.net(2003)

    That's how you should do it in this case. Get your javascript to set the value of a hidden field, but make sure the hidden field has a runat="server" attribute in the ASP.NET design view so that you can access it in codebehind.

    Yes. I wish Microsoft hadn't called their AJAX framework AJAX.NET. It does rather confuse things.
    It's irritated me ever since the release. It seemed to me something motivated by search engine greed. Searching for AJAX definitely obscures relevant results.

  23. #23
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: [RESOLVED] Javascript - pass variable to VB.net(2003)

    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  24. #24
    New Member
    Join Date
    Apr 2007
    Posts
    1

    Cool Re: [RESOLVED] Javascript - pass variable to VB.net(2003)

    The only solution is to assign value to a control and then read it using js.

    Here is the sample what I did,
    In the aspx page:

    1. Create a hidden control

    <INPUT id="name_hidden" type="hidden" runat="server">

    2. Read the hidden control value(session variable)

    var varName=document.getElementById('name_hidden').value;

    In the Code behind file:

    3. Set the value for hidden control on run time

    name_hidden.Value = Session("UserName")

    Hope it will work for you!

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: [RESOLVED] Javascript - pass variable to VB.net(2003)

    Aspvava

    Thanks for that - it's a neater solution than mine and I'll give it a try.

    Regards

    Barry

  26. #26
    New Member
    Join Date
    Aug 2007
    Posts
    1

    Re: [RESOLVED] Javascript - pass variable to VB.net(2003)

    according to this code, how can i trigger a function in the code behind file when the hidden value changes in the script???
    i tried the hidden events but it didn't work & i couldn't know wht's wrong with it

    The only solution is to assign value to a control and then read it using js.

    Here is the sample what I did,
    In the aspx page:

    1. Create a hidden control

    <INPUT id="name_hidden" type="hidden" runat="server">

    2. Read the hidden control value(session variable)

    var varName=document.getElementById('name_hidden').value;

    In the Code behind file:

    3. Set the value for hidden control on run time

    name_hidden.Value = Session("UserName")

    Hope it will work for you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width