Run an .aspx file from a .js (javascript file) in .NET

Should be simple. But I’m not getting any responses from a computer forum. So, I’m going to give you folks a try.

I have a .js (jscript) file that creates a line of code - “identify.aspx?max=100”

Fine.

But how do I get it execute I just want the identify.aspx file to run and pass it the value max=100.

I don’t want to open any new documents or windows. Can I set it as the URL of a ‘blank’ document or something?

You need to look into what they call AJAX to do this. Google the term, there are plenty of sites about it. Here’s an introduction to AJAX.

Hmm… that ajax stuff sounds interesting!!

for a simpler solution, maybe you could put a very small iframe on your page and use that to execute the page.

You can also use SOAP, or a lighter weight protocol.

I wrote a Javascript tree control which fetches new branches from the server when an expand box is clicked. I send a load page request to a hidden iFrame, passing the SQL index of the node clicked on the querystring. That page has some ASP code that does an SQL lookup, gets the collection of children, and creates a Javascript array. An onload handler on the page calls a javascript function in the parent page of the iFrame, passing the collection of children. Then I use DHTML to create the new leaf of the tree. The user never sees any of this. It just looks like a regular tree control.

Thanks for the suggestions. I’ve given the iFrame a shot with no luck.

in my default.aspx I have -
<IFRAME ID=IFrame1 FRAMEBORDER=0 SCROLLING=NO></IFRAME>

then in a .js file -

test = document.getElementById("IFrame1");
test.src = "Identify.aspx?max=100";

I don’t get any errors or anything, but the code in Identify.aspx is never reached.

Thanks

I’m not sure about the getElementById method, but try replacing ‘test.src’ with ‘test.location.href’

This is a snippet of my own code which seems to work well for loading an iframe from javascript. (Admittedly, it’s an iframe that’s there to provide visible results, but the principle should be at least partially the same.)



		function findInFrame() {

			frames[0].location.href = "test.asp?finder=yes"

		}


Thanks chrisk,

Got it working. The test.src is working now, I ended up moving the location of the IFrame in the default.aspx and, I think part of my problem was the way I was interpreting the debugger.

test.src = identify.aspx?max=100 is in the middle of a function. I thought that the debugger would immediatly resolve the url. It seemed instead that the function would finish before the identify.aspx file would run. Or at least that’s how the debugger seemed to be interpereting it.

Thanks again

Spoke to soon the IFrame will launch the .aspx, but I want the .aspx to change the values on some Web Forms. Doesn’t look like it can do that.

So, what I have been reading about AJAX looks like it will work. If I understand, XML will be sent back to the calling jscript where it can be processed and update the page.

I would also like to check out SOAP. But its a tough thing to google. Anyone have any good sites they know of? I could really use a tutorial or some samples.

Thanks

Have a look at the solution I offered. AJAX may be overkill, depending on exactly what you’re trying to do.

The page in the iFrame can get access to the parent object model. So you can load your aspx page in the iFrame, then call a javascript function in the parent and pass data to it, or directly manipulate the parent DOM from the iFrame, doing whatever you want. It’s really not hard. JAVAX is a generalized implementation of this concept, but if you’re just doing something simple (like displaying a table of results from a server-side query or something), it’s probably faster and simpler to just do it without the Javax framework.

