Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

785 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