Link to home
Start Free TrialLog in
Avatar of TechMonster
TechMonster

asked on

Retrieve variables from Query String using asp.net

I have a query string with first name and lastname.  
I was told to use the attached code to display the info..but it does not seem to be worklng.
my querystring variables looks like      ?fn=myFirstname&ln=myLastname

I would like to be able to pass these into the body...I have tried to do the Alert(lastname) but nothing comes up but "undefined"

Thanks.
<SCRIPT type ="text/javascript">
function qs() {
      var query = window.location.search.substring(1);
      var parms = query.split('&');
      for (var indx=0; indx<parms.length; indx++) {
            var pos = parms[indx].indexOf('=');
            if (pos > 0) {
                  var key = parms[indx].substring(0,pos);
                  var val = parms[indx].substring(pos+1);
                  qsParm[key] = val;
            }
      }
      return qsParm;
      
var firstname = qs['fn'];
var lastname = qs['ln'];
 
      
      
} 

</script>




</head>



<body>




</body>

</html>

Open in new window

Avatar of stalhw
stalhw

and are you calling the function qs somewhere?
This does not belong to  Java Programmimg Language zone.
also, qsParms is never defined, and in the function you are using qs['ln'] but qs is a function not an arrays...
try this instead:

<SCRIPT type ="text/javascript">
function qs() {
	var qsParm=new Array();
      var query = window.location.search.substring(1);
      var parms = query.split('&');
      for (var indx=0; indx<parms.length; indx++) {
            var pos = parms[indx].indexOf('=');
            if (pos > 0) {
                  var key = parms[indx].substring(0,pos);
                  var val = parms[indx].substring(pos+1);
                  qsParm[key] = val;
            }
      }
      return qsParm;
} 





var info=qs();

var firstname = info['fn'];
var lastname = info['ln'];

alert(firstname);
alert(lastname);


</script>

Open in new window

Avatar of TechMonster

ASKER

I thought I did put this is javascript..sorry.

Stalhw - The code works great...would you happen to know how to pass them into a textbox in html.

<%@ Page Language="C#" %>
<html dir="ltr" xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">

<head runat="server">
<META name="WebPartPageExpansion" content="full">
<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:PublishingContactPicture msdt:dt="string">, </mso:PublishingContactPicture><mso:PublishingContact msdt:dt="string"></mso:PublishingContact></mso:CustomDocumentProperties></xml><![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled 1</title>


   
 



</head>



<body>
<SCRIPT type ="text/javascript">
function qs() {
      var qsParm=new Array();
      var query = window.location.search.substring(1);
      var parms = query.split('&');
      for (var indx=0; indx<parms.length; indx++) {
            var pos = parms[indx].indexOf('=');
            if (pos > 0) {
                  var key = parms[indx].substring(0,pos);
                  var val = parms[indx].substring(pos+1);
                  qsParm[key] = val;
            }
      }
      return qsParm;
}





var info=qs();

var firstname = info['fn'];
var lastname = info['ln'];
//document.write(firstname + ' ' + lastname);
</script>

<label id="lblFirstName">First Name </label><textarea name="txtFirstName" >firstname</textarea>
<br>
<label id="lblLastName">Last Name </label><textarea name="txtLastName">lastname</textarea>

</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of stalhw
stalhw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial