By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,222 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,222 IT Pros & Developers. It's quick & easy.

Return value from js function

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.