Dmitrij Holkin
Dmitrij Holkin

Reputation: 2055

Assign variable from one asp to other asp page

I have to asp pages, on selector change i send ajax to other asp page and want to get back variable from that asp page.

index.asp

<div  id="ExSearch" name="ExSearch" >
<script>

$(function() {   
    $('div.selector select').on('change', onValueChange);
    var sIndex = 2;
    var Vb_sIndex;
    Vb_sIndex = <%=Vb_sIndex_geted%> ' How to do that?
    function onValueChange() {      
      sIndex = sIndex + 1;
        $.ajax({
            url: '/Functions/ExSearch.asp',
            type: 'POST',
            data: 'startIndex=' + sIndex + '',
            dataType: 'text',
            success: function (data) {
                $("#ExSearch").html(data);
            }
        });
    };
});

</script>
<%= Vb_sIndex%>

ExSearch.asp

<% Vb_sIndex_geted = Request.form("startIndex") + 1
     Response.Write = Vb_sIndex_geted ' for first click it = 4,how to assign this value to index.asp?
%>

How to set Vb_sIndex in index.asp from ExSearch.asp ?

Upvotes: 1

Views: 132

Answers (1)

Bond
Bond

Reputation: 16311

AJAX runs client-side after ASP has processed all of your <%...%> sections, so you can't return a value from AJAX into a server-side variable (without some additional work, anyway).

Also, you're mixing client-side and server-side code in your example. Vb_sIndex is declared in your client-side jQuery function but you're attempting to use it in a server-side ASP tag: <%= Vb_sIndex%>. This isn't going to work. When ASP processes <%= Vb_sIndex%>, you'll either get an error (if Option Explicit is declared) or an empty value (if not).

If you want to assign the return value of your AJAX call to a server-side variable, consider creating and submitting a form in your AJAX success function, passing data as a hidden form variable. Something like this should work:

$.ajax({
    ....
    success: function (data) {
        $('<form method="post" action="index.asp"><input type="hidden" name="h" value="' + data + '"></form>').appendTo('body').submit();
    }            
}

Then when your page is reloaded, you can pull the value out of your Form collection:

<%
    Dim Vb_sIndex
    Vb_sIndex = Request.Form("h")
%>

Upvotes: 1

Related Questions