Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variables from vbscirpt 2

Status
Not open for further replies.

pgstein

Programmer
Jul 27, 2005
33
0
0
US
I am creating an ASP.NET project, and what I need to do is have the user click a button, he is then prompted with a date, and then he is transfered to the correct page based on his input.

I am using VBScript to generate the input box, but the problem I am having is when I use "window.open" in VBScript, the ASP.NET page just refreshes itself. Is there or fix for this? and if not, can I pass variables from my VBScript back to my ASP.NET page?

Thanks alot, some of my code is posted below, any help is greatly appreciated!
Paul

This is my ASP.NET Code:
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Attributes.Add("onClick", "return test(""mnthlyaverage.aspx"");")
End Sub

This is my VBScript function:
Code:
function test(pagename)
        dim startdate
	dim enddate
        Dim URL

	startdate=InputBox("Enter Start:")
	enddate=InputBox("Enter End:")
	URL = pagename & "?startdate=" & startdate & "&enddate=" & enddate
	window.open(URL)
end function
 
pgstein - I haven't seen much mix over the last year or so on these threads in which vbscript is used - I would suggest that perhaps using javascript to accomplish the same thing in conjunction with an aspx page might be the better route.

However, I am not familiar with mixing vbscript + ASP.NET so your approach may in fact be perfectly acceptable. Good chance one of the more experienced programmers will drop by in a bit and give you their recommendations on this.
 
Thanks for the response, I am not tied to vbscript. I can just as easily code it in javascript. Is there a way to pass a variable back using javascript?
 
You could put the value in a hidden input and then read that server-side


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
okay, I am an idiot. Maybe I dont need to pass it after all. I am using the window.navigate(URL) in javascript.

When I use window.open(URL), it opens the correct page in a new window, but when I use window.navigate(URL), the ASP.NET page just refreshes itself after the event.

Aslong as I can direct the user to the right page using javascript, I wont need to pass a variable back.
 
pgstein - I hardly doubt you are an idiot! We all feel that way from time to time; your question is perfectly legit -- and actually a good one I think.

There are of course Browser specific aspects to javascript and you may want to keep this in mind. If your question is specifically javascript you can always jump over to the javascript forum where the gurus over there generally make short work of any problems presented.

I'm a tad bit confused but I think its me; you have been fairly clear. ca8msm suggested placing a value in a hidden textbox (inside javascript) and proceed.

At any rate your problem is simple enough. Let me see if I'm on the right track.

