IIS ASP - HOWTO Make CStr for JavaScript on ASP w/ Request.Form and QueryStr

This is Interesting: Free IT Magazines  
Home > Archive > IIS ASP > March 2007 > HOWTO Make CStr for JavaScript on ASP w/ Request.Form and QueryStr





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author HOWTO Make CStr for JavaScript on ASP w/ Request.Form and QueryStr
ATS

2007-03-21, 7:20 pm

HOWTO Make CStr for JavaScript on ASP w/ Request.Form and QueryString

In ASP, Request.Form and Request.QueryString return objects that do not
support "toString", or any JavaScript string operation on parameters not
passed.

Example: Make a TST.asp and post to it as TST.asp?STATE=TEST

<%@ Language=JavaScript %>
<%
Response.AddHeader("Pragma", "No-Cache");
%>
<html>
<%
var csTST = Request.Form("STATE");

if ((csTST == "") || (typeof(csTST) == "undefined"))
{
%>
Request.Form("STATE") is NULL/Empty/Blank<br>
<%
}
else
{
%>
Request.Form("STATE") = "<%=csTST%>"<br>
<%
}

csTST = Request.QueryString("STATE");

if ((csTST == "") || (typeof(csTST) == "undefined"))
{
%>
Request.QueryString("STATE") is NULL/Empty/Blank<br>
<%
}
else
{
%>
Request.QueryString("STATE") = "<%=csTST%>"<br>
<%
}
%>
</html>

From this sample, the output will be as such:

Request.Form("STATE") = ""
Request.QueryString("STATE") = "REGISTER"

This is WRONG. The Request.Form is blank, but yet, the test for "" and even
typeof failed to detect it. What I want is some kind of CStr function so that
the returned data is 100% turned into a string that JavaScript can work upon.

Any ideas?

McKirahan

2007-03-21, 7:20 pm

"ATS" <ATS@discussions.microsoft.com> wrote in message
news:D0BA7279-D9BA-4FD0-91C6-C0A498D359DB@microsoft.com...
> HOWTO Make CStr for JavaScript on ASP w/ Request.Form and QueryString
>
> In ASP, Request.Form and Request.QueryString return objects that do not
> support "toString", or any JavaScript string operation on parameters not
> passed.
>
> Example: Make a TST.asp and post to it as TST.asp?STATE=TEST
>
> <%@ Language=JavaScript %>
> <%
> Response.AddHeader("Pragma", "No-Cache");
> %>
> <html>
> <%
> var csTST = Request.Form("STATE");
>
> if ((csTST == "") || (typeof(csTST) == "undefined"))
> {
> %>
> Request.Form("STATE") is NULL/Empty/Blank<br>
> <%
> }
> else
> {
> %>
> Request.Form("STATE") = "<%=csTST%>"<br>
> <%
> }
>
> csTST = Request.QueryString("STATE");
>
> if ((csTST == "") || (typeof(csTST) == "undefined"))
> {
> %>
> Request.QueryString("STATE") is NULL/Empty/Blank<br>
> <%
> }
> else
> {
> %>
> Request.QueryString("STATE") = "<%=csTST%>"<br>
> <%
> }
> %>
> </html>
>
> From this sample, the output will be as such:
>
> Request.Form("STATE") = ""
> Request.QueryString("STATE") = "REGISTER"
>
> This is WRONG. The Request.Form is blank, but yet, the test for "" and

even
> typeof failed to detect it. What I want is some kind of CStr function so

that
> the returned data is 100% turned into a string that JavaScript can work

upon.
>
> Any ideas?
>


Adding this returns what you expect:

<form method="post">
<input type="text" name="STATE" value="">
<input type="submit" value="Submit">
</form>

This too: http://localhost/temp/TST.asp?STATE=

(If I understand you correctly.)


Anthony Jones

2007-03-22, 7:29 am


"ATS" <ATS@discussions.microsoft.com> wrote in message
news:D0BA7279-D9BA-4FD0-91C6-C0A498D359DB@microsoft.com...
> HOWTO Make CStr for JavaScript on ASP w/ Request.Form and QueryString
>
> In ASP, Request.Form and Request.QueryString return objects that do not
> support "toString", or any JavaScript string operation on parameters not
> passed.
>
> Example: Make a TST.asp and post to it as TST.asp?STATE=TEST
>
> <%@ Language=JavaScript %>
> <%
> Response.AddHeader("Pragma", "No-Cache");
> %>
> <html>
> <%
> var csTST = Request.Form("STATE");
>
> if ((csTST == "") || (typeof(csTST) == "undefined"))
> {
> %>
> Request.Form("STATE") is NULL/Empty/Blank<br>
> <%
> }
> else
> {
> %>
> Request.Form("STATE") = "<%=csTST%>"<br>
> <%
> }
>
> csTST = Request.QueryString("STATE");
>
> if ((csTST == "") || (typeof(csTST) == "undefined"))
> {
> %>
> Request.QueryString("STATE") is NULL/Empty/Blank<br>
> <%
> }
> else
> {
> %>
> Request.QueryString("STATE") = "<%=csTST%>"<br>
> <%
> }
> %>
> </html>
>
> From this sample, the output will be as such:
>
> Request.Form("STATE") = ""
> Request.QueryString("STATE") = "REGISTER"
>
> This is WRONG. The Request.Form is blank, but yet, the test for "" and

even
> typeof failed to detect it. What I want is some kind of CStr function so

that
> the returned data is 100% turned into a string that JavaScript can work

upon.
>
> Any ideas?
>


You need to bear in mind that that both form and querystring fields can be
multivalued hence both actual return an indexable object. The objects
returned have an Item indexer property that is marked as the default
property. In VBScript whether to access this default property or not is
determined by whether the assignment is prefixed with the Set keyword. In
JScript default properties are ignored and the object reference is always
passed.

Hence in the code above it doesn't matter what name you pass to QueryString
or Form you will always get an object back. I.E. typeof(csTest) ==
'object' will always be true.

Use code like this:-

csTST = Request.QueryString("STATE").Item // note the additional explicit
use of Item.

Now with no state in the query string then typeof(csTST) == 'undefined' is
true.

With ?state= then typeof(csTST) == 'string' is true and csTST == "" is
true.

With ?state=NY then typeof(csTST) == 'string' is true and csTST == "" is
false.

Personally I would just use:-

if (csTest)
{

}

in my code.



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com