PDA

View Full Version : Passing parameters to javascripts from code-behind


kevinkan
10-11-2002, 03:53 AM
Hi all,

I would like to pass parameters to a javascript function through the code-behind...
do I pass it in like this? :

Response.Write("<script>validate("+param+");</script>");

Please advise...

Cheers,
Kevin
--------------

raf
10-11-2002, 11:12 AM
Hmm, not sure i got this all right.

Buth i think this is what your looking for (in VBscript in your ASP page):


Response.Write("<script>validate('" & param & "');</script>")


result in your browser will be


<script>validate('param');</script>


Note : html-doublequotes become single quotes when you use them in an asp-string. doublequotes mark beginning and end of what will be wrritten to a browser as straight html-code

glenngv
10-11-2002, 11:25 AM
it will depend on what datatype of the parameter of the validate function is.

if it is a number, then:
<%
param = 1 'sample
Response.Write("<script>validate(" & param & ");</script>")
%>

The result will be:

<script>validate(1);</script>

if it is a string, then:
<%
param = "something" 'sample
Response.Write("<script>validate('" & param & "');</script>")
%>

The result will be:

<script>validate('something');</script>

kevinkan
10-11-2002, 05:19 PM
Hi Glenn,

I've tried yor method... but was thrown back wif a compilation error: "Operator '&' cannot be applied to operands of type 'string' and 'string' "

my javascript:
function Popup(msg)
{
alert(msg);

}

my code-behind:
string m="Registration completed successfully.";
Response.Write("<script>Popup('" & m & "');</script>");


Please advise.


Thanks,
Kevin
---------

glenngv
10-14-2002, 02:50 AM
i think you are using jscript not vbscript. if so, then:

string m="Registration completed successfully.";
Response.Write("<script>Popup('" + m + "');</script>");

kevinkan
10-14-2002, 03:32 AM
Hi Glenn,

I am indeed using jscript. Tried your suggested method... but still got this javascript error: "Expected )" .

What possibly could have went wrong??
Please advise.


Thanks,
Kevin
---------------


Originally posted by glenngv
i think you are using jscript not vbscript. if so, then:

string m="Registration completed successfully.";
Response.Write("<script>Popup('" + m + "');</script>");

glenngv
10-14-2002, 03:40 AM
could you post the javascript code generated by jscript by opening View-Source and copying that part?

kevinkan
10-14-2002, 04:24 AM
Hi Glenn,

this is what was generated right at the top of the page:
<script>Popup(Registration failed: MemberID already registered.);</script>

----------------


Originally posted by glenngv
could you post the javascript code generated by jscript by opening View-Source and copying that part?

glenngv
10-14-2002, 04:40 AM
you missed the single quotes!

Response.Write("<script>Popup('" + m + "');</script>");

remember, the output should be:
<script>Popup('Registration failed: MemberID already registered.');</script>

kevinkan
10-14-2002, 02:56 PM
Haha... u r rite Glenn... I've missed out the quotes indeed!!
My apologies....

But then again...
this is the error I get this time: "Object expected"

I guess it's due to the javascript which was preloaded. How can I resolve this??


Thanks,
Kevin
-----------


Originally posted by glenngv
you missed the single quotes!

Response.Write("<script>Popup('" + m + "');</script>");

remember, the output should be:
<script>Popup('Registration failed: MemberID already registered.');</script>

glenngv
10-15-2002, 03:20 AM
it means, it can't find the Popup() function. Where is it located? I suspect it is in an external js and you called that function while the js is not yet loaded.

kevinkan
10-15-2002, 03:34 AM
You are right, Glenn.
The javascript is indeed in an external js file, and is loaded within the <head> tags of the page.

I think I'm understanding now:
The Response.Write writes to the top of the page, and the external js file is loaded only after it... hence the Popup function cannot be located... am I rite??

So how shld I resolve this then?? Place the Response.Write after the <head> tags??

Pls advise.

Thanks,
Kevin
-------------

Originally posted by glenngv
it means, it can't find the Popup() function. Where is it located? I suspect it is in an external js and you called that function while the js is not yet loaded.

whammy
10-15-2002, 03:43 AM
Yes... you are right (I think).

Try to keep in mind that anything generated by the server is processed BEFORE it gets sent to the client's browser.

Therefore if you're going to use a server-side variable in a client-side language like js, it needs to ALREADY have a value before the user's webpage loads it.

I hope that makes sense... basically, the client's browser can NOT read from your server! So you need to send final values to the client - whether they are javascript, HTML, or whatever. ;)

kevinkan
10-15-2002, 05:32 AM
Hi Whammy,

