473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Return value from js function

Hello,
I have a javascript function for a validation in the HTML page of the
asp.Net page..
I call this function in a Savebutton click

When the validation fails No postback should happen ( ie Save should not
happen)
the js function is as
function IsValidAmount()
{

if (condition valid)
{return true; }
else
{return false;}
}
I had called the function in the html code as

onclick="IsValidAmount();"

but how should I get the return value here so that I can prevent the posback.
I guess it can be done somehow if I could call the script in the code
behind....

Any idea will be highly appreciated..

cheers,
siaj
Nov 19 '05 #1
5 3952
Siaj:
Just wanna make sure that you are adding the onClick event via:

someButon.Attributes.Add("onclick", "IsValidAmount();");
instead of

<asp:button id="someButton" runat="Server" onClick="IsValidAmount();" />

because in this last example it'll look for the server-side OnClick event.

anyways, change it to be:

someButon.Attributes.Add("onclick", "return IsValidAmount();");
add the "return" in there...so it'll return false; and cancel the event..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"siaj" <si**@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Hello,
I have a javascript function for a validation in the HTML page of the
asp.Net page..
I call this function in a Savebutton click

When the validation fails No postback should happen ( ie Save should not
happen)
the js function is as
function IsValidAmount()
{

if (condition valid)
{return true; }
else
{return false;}
}
I had called the function in the html code as

onclick="IsValidAmount();"

but how should I get the return value here so that I can prevent the posback. I guess it can be done somehow if I could call the script in the code
behind....

Any idea will be highly appreciated..

cheers,
siaj

Nov 19 '05 #2
Here's some server side code that uses javascript to display a confirmation
message.

myDeleteButton.Attributes.Add("onclick", _
"return confirm('Are you sure you want to delete?');")

In this example, the Delete button will post back only if the person
confirms they want to delete. Otherwise your server code is never called in
response to the button click.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"siaj" <si**@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Hello,
I have a javascript function for a validation in the HTML page of the
asp.Net page..
I call this function in a Savebutton click

When the validation fails No postback should happen ( ie Save should not
happen)
the js function is as
function IsValidAmount()
{

if (condition valid)
{return true; }
else
{return false;}
}
I had called the function in the html code as

onclick="IsValidAmount();"

but how should I get the return value here so that I can prevent the
posback.
I guess it can be done somehow if I could call the script in the code
behind....

Any idea will be highly appreciated..

cheers,
siaj

Nov 19 '05 #3
Thanks for the suggestion...

I moved my code to the code behind in the page load event as
btnSaveContiniue.Attributes.Add("onclick", "return IsValidAmount();")

My javascript is as

function IsValidAmount()
{
if (condition valid)
{
document.forms[0].submit();
return true; }
else
{return false;}
}
My HTML code for button looks as
<INPUT style="type="button" value="Save and Continue" id="btnSaveContiniue"
name="Button1" runat="server" onclick = btnSaveContiniue_ServerClick >

