How to write JavaScript and asp code on the same line?

Go To StackoverFlow.com

0

Can some one tell me how can I achieve this javascriptVariable=AspVariable concatenated with javascript incrementing variable;

ie I want to change the left hand side of the equation dynamically,here is how I tried to do it and FAILED:

for(j=1;someCondition;j++)
{
eval("job"+j).Sdate=parseInt('<%= djobSdate[%>'+j+'<%=] %>'); 
}

the right hand side works fine but the left hand side of the equation gives problem

2012-04-04 08:31
by Snedden27
Is djobSdate a server variable? If it is an array you need to output the whole array to the page whith something like Page.ClientScript.RegisterArrayDeclaration( - mortb 2012-04-04 08:40
Is it feasible for you to render the final Javascript Text server side ? I mean prepare the string at server side and use RegisterStartUpScript at server side. This will also work for you - Pankaj 2012-04-04 08:44
no calling javascript from server would be very hectic ,it may work but would have to rewrite a alot of code and this might give further issues,I am new to server side scripting infact this is the first time I doing it,thanks for your answers tho,I am am doing a lot of trial and error stuff hope something would clic - Snedden27 2012-04-04 08:59
The way I interpret '<%= djobSdate[%>'+j+'<%=] %>' is that you are trying to mix server and client side in a way that will not work for you. The tags <%= %> will only output text and not objects. You need either to 1. Output a ready processed text to print on the page or 2. Output your object (transformed in someway) to javascript that your javascript code will use. (Not easy to explain, I did the same kind of errors some years ago - mortb 2012-04-04 09:33


1

To get the result of server side function djobSdate() by passing javascript variable j, you will need to use AJAX.

This is because the variable j has its value calculated client side.

Look into using ASP.NET Web Services with jQuery AJAX

You may find this article useful:

http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/

Then your code would change to something like:

for(j=1;someCondition;j++)
{
  $.ajax({
    type: "POST",
    url: "myWebService.asmx/GetdjobSdate",
    data: '{j: "' + j + '"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
       eval("job"+j).Sdate=parseInt(data);
    }
  });
}

And your web service would look something like:

[System.Web.Script.Services.ScriptService]
public class myWebService : System.Web.Services.WebService
{
    [WebMethod]
    public string GetdjobSdate(j)
    {
        return djobSdate(j);
    }
}
2012-04-04 08:38
by Curt
Ads