Cancel Postback for ASP.Net Button

G

Guest

I read the 4 or 5 threads on this Subject and it seems my javasript should
work but it doesn't. In the designer, the onClientClick is set to:
CheckForSave(this). This works in .Net 1.1 but not 2.0. The User is
presented with an OK or Cancel confirmation window. However, pressing Cancel
does not result in the button's post back being cancelled. In the meantime,
I'll try the other suggested methods.

Here is my javscript:

function CheckForSave(thisButton) {

if (confirm("Appointment Will Be Saved. Check Current Appointment box and
make sure it is correct. If so, you may navigate away or close your
browser.")==true) {
return true;
}
else {
return false;
}
}
 
S

Scott M.

The button itself needs to have an onclick event handler that reads like
this:

onclick="return CheckForSave(this)"

Just returning true or false from the function isn't enough, you need to
have that return value itself returned to the onclick event handler.
 
G

Guest

In the InitializeComponent Sub which VS2005 creates for you when addinh
components to the Component Designer, I just needed to add this line:

Me.btnNewAppt.Attributes.Add("onclick", "return CheckForSave(this);")

This answers the question why it works in 1.1 but not 2.0.

Which begs the questions:

Which event is the best event to call this Initialize Component?

Why does 2.0 work this way? Is adding components not the preferred way
anymore?

WR
 
G

Guest

Thanks for the quick reply. See my 2nd post. I still do not understand why
setting the onClientClick in the designer does not work. Isn't that the
purpose of it?
 
G

Guest

the designer works fine if you follow directions:

onclientclick="return CheckForSave(this);"

would work as well as setting the onclick attribute (as thats all
onclientclick does).

why you need the retun:

whent the browsers sees onclick="CheckForSave(this)" it generates an
anonymous function to execute the code and assigns as the click handler. when
it executes the click handler it checks the return value of the anonymous
function:

// returns void
button.click = function(){ CheckForSave(this); }

// returns function result
button.click = function(){ return CheckForSave(this); }










-- bruce (sqlwork.com)
 
S

Scott M.

The instructions discuss how to add an event handler to a piece of client
side code, but adding the extra "return" is not really related to adding
event handlers, it's something you've just got to know about JavaScript.

If you look at the description of the help topic "Gets or sets the
client-side script that executes when a Button control's Click event is
raised.", you'll note that it doesn't say "...and how to cancel an event
that has already been triggered." - - That's a completely different issue.

-Scott


WhiskeyRomeo said:
That make sense but . . .
maybe someone has to represent the 2% -- here are the instructions I have:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx

It says put the name of the javacript function to be called not "return
[name of javascript function]" One has to scroll all the way down to the
last commient in Community Content section to see those "directions."
wr

bruce barker (sqlwork.com) said:
the designer works fine if you follow directions:

onclientclick="return CheckForSave(this);"

would work as well as setting the onclick attribute (as thats all
onclientclick does).

why you need the retun:

whent the browsers sees onclick="CheckForSave(this)" it generates an
anonymous function to execute the code and assigns as the click handler.
when
it executes the click handler it checks the return value of the anonymous
function:

// returns void
button.click = function(){ CheckForSave(this); }

// returns function result
button.click = function(){ return CheckForSave(this); }










-- bruce (sqlwork.com)
 

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

Top