The buttton click which I wnat to call on successful validation is
Private Sub btnSaveContiniue_ServerClick(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Try
Response.Write("Hello")
Catch ex As Exception
Response.Write(ex.Message)
End If
End Try
End Sub

--------------------
Now issue I am facing is that java validation is getting called properly (I
stepped thru it) but the sub btnSaveContiniue_ServerClick is never getting
called irrespective of succesfull or failed validation.
I am new to web dev so please pardon my ignorance for something silly....

cheers,
siaj

"Steve C. Orr [MVP, MCSD]" wrote:
Here's some server side code that uses javascript to display a confirmation
message.

myDeleteButton.Attributes.Add("onclick", _
"return confirm('Are you sure you want to delete?');")

In this example, the Delete button will post back only if the person
confirms they want to delete. Otherwise your server code is never called in
response to the button click.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"siaj" <si**@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Hello,
I have a javascript function for a validation in the HTML page of the
asp.Net page..
I call this function in a Savebutton click

When the validation fails No postback should happen ( ie Save should not
happen)
the js function is as
function IsValidAmount()
{

if (condition valid)
{return true; }
else
{return false;}
}
I had called the function in the html code as

onclick="IsValidAmount();"

but how should I get the return value here so that I can prevent the
posback.
I guess it can be done somehow if I could call the script in the code
behind....

Any idea will be highly appreciated..

cheers,
siaj


Nov 19 '05 #4
I'm surprised the JIT compiles....Private Sub btnSaveContiniue_ServerClick
should be Protected instead or private...

also, don't documnet.forms[0].submit(); just return true this is why you
aren't getting the button click event...because you are no longer clicking
the button to submit the form, you are submitting it yourself via
javascript...
Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"siaj" <si**@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
Thanks for the suggestion...

I moved my code to the code behind in the page load event as
btnSaveContiniue.Attributes.Add("onclick", "return IsValidAmount();")

My javascript is as

function IsValidAmount()
{
if (condition valid)
{
document.forms[0].submit();
return true; }
else
{return false;}
}
My HTML code for button looks as
<INPUT style="type="button" value="Save and Continue" id="btnSaveContiniue" name="Button1" runat="server" onclick = btnSaveContiniue_ServerClick >

The buttton click which I wnat to call on successful validation is
Private Sub btnSaveContiniue_ServerClick(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Try
Response.Write("Hello")
Catch ex As Exception
Response.Write(ex.Message)
End If
End Try
End Sub

--------------------
Now issue I am facing is that java validation is getting called properly (I stepped thru it) but the sub btnSaveContiniue_ServerClick is never getting called irrespective of succesfull or failed validation.
I am new to web dev so please pardon my ignorance for something silly....

cheers,
siaj

"Steve C. Orr [MVP, MCSD]" wrote:
Here's some server side code that uses javascript to display a confirmation message.

myDeleteButton.Attributes.Add("onclick", _
"return confirm('Are you sure you want to delete?');")

In this example, the Delete button will post back only if the person
confirms they want to delete. Otherwise your server code is never called in response to the button click.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"siaj" <si**@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Hello,
I have a javascript function for a validation in the HTML page of the
asp.Net page..
I call this function in a Savebutton click

When the validation fails No postback should happen ( ie Save should not happen)
the js function is as
function IsValidAmount()
{

if (condition valid)
{return true; }
else
{return false;}
}
I had called the function in the html code as

onclick="IsValidAmount();"

but how should I get the return value here so that I can prevent the
posback.
I guess it can be done somehow if I could call the script in the code
behind....

Any idea will be highly appreciated..

cheers,
siaj


Nov 19 '05 #5
thanks for ur help but I am sorry even removing the
documnet.forms[0].submit(); does not helps. Cleck event is never fired.. I
believe the post back is not happening irrespective of the js function
returns true or false.

Cheers,
siaj

"Karl Seguin" wrote:
I'm surprised the JIT compiles....Private Sub btnSaveContiniue_ServerClick
should be Protected instead or private...

also, don't documnet.forms[0].submit(); just return true this is why you
aren't getting the button click event...because you are no longer clicking
the button to submit the form, you are submitting it yourself via
javascript...
Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"siaj" <si**@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
Thanks for the suggestion...

I moved my code to the code behind in the page load event as
btnSaveContiniue.Attributes.Add("onclick", "return IsValidAmount();")

My javascript is as

function IsValidAmount()
{
if (condition valid)
{
document.forms[0].submit();
return true; }
else
{return false;}
}
My HTML code for button looks as
<INPUT style="type="button" value="Save and Continue"

id="btnSaveContiniue"
name="Button1" runat="server" onclick = btnSaveContiniue_ServerClick >

The buttton click which I wnat to call on successful validation is
Private Sub btnSaveContiniue_ServerClick(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Try
Response.Write("Hello")
Catch ex As Exception
Response.Write(ex.Message)
End If
End Try
End Sub

--------------------
Now issue I am facing is that java validation is getting called properly

(I
stepped thru it) but the sub btnSaveContiniue_ServerClick is never

getting
called irrespective of succesfull or failed validation.
I am new to web dev so please pardon my ignorance for something silly....

cheers,
siaj

"Steve C. Orr [MVP, MCSD]" wrote:
Here's some server side code that uses javascript to display a confirmation message.

myDeleteButton.Attributes.Add("onclick", _
"return confirm('Are you sure you want to delete?');")

In this example, the Delete button will post back only if the person
confirms they want to delete. Otherwise your server code is never called in response to the button click.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"siaj" <si**@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
> Hello,
> I have a javascript function for a validation in the HTML page of the
> asp.Net page..
> I call this function in a Savebutton click
>
> When the validation fails No postback should happen ( ie Save should not > happen)
> the js function is as
> function IsValidAmount()
> {
>
> if (condition valid)
> {return true; }
> else
> {return false;}
> }
> I had called the function in the html code as
>
> onclick="IsValidAmount();"
>
> but how should I get the return value here so that I can prevent the
> posback.
> I guess it can be done somehow if I could call the script in the code
> behind....
>
> Any idea will be highly appreciated..
>
> cheers,
> siaj


Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: strout | last post by:
function F(e) { return function(){P(e)} } Can anybody tell me what the code is doing? If return another function all in a function I would do function F(e)
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
3
by: tshad | last post by:
I am trying to set up a class to handle my database accesses. I can't seem to figure out how to get the return value from my dataReader from these routines (most of which I got elsewhere). They...
5
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.