Return value from js function

G

Guest

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
 
K

Karl Seguin

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
 
S

Steve C. Orr [MVP, MCSD]

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.
 
G

Guest

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
 
K

Karl Seguin

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 said:
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 said:
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.
 
G

Guest

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 said:
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 said:
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 said:
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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top