tags:

views:

267

answers:

4

I think that this should be pretty easy but I'm not quite sure how to wire things up.

I have a page on which the user can define a query. When done, the user enters a name for the query and presses a button. I'd like to process the button click, make a text label (or Span) visible for a few seconds and then have it fade out.

Since it is a postback, I can turn an ASP:Label control to visible - that's easy. Now how do I get jquery to make the label fade away after a few seconds? In a broader sense, how do you get a postback to trigger a jquery method?

Bonus for the simplest solution!

+1  A: 

Here's a low-tech technique that keeps all the script in your page template: add a $(document).ready() function to the page that executes conditionally based on a page-level variable, like this ...

// In your button's click handler, set this.UserHasClicked to true
var userHasClicked = '<%= this.UserHasClicked %>' == 'True';
$(document).ready(function() {
    if(userHasClicked) {
        $(labelSelector).delay(2000).fadeOut(1000);
    }
});
Jeff Sternal
Took me a few tries until I figured out that this uses jQuery 1.4 (I was on 1.3) but it works like a charm. Thanks. The only thing to add is that I have an "else" condition wherein I call hide() to hide the label. I also didn't use an ASP:Label but instead opted for a div to hold the html/text.
Mark Brittingham
Gah, I should have mentioned that. You're welcome for the part that didn't waste your time!
Jeff Sternal
@Jeff - +1 and thx for the answer! I used this at first but there were times when I need to change the text so I've switched to implementing brheal's solution.
Mark Brittingham
Now that I've read it, I prefer that solution too (for this requirement).
Jeff Sternal
A: 

Call ClientScriptManager.RegisterClientScriptBlock

SLaks
Why was this downvoted?
SLaks
A: 

Something like this...

var buttonFade = function() {
  $('#my .label .selector').fadeOut(2000);
}
setTimeout(buttonFade, 2000);

If you post some of your markup, I could also take a stab at putting the setTimeout() into a funtion triggered when the label appears.

Drew Wills
+4  A: 

Start the asp label text as empty.

<asp:Label id="myLabel" runat="server"></asp:Label>

Then you can fade out the label every page load and set the text of the asp label after hitting the button.

Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
  myLabel.Text = "You hit the button"
End Sub

    $(document).ready(function() {
        $('#mylabel').fadeOut(3000, function() {
            $(this).innerHTML(""); //reset the label after fadeout
        });
    });
brheal
+1 - I like this answer too.
Mark Brittingham