tags:

views:

39

answers:

4

Here is my code

<div id="AuthenticateUser" runat="server" style="display:block" >
   //login control
   <asp:button id="ValidateUser" ... />
</div>

<div id="ForceToChangePassword" runat="server" style="display:none" >
  //reset password control
</div>

On click of "ValidateUser" button, I do check whether user is valid or not. Based on some condition I do need hide "AuthenticateUser" div tag and show "ForceToChangePassword" div tag.
I really like the jQuery fadeIn/fadeOut effect. I can easily do that on client side. But how can I give that effect after executing server side code?

I tried to call javascript method from code behind that method has a fadeIn/fadeOut logic but it seems like that javaScript method is never been called.

+2  A: 

The best way is to replace your asp:button (which I'm assuming is doing a postback) with an html button and some javascript to make an AJAX call (or JSON & REST call).

jQuery supports REST services really well with their .ajax method.

http://api.jquery.com/category/ajax/

WCF also supports JSON & REST services very nicely.

http://www.west-wind.com/weblog/posts/324917.aspx

It's a match made in heaven.

When the web service call completes, a javascript method (specified in the jquery .ajax call) will get called, and you can do the fade at that point.

Steve
A: 

You need to use an AJAX call for something like this. http://api.jquery.com/category/ajax/

Basically, the javascript will send your login request to your serverside code. The serverside will send the response back (you can decide what kind of response).

Then use that response to determine if you should be fading in or not.

$.post('login.aspx', {credentials (hopefully encrypted)}, callbackFunction(dataFromServer){
    if(dataFromServer == loggedin)
          fadein
    else
          don't
});
idrumgood
You'd want to send credentials as a POST, not GET... with GET (even though they're encrypted) they'll remain in all sorts of logs, caches and what not. Otherwise right architecture.
Jaanus
Good point. Updated to reflect.
idrumgood
A: 

If you make any ajax request then you must have to call the function which handling fadeIn/fadeOut logic, within the response callback function. you have to do something like

$.ajax({
   url: "url-to-backend",
   success: function(msg){
     changeDiv();
   }
 });
function changeDiv()
{
   //your code to handle fadeIn/fadeOut logic;
}

Thanks

Muhit
+1  A: 

If you have the AJAX extensions, put this in the button event handler. (C# example, easily converted to vb.net)

ScriptManager.RegisterClientScriptBlock(this, GetType(this), "fader", "$('#AuthenticateUser').fadeOut(); $('#ForceToChangePassword').fadeIn();");

This will send a client script after the postback has completed. Here is the documentation. This also requires a ScriptManager on the page. If you don't have the AJAX extensions you can probably use it from the Page's method itself.

Andrew Koester
Subjective comment: i hate the asp.net ajax stuff. It all seems like way too much. JQuery and some web services have always blown the asp.net stuff away as far as i'm concerned... Although i'm sure there are some people who would disagree with me and like using the asp.net ajax stuff.
Steve
@Andrew Koester -- Excellent. This is what I needed. I was using Page class's "RegisterClientScriptBlock" method that's why wasn't working. What's insight about ScriptManager? Why we need to call it's method to emit javascript?
shailesh
@Steve: I hate it for some things and like it for others, just as I hate/like WebForms.@shailesh: Page's should work, but I'm not sure why it doesn't. I've had exactly the same problem as you, and I always use the ScriptManager's.
Andrew Koester