Cancel the postback using javascript ?

Cancel the postback using javascript ?

Post by Franci » Sat, 06 Apr 2002 04:16:55



I have a ASP.NET server-side button on my page. I want to write a
client-side onClick handler in javascript to determine whether to do the
post-back when the user click the server-side button. Is there a way to
cancel the post-back action in javascript ?

Thank you.

 
 
 

Cancel the postback using javascript ?

Post by Joe Kin » Sat, 06 Apr 2002 04:55:01


Is this an INPUT 'Submit' type or an INPUT 'Button' type?

If it is a 'Submit' type, the information at this link should help:

http://www.aspalliance.com/aldotnet/examples/cd.aspx

However, if it is a 'Button' type and uses the __doPostBack() javascript
mechanism for firing the event, then here is some info that should help.

This server code:

<input type="button" value="OK" onserverclick="button_onclick"
onclick="alert('hello word');" runat="server">

Will result in this HTML output:

<input language="javascript" onclick="alert('hello world');
__doPostBack('_ctl0', '')" name="_ctl0" type="button" value="OK" />

As you can see, the client-side OnClick event sequence places the
__doPostBack() function last.  This allows you to perform whatever
statements are required leading up to the PostBack.

Here is what you want to do if you need to confirm with the user to
undertake an action, which conditionally causes the PostBack to occur:

<input type="button" value="OK" onserverclick="button_onclick"
onclick="if(!confirm('Fire PostBack Event?')){return false;}"
runat="server">

A confirmation dialog will appear to the client.  The PostBack either occurs
or is cancelled based on the choice made.

Examining the HTML code and sequence of events at the client should allow
you to conditionally raise a PostBack event with any of the HTML Controls.

Regards,

Joseph King
Coalesys, Inc.
http://www.coalesys.com/
ASP.NET Navigation Toolkit
Microsoft Tech-Ed 2002, booth #739


Quote:> I have a ASP.NET server-side button on my page. I want to write a
> client-side onClick handler in javascript to determine whether to do the
> post-back when the user click the server-side button. Is there a way to
> cancel the post-back action in javascript ?

> Thank you.


 
 
 

Cancel the postback using javascript ?

Post by McGi » Sat, 06 Apr 2002 09:27:28


Add an onClick attribute to the button which calls a clientside JavaScript
function

this.cmdButton.Attributes.Add("onClick","return doCallBack();");

and have a function to determine whether or not the call-back is required
returning true or false.

<script language=JavaScript>

function doCallBack(){

    if (callBackRequired)

            return true;

    else

            return false;

    }

</script>

Note that if you do not include the return keyword before the function name
the submit (call-back) cannot be cancelled.


Quote:> I have a ASP.NET server-side button on my page. I want to write a
> client-side onClick handler in javascript to determine whether to do the
> post-back when the user click the server-side button. Is there a way to
> cancel the post-back action in javascript ?

> Thank you.

 
 
 

1. Postback problem when using Javascript???

Hi everybody,

I have the following drop down lists:

<td valign="middle">
     <asp:DropDownList CssClass="normal" runat="server" id="Level01DDL"
width="160" Enabled="true" onchange="CategAndManuf(this, document.all.item
('Level02DDL'), document.all.item('ManufDDL'),
document.all.item('categoriesHidden'),
document.all.item('subCategoriesHidden'), document.all.item
('subCategoriesManHidden'));"/>
</td>
<td valign="middle">
     <asp:DropDownList id="Level02DDL" CssClass="normal" width="160"
Enabled="true" AutoPostBack="true" OnSelectedIndexChanged="FetchLevel3"
runat="server"/>
</td>
<td valign="middle">
    <asp:DropDownList id="Level03DDL" CssClass="normal" width="160"
Enabled="true" AutoPostBack="true" OnSelectedIndexChanged="AdjustManufDDL"
runat="server"/>
</td>
<td valign="middle">
    <asp:DropDownList id="ManufDDL" runat="server" CssClass="normal"
width="160" Enabled="true" onchange="ManufAndCateg(this, document.all.item
('Level01DDL'),document.all.item('Level02DDL'),document.all.item('categories
Hidden'));" />
</td>

I use Javascript to filter Level02DDL and ManufDDL when Level01DDL changes
(client side)...
Javascript functions work just fine....but AutoPostBack for Level02DDL does
execute function FetchLevel3 which is implemenent in a dll (i use the
debbuger).
When i DO NOT use Javascript and every filtering in drop downs occurs with
AutoPostBacks, FetchLevel3 is executed normally....

Does client scripting is aspx files causes problems (while it shouldn't)?

Thanxs in advance
Katsikas Dimitris

2. Test web site error

3. PostBack, LinkButton, delete using JavaScript

4. TOS 2.05 loader?

5. Cancel postback from datagrid ButtonColumn

6. Please HELP me.....

7. Cancel PostBack

8. the fact is that VB has limits

9. my javascript window defaults to cancel when hit enter

10. e.cancel does not supress/cancel the remaining events in validating event.

11. problem executing javascript on postback

12. Javascript message before postback?

13. web client postback from javascript