Solved

How to make a label disappear after a certain length of time?

Posted on 2008-10-30
5
3,673 Views
Last Modified: 2012-06-27
I have an asp.net label control which displays messages when users do certain operations on my page. This label control is set to visible="false" until it needs to be displayed, and then programmatically it's visibility is set to "true". When this happens, I don't want it to stay there, I want it to disappear after 5 seconds. What's the best way to do this? Is there an AJAX control that already does this, or do I need some custom Javascript? And if it is Javascript that I need, how do I get the function to fire every time the label's visibility is set to true?
0
Comment
Question by:CitySec
5 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 22843325
i think you have to use the asp ajax controls to do this...there is a timer that you can use...there...
0
 
LVL 21

Expert Comment

by:silemone
ID: 22843402
oops...it's not part of the asp ajax control...It's the UpdateProgress...I'm thinking that if you set it for 5 seconds, then you can cause another event to happen after its finished...say:  have an event in you page called makeInvisible().  in the makeInvisible call a <asp:button.....style="display:none;"> button which will make label invisible...now, it's important that the label be placed inside of a UpdatePanel and the trigger for that updatePanel be that invisible button...
0
 
LVL 8

Accepted Solution

by:
tiagosalgado earned 500 total points
ID: 22843405
You can use setTimeOut() javascript function to hide your label.
Above code, will set your label text and after 5 seconds the label will disappear.
 

<asp:ScriptManager runat="server" ID="sm1" />
        <asp:UpdatePanel runat="server" ID="up1">
            <ContentTemplate>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </ContentTemplate>
        </asp:UpdatePanel>
 
And at code-behing you can have something like this:
 
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "my label text"
        ScriptManager.RegisterClientScriptBlock(Me.Page, Me.GetType, "script", "window.setTimeout(function() { document.getElementById('" + Label1.ClientID + "').style.display = 'none' },5000);", True)
    End Sub

Open in new window

0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22843413
you can hide the label using javascript

add this code after you make label visible in code behind file
ClientScript.RegisterStartupScript("HideLabel()",<time in milliseconds>,true);

//add this function to the page
function HideLabel()
{
document.getElementById("LabelID").style = "display:none";
}

(there could be syntax error)
0
 
LVL 2

Author Comment

by:CitySec
ID: 22843483
That was the first solution I tried because it was the easiest to paste and required only one line of code. The other solutions may work too, but I didn't try them. One last question, can I make the label fade out instead of disappear at once?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question