AJAX (is that the same as JAVAX) does look like it will work. But It does seem like over kill. At least to me (it’s probably something I sould try to fiqure out thou [thanks for the link rysto].

It may be best if I try to spell it out in English. I can then send code later.

In English -

I have a default.aspx with some Web Forms on it. One form is an <IMG> (its a map). A click on the image calls a jscript function. The function gets the XY coordinate of the click.

That XY coordinate is the passed to identify.aspx like this - iFrame.src = identify.aspx?x=100&y=100. Identify.aspx.vb page_load builds XML that is sent to a service. The identify.aspx reads the returned XML, and sends a call to an SQL database. The data is returned to the identify.aspx.vb in datareader class.

Now I have the data. I need to get it back to the parent and populate some cells in a table.

I think I’m missing how to call a javascript function in the parent (default.aspx) from the identify.aspx.vb page that has the data.

[sub]thinking…
I’ve be trying to directly populate the table in the default.aspx from a file that is loaded in the iFrame…
Can I perhaps get back to the iFrame, from identify.aspx.vb and have the iFrame run a jscript function…
What has thrown me is that default.aspx creates a class (mappage). A public shared with events object there is the table.
I can see mappage.table.row(0).cell(0).text in the identify.aspx.vb. But when I try to change it. Nothing happens, no errors no nothing.
Basically, I think that I have been going up one ladder, and down another and expecting to be in the same place.
ohhhhh… I think I might have it…I’ve been trying to have identify.aspx.vb populate the table cells in default.aspx directly. I’ve got to go back down the iFrame ‘ladder’ and have it call some jscipt (on the client) and have it change the values.
I’ll try it tomorrow.[/sub]

Thanks so much for the help (everyone). I’m at home now, and don’t have any access to any code snippits.

Maybe this would be better handled in email? This is not going to be very interesting to most people on the SDMB.

Javax stands for “Java Extensions”. They’re just extra libraries for the Java programming language provided by Sun. Nothing to do with AJAX.

Sorry for the confusion. I meant to type AJAX, but somehow it came out ‘Javax’.

Okay, here’s how to call a parent function. I just whipped this up with notepad, and verified it works. Let’s say your main page looks like this:



<html>
<head>
<script language="Javascript">
function loadTableFromServer(tableData)
{
   //function that takes tableData parameter and uses it to populate table
   alert("Look! I got this data: " + tableData);
}

function getData()
{
  var rpc = document.getElementById("rpcFrame");
  rpcFrame.location.href="queryServer.html";
}
</script>
</head>
<body>
<iframe id="rpcFrame" style="display:none;"></iFrame>
<input type="button"  value="Clickme to load table" onclick="getData()" />
</body>
</html>


Now you have your server page, called QueryServer.html:



<html>
<head>
<script language="Javascript">
function sendData()
{
  var tableData="Foo";
  window.parent.loadTableFromServer(tableData);
}
</script>
</head>
<body onload="sendData()">
</body>
</html>


Copy that code, paste it into two files, then load the main doc and click the button. The second file will be loaded in the hidden iFrame, then it will make a call to the Javascript function in the first file, passing some data.

Now, since you’re doing a server lookup, the second file will obviously be an .asp or .aspx file. Do your query, then dynamically write the results as Javascript variables, and pass them back to the main page.

There are many ways to do this. The one I posted above is about as simple as it gets. You could do lots of things differently. For example, you could could directly manipulate the DOM of the parent page, rather than calling a javascript function, using DHTML to dynamically insert columns and rows into a table right from the page loaded in the iFrame. Or, you could use ASP to load arrays of data in Javascript, then pass the arrays up to the main page. Or, you could call one function in the main page, which then reads the javascript variables from the iFrame page. Endless combinations.

My Server page is actually an .aspx page.

This is what I can’t figure out. My .aspx is VB. Not sure how to call a javascript in the parent with VB. Or how to pass VB variables to jscript. I just found something called a postback method. Might that be it?

This is what I tried to do originally. For instance, in my .aspx.vb file that is loaded in the iFrame this code -


MapPage.Table1.Rows(0).Cells(0).Text() = "identified"

Does not error out, but it does not change on the Cell on the MapPage. However if I set -


test = MapPage.Table1.Rows(0).Cells(0).Text()

test will be equal to “identified”. But as I said, but the text on the page does not actually change.

OK. Maybe I see it.

If I put



function sendData(TableData)
{
    window.parent.loadTableFromServer(TableData);
}
</script>


In my Identify.aspx file. How do I get my server side Identify.aspx.vb to execute it and send it data?

Okay, I don’t think you’re really clear on the concept of which code runs server-side, and which code runs on the client. Javascript is client side code. Your VB code runs on the server. What you’re going to do is use your asp to generate that Javascript. Then when the resultant page is loaded on the client, the Javascript will be executed. For example, you could have something like this:



<script language="Javascript">
function sendData()
{
   var mydata="<%=myServerVariable%>";
   window.parent.getData(myData);
}
</script>


Now, that ASP code snippet will be replaced on the server with whatever is in myServerVariable. For example, let’'s say that myServerVariable was the result of an SQL query, which returned the name ‘Fred’. If you looked at the code above in your browser after it returned from the server, this is what you’d actually see:



<script language="Javascript">
function sendData()
{
   var mydata="fred";
   window.parent.getData(myData);
}
</script>


Again, this is just one way to do it, but it will work. You could generate entire javascript arrays on the server side if you wanted. For instance, let’s say your SQL query returned an array of strings (“Foo”,“bar”,“snafu”). You could write something like this:



Response.write("var myArray = new Array();");
for i = 0 to myArray.length-1
  Response.write("myArray(" & i &  ") = '" & myArray* & "';")
next


The resulting Javascript that would be generated would look like this:



var myArray = new Array();
myArray(0) = 'foo';
myArray(1) = 'bar';
myArray(2) = 'snafu':


Now you can write your javascript function to pass the array to the calling page.

Does that make sense?

Note that this is plain-jane ASP type stuff. ASPX, depending on the features you use, can be quite different. But the concept is the same - keep in mind that your vb code runs on the SERVER, and Javascript runs on the CLIENT. If you want to send data back to a client and execute it in Javascript, you have to convert the results of your server-side query to client-side code. There are many, many ways to do this. For example, instead of dynamically creating that Javascript array, I could have just dumped the contents of the array into a hidden DIV on the page as comma-delimited text, then parsed it when it loads back in the client. You could even dynamically generate XML and use an XML data island object if you’re using IE only.

Okay, I don’t think you’re really clear on the concept of which code runs server-side, and which code runs on the client. Javascript is client side code. Your VB code runs on the server. What you’re going to do is use your asp to generate that Javascript. Then when the resultant page is loaded on the client, the Javascript will be executed. For example, you could have something like this:



<script language="Javascript">
function sendData()
{
   var mydata="<%=myServerVariable%>";
   window.parent.getData(myData);
}
</script>


Now, that ASP code snippet will be replaced on the server with whatever is in myServerVariable. For example, let’'s say that myServerVariable was the result of an SQL query, which returned the name ‘Fred’. If you looked at the code above in your browser after it returned from the server, this is what you’d actually see:



<script language="Javascript">
function sendData()
{
   var mydata="fred";
   window.parent.getData(myData);
}
</script>


Again, this is just one way to do it, but it will work. You could generate entire javascript arrays on the server side if you wanted. For instance, let’s say your SQL query returned an array of strings (“Foo”,“bar”,“snafu”). You could write something like this:



Response.write("var myArray = new Array();");
for i = 0 to myArray.length-1
  Response.write("myArray(" & i &  ") = '" & myArray* & "';")
next


The resulting Javascript that would be generated would look like this:



var myArray = new Array();
myArray(0) = 'foo';
myArray(1) = 'bar';
myArray(2) = 'snafu':


Now you can write your javascript function to pass the array to the calling page.

Does that make sense?

Note that this is plain-jane ASP type stuff. ASPX, depending on the features you use, can be quite different. But the concept is the same - keep in mind that your vb code runs on the SERVER, and Javascript runs on the CLIENT. If you want to send data back to a client and execute it in Javascript, you have to convert the results of your server-side query to client-side code. There are many, many ways to do this. For example, instead of dynamically creating that Javascript array, I could have just dumped the contents of the array into a hidden DIV on the page as comma-delimited text, then parsed it when it loads back in the client. You could even dynamically generate XML and use an XML data island object if you’re using IE only.

Hi Sam. Thanks again.

Oh yeah, I’ll admit that sometimes that I don’t see where the code is going, server or client. I’m getting it though.

The crux is, we are moving over to .NET (I work for a small County Gov, and the internet is about the only resources to learn how to do this [I took a 5 day MS VS05 .NET class but it was not an introductory one. That one was full. It was a bit over my head [no surprise])

Bolding mine.

Umm. Maybe I was not clear. I’m looking for help in ASPX. I’m working in .NET. Or trying to… ASPX Is very different. That’s why I’m asking for help. Not that I’m any fireball in regular ASP.

I don’t want to run any VBScript on the server side. I want to do it in ASP.NET with VB code - aspx.vb.

I looking for a .NET solution.

Are you suggesting putting jSscript or VBscript in a .aspx.vb file? :confused:

I’ll look closer at your recommedations, but I think we are looking at two different solutions. Or maybe you didn’t realize I was looking at .NET.

Our existing site does what I need to do in plain ASP with all vbscript on the sever side. And boat loads of jscript on the client side. I want to get rid of as much jscript as possible and make light clients. And write as much as I can on the server side in VB.NET.

I really want to tighten the existing site up (actually, it will go away). Most of the site I built for my employer is based on sample scripts from our service provider that I have modified (it should be noted that the service does run on our own server). I understand it for the most part, but… the .NET switch over is being a bit of a problem.

I really want to, and need to do this in .aspx. - .NET. It’s a bit tough to query help pages when you don’t know what to look for. I think, ‘postback’ might be it. Working on it. It seems that you create the classes in .NET (say a HTML table)and then have the ‘default’ page call them when it loads. Then the server can update them buy calling the class.

Thanks for all your code.

[sub]Please excuse me if I get a bit frustrated. I have a boss that thinks thinks every thing is easy because he read about it somewhere….[/sub]