So am I right to say that I shld either:
1) Response.Write at the end of the page

or

2) Load the javascript functions in the code-behind on Page_Load, and calling the functions as and when required??

Thanks,
Kevin
-----------

Originally posted by whammy
Yes... you are right (I think).

Try to keep in mind that anything generated by the server is processed BEFORE it gets sent to the client's browser.

Therefore if you're going to use a server-side variable in a client-side language like js, it needs to ALREADY have a value before the user's webpage loads it.

I hope that makes sense... basically, the client's browser can NOT read from your server! So you need to send final values to the client - whether they are javascript, HTML, or whatever. ;)

glenngv
10-15-2002, 06:01 AM
<head>
<script language="javascript" src="external.js"></script>
<%
string m="Registration completed successfully.";
Response.Write("<script>Popup('" + m + "');</script>");
%>
</head>

kevinkan
10-15-2002, 03:58 PM
HI Glenn,

I'm sorry, but I dun quite understand how to implement yor method...
Am I suppose to code the "Response.Write" in the .aspx page or the .cs ??

Pardon my ignorance.

Thanks,
Kevin
--------

Originally posted by glenngv
<head>
<script language="javascript" src="external.js"></script>
<%
string m="Registration completed successfully.";
Response.Write("<script>Popup('" + m + "');</script>");
%>
</head>

kevinkan
10-16-2002, 05:55 PM
Hi guys,

thanks for all yor help. I've finally solved the problem and achieved my requirement.
Basically, this is what I've done:

in my external .js:
function Popup(msg)
{
alert(msg);

}

in my .cs file:
string m = "Registration failed: MemberID already registered.";
String s = "<script>Popup('" + m + "');</script>";
Page.RegisterClientScriptBlock("Popup",s);

This way, I can actually call the javascript in my external .js file... :)
Thanks again!!

Cheers,
Kevin
-------------

kevinkan
10-16-2002, 06:07 PM
Hi guys,

I'm sorry but to trouble u guys again...
a similar problem to the previous one, but yet a little more complicated... Similarly, I want to reuse my javascripts from an external .js file. But this time, to validate the text input within a datalist.
This is my code:

in my .js file:
function valpro(c1,p1)
{
if (valblank(document.p1['c1'].lname,"Last Name")){
return false;
}
else
return true;
}

in my .cs file:
....
TextBox lname = (TextBox) e.Item.FindControl("lname");
ValEditProfile(lname);

public void ValEditProfile(Control c1)
{
Control p1 = c1.Parent;
while (p1.GetType() != typeof (System.Web.UI.HtmlControls.HtmlForm))
{
p1 = p1.Parent;
}

String s;
s = "<script>valpro('"+c1.ClientID+"','"+p1.UniqueID+"');</script>";
c1.Page.RegisterClientScriptBlock("ValProfile", s);
}

The error I've got was "Expected identifier" and "Object Expected".
Where did I go wrong this time??

Please advise.

Thanks,
Kevin
-----------

ksridhar69
10-16-2002, 08:56 PM
Use this you will get the alert when page loaded
<%
Response.Write "(<script>alert('"+"Registration Completed successfully"+"')</script>)"
%>

glenngv
10-17-2002, 03:34 AM
Originally posted by ksridhar69
Use this you will get the alert when page loaded
<%
Response.Write "(<script>alert('"+"Registration Completed successfully"+"')</script>)"
%>

maybe you meant like this:

<%
Response.Write("<script>alert('Registration Completed successfully')</script>")
%>

kevinkan,

you didnt post the valblank() function, maybe the error was there.
also in you valpro() function, remove the quotes in c1, it's a variable not a string:

function valpro(c1,p1)
{
if (valblank(document.p1[c1].lname,"Last Name")){
return false;
}
else
return true;
}

can you also post the generated code when the page is run?

kevinkan
10-17-2002, 05:14 PM
Hi Glenn,

I've amended the valpro() function as u've shown, but the error still persists...

Here's the portion of the generated code:
<script>valpro('profileDataList__ctl0_lname','profileform','profileDataList__ctl0_gname','profileform','prof ileDataList__ctl0_add1','profileform','profileDataList__ctl0_email','profileform','profileDataList__ ctl0_contact','profileform','profileDataList__ctl0_icqno','profileform');</script>

and this is the javascript valblank() function:
function valblank(field, s) {
if (Null(field.value) || Blank(field.value)){
field.focus()
field.select();
alert("The " + s + " field is blank.\nPlease re-enter.")
return true
}
else {
return false
}
}
function Null(str) {
if (str.length == 0 )
return true
else
return false
}
function Blank(str) {
for (i = 0; i < str.length; i++) {
if (str.charAt(i) != " ")
return false
}
return true
}