You want a pop-up to take in a date window, start and finish. Then you want javascript and/or code behind in the pop-up aspx page to evaluate the date window and select the response.redirect page accordingly (in which case the original page gets changed to the new one and the pop up closes? Is that it? If so we can post a little code to get through this. Post back.
 
Your exactly right. There were two ways I could think of doing it. Either having javascript redirect to the new page, or passing a variable back and using response.redirect in ASP.NET. Either way is fine, but I cant get either to work. Any code you could help me with I would apprecitate. Thanks again for your help.
 
Ok. Lets try this and see how it goes. First you need a javascript routine that will open up a pop-up aspx page, for example:
Code:
function getDates(){
 var strPg; 
 var strCondit = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,minimize=no,copyhistory=no,width=565,height=540,left=40,top=120"
  strPg = "MyDatePopup.aspx"
  myWindow = window.open(strPg, 'newWin', strCondit)
}
Now if you were sending up variables you'd have to modify this to take the variables with the call to open up the window. In this case you just want to open up a window that will taken in a two dates and return these values to the original form so a Response.Redirect can be carried out. However to capture the values in the popup window and return them (at least for this example) the parent form name is used, the code in the popup being:
Code:
<html>
<head>
<title>Accept After and Before Dates</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function ReturnY1(ADate, BDate){
var BDate;
var ADate;
window.opener.document.forms["frmParentForm"].elements["txtBDate"].value = BDate;
window.opener.document.forms["frmParentForm"].elements["txtADate"].value = ADate;
window.opener.document.forms["frmParentForm"].submit();
window.close();
}      
</script>
<form id="frmY1" runat="server">
Choose ADate:<br>
<asp:TextBox id="txtADate" runat="server" width="50px"/>
Choose BDate:<br>
<asp:TextBox id="txtBDate" runat="server" width="50px"/>
<p><INPUT TYPE="button" VALUE="Accept" onClick="javascript:ReturnY1(document.frmY1.txtADate.value, document.frmY1.txtADate.value)" width="60px">
</form>
</body>
</html>
I've tried to modify according to your situation; stripping table and css elements. Now when the pop-up button is clicked the ADate and BDate values should be stuck to the original page's textboxes (they could be hidden) and the page re-submitted and then processed in a post back line of code.

This is in the ballpark. We may have to do some modification to get it exactly as you like. Post back and let me know how it goes.
 
Note that txtBDate and txtADate are textboxes on the Parent form "frmParentForm".
 
I cannot get it to work, let me explain exactly what I did.

1. In my project I created a new page called MyDatePopup.aspx, and used the html you gave me.
2. On my main page, called main.aspx I added the function, and then adding the button.attribute part to call it.
3. I changed "frmParentForm" to "main" because thats my form name
4. I added two textboxes, called txtADate and txtBDate to "main"

When I run it, the page popups, I input values, and when I hit submit I get the following error:
'window.opener.document.forms.main.elements' is null or not an object

Did I do something wrong? Thanks.


 
pgstein: My oversight! I took one part of the code from a main page and the other from a popup and they were not the pair intended. We need to send the form name up as well. Its 10:40 (CST) my time; hand loose; I'll build the form - it'll take about 20 minutes and post back. (There was two ways I was using this, one carried variables up to the popup, the other did not; and each had its own page open java -- so to keep things from getting confused I'll just quicly build the two date pop up and post back within the next 30 mins and that way it'll work on this end and therefore should work on your end (with the code I posted above you would have had to supply the Form name in the URL for the pop up). Hang loose, be right back.
 
Thanks alot, I really appreciate it. Take your time, Im in no hurry.
 
pgstein: Not to keep you waiting; we're just about there. The following code takes initial values from the Main form up to the popup, returns new values to the Main form when changed in the popup, and causes a post back. Now, this is not complete, we have a one more modification to make. Open up 2 Notepad files and stick the following code and make sure it is behaving on your end. By the time you get that done I'll post back what I see as a final solution. Note that I am using an html button on the Main page and not an aspx button (don't want it post back - but consider this; an aspx button my supliment the "form.submit" code of the popup javascript function (haven't tested that).

'--------- Main page: MyDate.aspx --------------------------
Code:
<%@ Page Language="VB"%>
<%@Import Namespace = "Microsoft.VisualBasic"%>
<%@Import Namespace = "System"%>
<%@Import Namespace = "System.Web"%>
<%@Import Namespace = "System.Web.UI"%>
<%@Import Namespace = "System.Web.UI.WebControls"%>
<%@Import Namespace = "System.Web.UI.HtmlControls"%>
<script runat="server">
Sub Page_Load(Sender As Object, e As EventArgs) 
 If Not IsPostBack Then 
   'xxx
 Else 'this is a postback..
  txtTest.Text = "Post back done"
 End If
End Sub
</script>
<html>
<head>
<title>MyDate.aspx Test Page</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function getDates(){   
 var strPg;
 strPg = "MyDatePopup.aspx?adate=" + document.forms["Main"].elements["txtADate"].value + "&bdate=" + document.forms["Main"].elements["txtBDate"].value
 ChildWindow = window.open(strPg , 'newWin', "width=310,height=120,top=200,left=320,toolbars=no,scrollbars=no,status=no,resizable=no");    
} 
function CheckWindow(){
 ChildWindow.close(); 
} 
</script>
<form id="Main" runat="server">
After Date (could be invisible):<asp:TextBox id="txtADate" runat="server"/><br>
Before Date (could be invisible):<asp:TextBox id="txtBDate" runat="server"/>
<INPUT TYPE="button" VALUE="Get Dates" onClick="javascript:getDates();">
<br><asp:Textbox id="txtTest" runat="server"/>
</form>
</body>
</html>

----------MyDatePopUp.aspx --------------------------------
Code:
<%@ Page Language="VB"%>
<%@Import Namespace = "Microsoft.VisualBasic"%>
<%@Import Namespace = "System"%>
<%@Import Namespace = "System.Web"%>
<%@Import Namespace = "System.Web.UI"%>
<%@Import Namespace = "System.Web.UI.WebControls"%>
<%@Import Namespace = "System.Web.UI.HtmlControls"%>
<script runat="server">
Sub Page_Load(Sender As Object, e As EventArgs) 
 If Not IsPostBack Then 
   afdate.Text = Request.QueryString("adate")
   bfdate.Text = Request.QueryString("bdate")
 End If
End Sub
</script>
<html>
<head>
<title>Get Dates</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function ReturnDates(adate, bdate){
var adate;
var bdate;
window.opener.document.forms["Main"].elements["txtADate"].value =adate;
window.opener.document.forms["Main"].elements["txtBDate"].value = bdate;
window.opener.document.forms["Main"].submit();
window.close();
}      
</script>
<form id="Form1" runat="server">
After Date:<asp:TextBox id="afdate" runat="server"/><br>
Before Date:<asp:TextBox id="bfdate" runat="server"/>
<INPUT TYPE="button" VALUE="Accept" onClick="javascript:ReturnDates(document.Form1.afdate.value, document.Form1.bfdate.value)">
<input Type="text" id="txtFN" runat="server"/>
</form>
</body>
</html>

Ok, I'll post back in about 15 with the final code - you might create these 2 pages and test them.
 
pgstein: I found that you could send up the 2 textbox values as "NULL" values and then send back the new values; but I don't particularly like that approach. Will post back shortly.
 
pgstein: ok these pages seem to work ok; let me know if you get an error. Also something you might consider is to put a calendar over each of the textboxes on the pop-up; if you are doing this I have some code that sets up the calendars and retrieves their value to send back. Ok try this:

'------------- MyDateFinal.aspx -----------------------------
Code:
<%@ Page Language="VB"%>
<%@Import Namespace = "Microsoft.VisualBasic"%>
<%@Import Namespace = "System"%>
<%@Import Namespace = "System.Web"%>
<%@Import Namespace = "System.Web.UI"%>
<%@Import Namespace = "System.Web.UI.WebControls"%>
<%@Import Namespace = "System.Web.UI.HtmlControls"%>
<script runat="server">
Dim intDD As Integer
Sub Page_Load(Sender As Object, e As EventArgs) 
 If Not IsPostBack Then 
   'xxx
 Else 'this is a postback..
  intDD = DateDiff("m", txtADate.Text, txtBDate.Text)
  If intDD > 12 Then
    Response.Redirect("[URL unfurl="true"]http://www.google.com")[/URL]
  Else
    Response.Redirect("[URL unfurl="true"]http://www.altavista.com")[/URL]
  End If
 End If
End Sub
</script>
<html>
<head>
<title>MyDate.aspx Test Page</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function getDates(){   
 var strPg;
 strPg = "MyDatePopup.aspx"
 ChildWindow = window.open(strPg , 'newWin', "width=310,height=120,top=200,left=320,toolbars=no,scrollbars=no,status=no,resizable=no");    
} 
function CheckWindow(){
 ChildWindow.close(); 
} 
</script>
<form id="Main" runat="server">
After Date (could be invisible):<asp:TextBox id="txtADate" runat="server"/><br>
Before Date (could be invisible):<asp:TextBox id="txtBDate" runat="server"/>
<INPUT TYPE="button" VALUE="Get Dates" onClick="javascript:getDates();">
</form>
</body>
</html>

'-----------MyDatePopUPFinal.aspx --------------------------
Code:
<%@ Page Language="VB"%>
<%@Import Namespace = "Microsoft.VisualBasic"%>
<%@Import Namespace = "System"%>
<%@Import Namespace = "System.Web"%>
<%@Import Namespace = "System.Web.UI"%>
<%@Import Namespace = "System.Web.UI.WebControls"%>
<%@Import Namespace = "System.Web.UI.HtmlControls"%>
<script runat="server">
Sub Page_Load(Sender As Object, e As EventArgs) 
 If Not IsPostBack Then 
   afdate.Text = Request.QueryString("adate")
   bfdate.Text = Request.QueryString("bdate")
 End If
End Sub
</script>
<html>
<head>
<title>Get Dates</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function ReturnDates(adate, bdate){
var adate;
var bdate;
window.opener.document.forms["Main"].elements["txtADate"].value =adate;
window.opener.document.forms["Main"].elements["txtBDate"].value = bdate;
window.opener.document.forms["Main"].submit();
window.close();
}      
</script>
<form id="Form1" runat="server">
After Date:<asp:TextBox id="afdate" runat="server"/><br>
Before Date:<asp:TextBox id="bfdate" runat="server"/>
<INPUT TYPE="button" VALUE="Accept" onClick="javascript:ReturnDates(document.Form1.afdate.value, document.Form1.bfdate.value)">
<input Type="text" id="txtFN" runat="server"/>
</form>
</body>
</html>

Hopefully we're there; or very close.
 
One of the orignal problems, I forgot to mention, was that we were not using "childwindow" on the javascript open command, hence the difference.
 
(Sorry I got pulled away for a minute) Your code works great. I can't thank you enough for helping me out. I really appreciate it.
 
No problem - it was a good exercise for the both of us.
 
pgstein: Hey, sorry to bother you; but interestingly I was working on a similar problem today except that the values were obtained and pass to a web page within an IFRAME on a web page; the pertinent part of the java being:

Code:
function getY2(){
 var fr=frames['myIframe'].document.forms[0]; 
 var strPg; 
 var w = window.screen.availWidth;
 w = (w-210)/2;
 var h = window.screen.availheight;
 h = (h-150)/2;
 var strCondit = 'left='+ w +',top='+ h +',height=150,width=210,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,minimize=no,copyhistory=no';
 strPg = "qwwSetY2.aspx?Y2Max=" + fr.txtY2Max.value + "&Y2Min=" + fr.txtY2Min.value + "&Y2Int=" + fr.txtY2Int.value
 childWindow = window.open(strPg, 'newWin', strCondit)
}
The reason I am posting this is because it is related to what we did this morning plus it uses a java reference to an IFRAME (uses window. and not documents. prefix) and also there is code in which the popup is centered on the screen. Just a related tidbit - see ya around.
 
pgstein: Not to beat a dead horse here but here's another tidbit on this subject.

The two textboxes in which I catch the incoming variables from the popup I want hidden. However, using an <asp:Textbox../> I find I have to keep them visible and make them invisible by setting width="0" and height="0"; and in this way you cannot see them on the page. If I set the Visible=False property the routine does not work. It may be possible to use straight away a <Input Type="hidden"..> html object and the routine would work just fine, haven't tested this approach since the initial one works just fine. Just a thought on this in case you run into it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top