Run jQuery function on PostBack (ASP.NET)

I have a form that is initially hidden through jQuery, and when I click the button, two radio buttons appear (also hidden initially through jQuery). When you click one switch, the user is redirected to another page (this works fine). When you click another switch, the "form" is again visible, through jQuery.

My problem arises when the field inside the "form" is checked on the server side during submission, and the page reloads with a validation error message, but the "form" is now hidden (according to the initial jQuery below).

How can I make the form visible in postback? (I already tried ASP panels and AJAX UpdatePanel to no avail.)

** This is my jQuery: **

// Reveal Radio Fields $(".btn-leavecomment, .txt-leavecomment").toggle(function(){ $("#commenttype").stop().animate({ down: "+=300" }, 3000) $("#commenttype").stop().slideDown("slow"); }, function(){ $("#commenttype").stop().animate({ down: "-=300" }, 1400) $("#commenttype").stop().slideUp("slow"); }); // Reveal Form on-click of one radio field in particular $(".reveal_ccform").toggle(function(){ $("#ccform_container").stop().animate({ down: "+=300" }, 4000) $("#ccform_container").stop().slideDown("slow:4000"); }, function(){ $("#ccform_container").stop().animate({ down: "-=300" }, 4000) $("#ccform_container").stop().slideUp("slow:4000"); }); 

A recently added JavaScript implementation (as suggested by Moar), this still doesn't work, any ideas? :(:

JavaScript:

 <script type="text/javascript"> $(document).ready() { function isPostBack() { if (!document.getElementById('clientSideIsPostBack')) { return false; if (document.getElementById('clientSideIsPostBack').value == 'Y' ) return true; } // Reveal Comment Type $(".btn-leavecomment, .txt-leavecomment").toggle(function () { $("#commenttype").stop().animate({ down: "+=300" }, 3000) $("#commenttype").stop().slideDown("slow"); }, function () { $("#commenttype").stop().animate({ down: "-=300" }, 1400) $("#commenttype").stop().slideUp("slow"); }); // Reveal Sign Guestbook Form $(".reveal_ccform").toggle(function () { $("#ccform_container").stop().animate({ down: "+=300" }, 4000) $("#ccform_container").stop().slideDown("slow:4000"); }, function () { $("#ccform_container").stop().animate({ down: "-=300" }, 4000) $("#ccform_container").stop().slideUp("slow:4000"); }); // Hide 'Leave a Comment' button and 'Comment Type' div $('.reveal_ccform').click(function () { $(".btn-leavecomment").stop().fadeOut("slow:1500"), $('#commenttype').slideUp("slow:8000"); }); } } </script> 

FROM#:

 if (Page.IsPostBack) { Page.ClientScript.RegisterStartupScript(GetType(), "IsPostBack", script, true); //Second part of code will run if is postback = true ClientScriptManager cs = Page.ClientScript; Type csType = this.GetType(); cs.RegisterClientScriptBlock(csType, "openForms", "$(document).ready(openForms);", true); } 
+8
jquery postback
source share
3 answers

This may seem rather annoying, but for that

I would use hidden values. Let's say when you open the form, update the hidden value to 1,

HTML page

 input type="hidden" id="hiddenFormOpen" value="1" runat="server" 

C # codebehind

 if (IsPostBack) ClientScriptManager.RegisterClientScriptBlock(this.GetType, "myFunction", "openForm("+hiddenFormOpen+")", true); 

Javascript function

 function openForm (val) { if (val ==1) { Update form visibility here } } 

The second option is to avoid sending server mail again and use jQuery to create a fully client base, therefore eliminating the postback

+5
source share

I would recommend posting all the javascript that you showed in the function. For this example, suppose you put it in a function called

 myFunction(); 

Then, in the code, put this in one of the page events (I would recommend page_load)

 if (IsPostBack) ClientScriptManager.RegisterClientScriptBlock(this.GetType, "myFunction", "$(document).ready(myFunction);", true); 

This will add javascript to the page that will run your function only if the page is a post back.

+3
source share

When the postback occurs, any changes you make to the page through jQuery will be lost.

I'm not sure if you are using UpdatePanels, but in this case there are additional problems. Read the following: jQuery $ (document) .ready and UpdatePanels?

If you are not using UpdatePanels, I would say that you just need to make the view visible on the server side during the postback (form.visible).

The asp.net web form model is really useful, however it definitely does not require discussion with jQuery. In my opinion, if you want to get the most out of the type of web 2.0, you might consider getting away from using postbacks / updatepanels and doing more with web services, etc. Personally, it definitely pulls me inexorably towards MVC.

+1
source share

All Articles