Thanks a million.

Cheers,
Kevin
-------------

Originally posted by glenngv


maybe you meant like this:

<%
Response.Write("<script>alert('Registration Completed successfully')</script>")
%>

kevinkan,

you didnt post the valblank() function, maybe the error was there.
also in you valpro() function, remove the quotes in c1, it's a variable not a string:

function valpro(c1,p1)
{
if (valblank(document.p1[c1].lname,"Last Name")){
return false;
}
else
return true;
}

can you also post the generated code when the page is run?

ksridhar69
10-17-2002, 10:17 PM
<%

Response.Write("<script>alert('" + Thankyou for submitting this form + "');</script>");
%>

or

<%

Response.Write("<script>alert('" + your shipping ID is: "& rs(0) &" + "');</script>");
%>

glenngv
10-18-2002, 03:18 AM
Originally posted by kevinkan
Hi Glenn,

I've amended the valpro() function as u've shown, but the error still persists...

Here's the portion of the generated code:
<script>valpro('profileDataList__ctl0_lname','profileform','profileDataList__ctl0_gname','profileform','prof ileDataList__ctl0_add1','profileform','profileDataList__ctl0_email','profileform','profileDataList__ ctl0_contact','profileform','profileDataList__ctl0_icqno','profileform');</script>


kevinkan, are the parameters in one line? and why do you have many parameters for the valpro() function when you declare it to only have 2?

Originally posted by ksridhar69
<%
Response.Write("<script>alert('" + Thankyou for submitting this form + "');</script>");
%>
or
<%
Response.Write("<script>alert('" + your shipping ID is: "& rs(0) &" + "');</script>");
%>


1st statement should be:

Response.Write("<script>alert('" + "Thankyou for submitting this form" + "');</script>");

or simply:

Response.Write("<script>alert('Thankyou for submitting this form');</script>");


2nd statement should be:

Response.Write("<script>alert('" + "your shipping ID is: " + rs(0) + "');</script>");

or simply:

Response.Write("<script>alert('your shipping ID is: " + rs(0) + "');</script>");

remember he is using jscript server-side, you don't use & as concatenation operator

kevinkan
10-18-2002, 04:07 AM
Opps.. I'm sorry...
the generated code shld be :
<script> valpro('profileDataList__ctl0_lname','profileform');</script>

and yes, they are in 1 line...

My apologies,
Kevin
-------------

Originally posted by glenngv


kevinkan, are the parameters in one line? and why do you have many parameters for the valpro() function when you declare it to only have 2?

glenngv
10-18-2002, 04:23 AM
could you post the link to the page?
or post the whole generated code.
it's hard to debug if I can't see the whole page.

kevinkan
10-19-2002, 09:15 AM
sure glenn.
you can login to this url: http://makers.homeip.net/sec4e
userid : glenn
password: password

Btw, this bug has caused the previously solved case (ie. the Popup() function alertbox) to fail too... :(

Thanks for all yor help.


Cheers,
Kevin
-----------

Originally posted by glenngv
could you post the link to the page?
or post the whole generated code.
it's hard to debug if I can't see the whole page.

glenngv
10-21-2002, 03:50 AM
you don't have 2 parameters for valpro() function! you indeed have 12!

function valproo(c1,p1,c2,p2,c3,p3,c4,p4,pc5,5,c6,p6)

you have a typo, you can't have variable name that starts with a number.

as i analyze the valproo function, you are passing the form names and its corresponding element names like this:

<script>valproo('profileDataList__ctl0_lname','profileform','profileDataList__ctl0_gname','profileform','pro fileDataList__ctl0_add1','profileform','profileDataList__ctl0_email','profileform','profileDataList_ _ctl0_contact','profileform','profileDataList__ctl0_icqno','profileform');</script>

you should modify the function like this:


function valproo(c1,p1,c2,p2,c3,p3,c4,p4,pc5,p5,c6,p6)
{
if (valblank(document.forms[p1].elements[c1],"Last Name")){
return false;
}
else if (valblank(document.forms[p2].elements[c2],"Given Name")){
return false;
}

//other codes here (like the pattern above)

}


and i saw your Popup() function, you are just calling alert().
why don't you just call alert() directly in your html pages?

kevinkan
10-21-2002, 04:09 PM
Hi Glenn,

thanks so much for yor consistent help...
I've made the changes accordingly... but still got this error " 'value' is null or not an object ", when I tried to leave the "last name" field empty. It's suppose to popup the alertbox...

at the same time, how can I make the javascript return a boolean result so as to tell the caller of the function whether the validation was done correctly?? ie:

public void functionA ()
{
...
boolean edit_ok = ValEditProfile(lname,gname,add1,email,contact,icqno);
..
}

public void ValEditProfile(Control c1.... )
{
Control p1 = c1.Parent;
...
while (p1.GetType() != typeof (System.Web.UI.HtmlControls.HtmlForm))
{
p1 = p1.Parent;
....
}

String s;
s = "<script>valpro('"+c1.ClientID+"','"+p1.UniqueID+"');</script>";
c1.Page.RegisterClientScriptBlock("ValProfile", s);
}


such that "edit_ok" can know if the validation is done properly or not??

Please advise.
And so sorry to trouble u again. :)

