views:

1036

answers:

3

I saw a couple of similar questions on the same topic, but none of them addressed my issue.

I've got a asp.net website, and want to show a status message (asp:label that will disappear in 5 seconds) to the user after the database was updated.

i want to assign the text to the label, and then hide it with javascript.

i got the js part sorted out, the only problem is how do i call js function after i assigned the text to the control?

let's say i'm updating something in the database with following code:

<asp:Button ID="btnUploadFiles" runat="server" OnClick="buttonSubmit_Click" Text="Update"  />

code behind

protected void buttonSubmit_Click(object sender, EventArgs e)
    { try{// update the database  
          // change label text to tell the user update succeeded}
      catch(Exception ex){...}
    }

please help!

UPDATE: no jquery please, just plain javascript

A: 

Are you posting back via Ajax or a normal postback? If it's a normal postback, just register some javascript on the page that sets a timer and calls your function that hides the label after the timer expires. The following example uses jQuery to make sure the DOM is loaded before the timer is started, but there are other ways. It will hide the label after 5s has elapsed.

function hideLabel(label)
{
   $(label).hide();
}

$(document).ready( function() {
    var label = $('#labelID');
    setTimer(function() { hideLabel(label); ),5000);
});

The idea is basically the same if you are using AJAX, except you set the timer in the onSuccess callback for the AJAX call instead of on document load.

tvanfosson
no jQuery please, how about some plain JS ?
roman m
+3  A: 

I'd personally use jQuery for this, but if you want to use plain old JavaScript then something like this will probably do the trick:

<script type="text/javascript">
function hideLabel()
{
    // replace yourLabelID with <%=YourLabelID.ClientID%> if it's a .NET Label control
    document.getElementById('yourLabelID').style.display = 'none';
}
setTimeout('hideLabel()', 5000);
</script>

If necessary you could also embed the script block in a Literal control and only make it visible when you update your label text.

LukeH
You don't need to pass 'hideLabel()' as a string. You can pass setTimeout a reference to the function instead: "setTimeout(hideLabel, 5000);". It doesn't make much difference in this case since it's a one-time call with a constant, but it's better to avoid executing strings.
Matthew Crumley
@Matthew, You're absolutely right. I was writing the example off the top of my head and wasn't sure whether the non-string version had any potential browser-compatibility issues, so I thought it better to be cautious.
LukeH
+2  A: 

To answer your question "How do i call js function after i assigned the text to the control?". You can just add a call to 'RegisterClientScriptBlock' inside of your button click event to output the JavaScript provided by Luke.

protected void buttonSubmit_Click(object sender, EventArgs e)
{ 
    try
    {
       // update the database  
       // change label text to tell the user update succeeded
       label.Text = "Message";
       string js = "function hideLabel(){document.getElementById('" + label.ClientID + "').style.display = 'none'};setTimeout(hideLabel, 5000);"
       ClientScript.RegisterClientScriptBlock(this.GetType(), "test", js ,true);
    }
    catch(Exception ex){...}
}
Phaedrus
If you're going to register it in the codebehind you should at least use the clientid from the control instead of hard-coding it.
tvanfosson