3

I have this panel on my .aspx webform page to display messages (success, failed, exception etc.) after button postback events . I also included some Jquery library on this page as well.

 <asp:Panel ID="InfoPanel" runat="server" class="alert alert-success alert-dismissable" Visible="False">
     <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
     <i class="fa-lg fa fa-bullhorn"></i>
     <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
 </asp:Panel>

It will look like this: (lblMessage will display the text at code-behind) enter image description here

How do I make it to be auto-close after 5s, I have zero knowledge about JS and jQuery but if you show me how it work I can apply it to my program (lol) Note: I see this question right here: Bootstrap Alert Auto Close the selected answer was:

    $("#success-alert").fadeTo(2000, 500).slideUp(500, function(){
    $("#success-alert").alert('close');
});

But I am confused, how do I make it work?

Community
  • 1
  • 1
Ronaldinho Learn Coding
  • 13,254
  • 24
  • 83
  • 110

3 Answers3

1

$("#success-alert") is a selector... You're choosing what part of the page the following actions will be taken against. In this case, it looks for an element with the id of success-alert.

The Id of asp controls in the page is auto-generated but can be accessed via the controls .ClientId property.

So, you need to execute that javascript but replace success-alert with the client id of the InfoPanel control

By default, Javascript executes as soon as it's loaded by the page (there are ways to delay it). In your case, the easiest way to do this would be to include a <script> block in the response containing the message.

For your reference, the fadeTo documentation is worth a read. The parameters being passed in are the delay (2000 milliseconds in your example) and the opacity (should be 0-1). After it's faded out, it then slides up over the course of 500ms (think of a closing concertina).

The function() {} at the end is an anonymous function that's called after the animations complete. Much like a lambda. You can optionally use this to do any other tidy up you need but it shouldn't be required.

It's been quite a while since I did webforms but I think you want something like this at the end of your info box

<script type="text/javascript">
    $("#<%=InfoPanel.ClientID%>").delay(5000).fadeTo(500, 0);
</script>

Eg wait 5 seconds then fade out over 1/2 second

Basic
  • 26,321
  • 24
  • 115
  • 201
  • Where should I put this to? I tried put it below the panel and inside the panel but both ways won't work? – Ronaldinho Learn Coding Oct 17 '14 at 05:42
  • Can you provide an example of a .aspx page with a button and an auto close panel after button click event triggered? – Ronaldinho Learn Coding Oct 17 '14 at 07:20
  • It should work if you place it before the ``. If it's not, you'll need to give me some debugging information. Your browser probably has developer tools (F12 in Chrome, Ctrl-Shift-K in Firefox, Menu option in IE). Go to the Console tab and watch what heppens when you cause a message to be displayed. if there are anysyntax errors/similar problems, you should see errors listed. If you see nothing at all, use the Inspect Tab (or Layout depending on browser) to see the _generated_ source. This is what the page looks like now, not when you first got it. Make sure you can see the jscript. – Basic Oct 17 '14 at 08:17
  • (Sorry, haven't got time to mock up an example right now but if you're still having issues when I finish work, I'll do that for you) – Basic Oct 17 '14 at 08:21
  • I see nothing on console, I will wait for your mock-up example, thank you very much! – Ronaldinho Learn Coding Oct 17 '14 at 08:34
  • Any update? I also stuck and need help with this please: http://stackoverflow.com/questions/26431890 My god I need to start leaning JS and Jqery!!! – Ronaldinho Learn Coding Oct 17 '14 at 21:21
1

I found this one and it works: http://www.queryadmin.com/997/automatically-close-twitter-bootstrap-alert-message/

Automatically close (fade away) the alert message after 5 seconds:

<script type="text/javascript">

$(document).ready(function () {

    window.setTimeout(function () {
        $(".alert").fadeTo(1500, 0).slideUp(500, function () {
            $(this).remove();
        });
    }, 5000);
     
});

</script>

The content below the message will slide-up to its original position.

This is the HTML code used to display the message:

<div class="alert alert-danger">
    This is an example message...
</div>

More information: window.setTimeout(function, delay)

The result will look as follows:

enter image description here

carloswm85
  • 1,396
  • 13
  • 23
Ronaldinho Learn Coding
  • 13,254
  • 24
  • 83
  • 110
0

You can set a timer and fade it out after 5 secs.

Something like

setTimeout(function () {
     $("#success-alert").fadeOut(100, null);                            
}, 5000);
Krishna Veeramachaneni
  • 2,131
  • 3
  • 18
  • 24