Solved

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

Posted on 2008-10-30
5
3,535 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now