Cheers,
Kevin
-----------

glenngv
10-22-2002, 03:11 AM
regarding the javascript error, i forgot to tell you that you should pass in the valproo() the element names not the ids.

this is not a javascript function, it's a server-side code called C#

public Boolean ValEditProfile(Control c1.... )
{
Control p1 = c1.Parent;
...
while (p1.GetType() != typeof (System.Web.UI.HtmlControls.HtmlForm))
{
p1 = p1.Parent;
....
}

//somewhere in this function you should return true/false;
return true;
}

don't know if the syntax is correct, i haven't work with C#

kevinkan
10-22-2002, 03:26 AM
Pardon my ignorance....
But does that means I shld be using this instead?

s = "<script>valproo('"+profileform+"','"+lname+"'+'"+profileform+"','"+gname+"'.......);</script>";
c1.Page.RegisterClientScriptBlock("ValProfile", s);

This will work even in a datalist??
Thanks.

Cheers,
Kevin
-----------

Originally posted by glenngv
regarding the javascript error, i forgot to tell you that you should pass in the valproo() the element names not the ids.

this is not a javascript function, it's a server-side code called C#

public Boolean ValEditProfile(Control c1.... )
{
Control p1 = c1.Parent;
...
while (p1.GetType() != typeof (System.Web.UI.HtmlControls.HtmlForm))
{
p1 = p1.Parent;
....
}

//somewhere in this function you should return true/false;
return true;
}

don't know if the syntax is correct, i haven't work with C#

glenngv
10-22-2002, 03:55 AM
if those variables pertain to the element names, then you should use that. try it.

kevinkan
10-22-2002, 03:34 PM
Hi Glenn,

tried passing in the element names as u've said... but the error : " 'value' is null or not an object" still persists...

I've tried calling the javascript function as such:
s = "<script>valproo('profileDataList__ctl0_lname','profileform','profileDataList__ctl0_gname','profileform','pro fileDataList__ctl0_add1','profileform','profileDataList__ctl0_email','profileform','profileDataList_ _ctl0_contact','profileform','profileDataList__ctl0_icqno','profileform');</script>";

but to no avail....
so what went wrong this time??
My website is still available wif the same userid and password.

Thanks a million,
Kevin
-------------

Originally posted by glenngv
if those variables pertain to the element names, then you should use that. try it.

glenngv
10-23-2002, 03:42 AM
you are still passing the id not the name!

you have this:

<script>valproo('profileDataList__ctl0_lname', ...);</script>

<input name="profileDataList:_ctl0:lname" type="text" value="AA" maxlength="15" id="profileDataList__ctl0_lname" class="ftext" style="width:155px;" />

you should have passed profileDataList:_ctl0:lname instead.

kevinkan
10-23-2002, 03:54 AM
oh gosh.. silly me...
I'm sooo sorry, glenn...

Will try it again when I get home tonite...
Thks for the enlightenment :)

Cheers,
Kevin
------------

Originally posted by glenngv
you are still passing the id not the name!

you have this:

<script>valproo('profileDataList__ctl0_lname', ...);</script>

<input name="profileDataList:_ctl0:lname" type="text" value="AA" maxlength="15" id="profileDataList__ctl0_lname" class="ftext" style="width:155px;" />

you should have passed profileDataList:_ctl0:lname instead.

kevinkan
10-23-2002, 05:33 PM
Hi glenn,

the same problem with the same error message still persists....
despite following yor advises....
am really lost now...

Pls advise...

Thanks,
Kevin
----------

aerton
05-16-2003, 10:43 PM
Try Team Remote ASP Debugger (http://www.remotedebugger.com ).

Much better than response.write logic, IMHO.

Good luck.

raf
05-18-2003, 04:55 PM
Welcome here.


:confused:
Please clarify