79949

how to prevent postback using jquery

Question:

$(document).ready(function () { $("#Div_1").show(); $("#Div_2").hide(); $('#Buttion1').click(function () { $("#Div_1").hide(); $("#Div_2").show(); }); });

in the above code, while loading is working but when click (Buttion1 is ASP.Net Server button) on button Div_2 not showing off and Div_1 not hiding off

Answer1:

$('#Buttion1').click(function () { $("#Div_1").hide(); $("#Div_2").show(); return false;

});

Answer2:

Here is the correct way to do it.

Eg: <a href="http://jsfiddle.net/vtLMT/" rel="nofollow">http://jsfiddle.net/vtLMT/</a>

HTML

<div id="div1">Div1</div> <div id="div2">Div2</div> <button id="btn">Click Me</button>

CSS

#div1 { } #div2 { display: none; }

JQUERY

$(document).ready(function() { $("#btn").click(function() { $("#div1").hide(); $("#div2").show(); return false; }); });

Answer3:

try this

$(document).ready(function () { $("#Div_1").show(); $("#Div_2").hide(); $('#Buttion1').click(function () { $("#Div_1").hide(); $("#Div_2").show(); return false; }); });

or

<asp:UpdatePanel runat="server"> <ContentTemplate> <asp:Button Text="text" ID="Buttion1" OnClientClick="hideshow();" runat="server" OnClick="Buttion1_Click" /> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Buttion1" EventName="Click" /> </Triggers> </asp:UpdatePanel> <div id="Div_1"> </div> <div id="Div_2"> </div> <script type="text/javascript"> $(document).ready(function () { $(document).ready(function () { $("#Div_1").show(); $("#Div_2").hide(); }); }) function hideshow() { $("#Div_1").hide(); $("#Div_2").show(); return true; } </script>

server side code

protected void Buttion1_Click(object sender, EventArgs e) { //server side code }

note:

1)your two div must be out side updatepanel.

2)your hideshow function must not be undefined.Place function on at the en of the page

3)on button client click call hideshow function which return true

4) after javascript function return true..it goes for the server side event

Recommend

  • Is there a way to clone native functions in javascript like window.alert or document.write
  • cell spacing in div table
  • Web.config system.webserver errors
  • How to clear text inside text field when radio button is select
  • FileReader+canvas image loading problem
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Counter field in MS Access, how to generate?
  • Get object from AWS S3 as a stream
  • Cross-Platform Protobuf Serialization
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to check if every primary key value is being referenced as foreign key in another table
  • Validaiting emails with Net.Mail MailAddress
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • jquery mobile loadPage not working
  • Do I've to free mysql result after storing it?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Python - Map / Reduce - How do I read JSON specific field in using DISCO count words example
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • 0x202A in filename: Why?
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How to stop GridView from loading again when I press back button?
  • File not found error Google Drive API
  • Turn off referential integrity in Derby? is it possible?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • JaxB to read class hierarchy
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal