I’m having a difficult time getting around AutoPostBack with ASP.NET components, and was hoping someone can help. I’ve looked through multiple queries but nothing is really helping.
I have a ‘Submit’ button on my page that when clicked should do 2 things:
1. Run a server-side C# function that updates a bunch of <asp:Labels in a div tag.
2. Run a javascript animation that moves stuff around and makes the <div tag visible.
My functions work just fine by themselves, however my issue is with autopostback. If I use an <asp:Button the postback refreshes and the javascript animation is undone along with it. If I use an <input type="button" tag I am unable to run both the C# and Javascript functions. I’ve tried the following:
-
<input type="button" runat="server" onclick="c#function" onclientclick="javascriptFunction(); return false;"
** runat=”server” seems to just enable postback on that button -
<asp:Button
** C# function uses Page.ClientScript.RegisterStartupScript to invoke javascript function, but postback defeats the animation.
I’m hoping to keep the javascript animations as they make the interface very clean and intuitive, so any help in keeping it is greatly appreciated. Any suggestions?
You may use ajax. You can pass values to the server page thru ajax and let the server page handles (insert a record in table / send an email to someone / whatever….) it for you and return a result to you. Till you get your result. you can show a “Processing…” Message to the user.
Here is a plugin to have the block effect.
http://jquery.malsup.com/block/
The below is a small example of how to do it in ajax (with jquery)
HTML
and you should have a page called myajaxserverpage.aspx which returns the content (ex: A list of products) for the div called “divProducts”
What the above code does is , When the user click on the button, it shows the text “Processing your request…” in the div and then make an ajax call to the aspx page.Once it receives the response, it put that to the div and then do a fadeIn effect.