Cancel postback (conditionally)

L

Leon

Hello everybody.

OnClientClick of the button I am running javascript code to check some
condition. When that condition is not met I want to suppress postback. Even
though I return false from javascript web page still gets reloaded. What
should I do?
 
N

Nathan Sokalski

I would take a look at all the controls on your page that can cause
postbacks. Here are some things to check:

1. Do a debug and see what control is actually triggering the postback
2. Do a view source to see where the JavaScript code that actually does the
postback is really located

We may be able to help you more if you could send us your code. Good luck!
 
L

Leon

Hello Nathan.

I believe that button is causing postback and it's supposed to cause
postback, but I want to cancel postbacks depending on some condition. That's
why I wrote some javascript to run some client side code and depending on
some condition, cancel form postback.
 
N

Nathan Sokalski

When you do a view source, in the onclick event, is __doPostBack called
before or after you return false? Something you may want to try is throwing
a window.alert in to see when it does the postback. If I could see your code
it may help.
 
J

James Parker

Hello everybody.

OnClientClick of the button I am running javascript code to check some
condition. When that condition is not met I want to suppress postback. Even
though I return false from javascript web page still gets reloaded. What
should I do?
The buttons on client click should look something like this.

OnClientClick="return Validate();"

I have had issues in the past where i not returned the return value
from the javascript validation method.

Hope this helps
 
L

Leon

In Page_Load:
cmdSearch.OnClientClick = "ValidSearch('S')"

That's my javascript:

<script type="text/javascript">
function ValidSearch(tcLang) {
var cMessage = '';
var myTextField = document.getElementById('<%= txtSearch.ClientID %>');
if (myTextField != null){
var sSearchStr = AllTrim(myTextField.value);
if (sSearchStr.length == 1) {
if (tcLang == "S")
cMessage = 'La búsqueda requiere 2 o mas caracteres';
else
cMessage = 'Search requires 2 or more characters';

alert(cMessage);
}
}
return false;
}
function AllTrim(tcStr) {
return tcStr.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
</script>

The other thing that might matter - all this code in the master page.
 
N

Nathan Sokalski

Try changing your code to the following:

cmdSearch.OnClientClick = "return ValidSearch('S')"

Without including the keyword return as I did above, your code is basically
the same as the following:

cmdSearch.OnClientClick = "false"

See if this helps.
 
L

Leon

That was it. Thank you very much.

Nathan Sokalski said:
Try changing your code to the following:

cmdSearch.OnClientClick = "return ValidSearch('S')"

Without including the keyword return as I did above, your code is basically
the same as the following:

cmdSearch.OnClientClick = "false"

See if this helps.
 
L

Leon

Yes, that was exactly my problem. Thanks a lot.

James Parker said:
The buttons on client click should look something like this.

OnClientClick="return Validate();"

I have had issues in the past where i not returned the return value
from the javascript validation method.

Hope this helps
 
G

Gregory A. Beamer

Leon said:
Hello everybody.

OnClientClick of the button I am running javascript code to check some
condition. When that condition is not met I want to suppress postback.
Even
though I return false from javascript web page still gets reloaded. What
should I do?

Check the HTML output and make sure it is respecting the false return.
False, by itself, does not solve the problem. The normal method here is use
OnClientClick, as you have stated, so make sure you are getting the proper
output. You might also want to go into debug and make sure sender is the
button in question and not something else that is firing.

I am getting more and more fond of JQuery in my apps, which works very well
with AJAX. It has a nice conditional submit mechanism.

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
| Think outside the box! |
************************************************
 

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