This is a migrated thread and some comments may be shown as answers.

Calling Javascript after postback

4 Answers 116 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
John Snyder
Top achievements
Rank 1
John Snyder asked on 02 Apr 2008, 01:23 PM

When a user clicks on the "Save" button, I want to write a message that says "Saved at [current time]" (like Gmail does when saving a draft).  In order to get the current time I was going to use javascript.  How do I go about calling the Javascript to get the time and displaying it in my label?

Some things to note:

  • I am using ASP.Net 2.0 and the latest build of Promethius
  • I am using a Master Page that has the AjaxManager and my other pages and usercontrols are all using the AjaxManagerProxy controls
  • I only want this message to be set when a certain button is clicked, not on every ajax request.

Any help would be appreciated.

4 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 02 Apr 2008, 01:46 PM
Hi Dustin DeFoe,

One of the benefits of the ajax technology is that it brings rich "client-side like" performance to your app. So in this line of thoughts you can go ahead and update the label with the DateTime.Now on the server. If you're going to do this with javascript, you do not need our Ajax controls for this.

Best wishes,
Steve
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
John Snyder
Top achievements
Rank 1
answered on 02 Apr 2008, 01:49 PM
I am not wanting the server date and time, although that would be much easier.  Instead I am wanting the users local time and the only way I know to get that is by using javascript.  I still need to do the postback in order to perform the saving code.  Assuming the save worked  I want to show the "Saved at [time]" message.  I am trying to set this from the postback as I don't want to show a saved message before the postback.
0
John Snyder
Top achievements
Rank 1
answered on 02 Apr 2008, 01:58 PM
I believe I have this working:

HTML Code:
<telerik:RadAjaxManagerProxy ID="AjaxManagerProxy1" runat="server">  
    <AjaxSettings> 
        <telerik:AjaxSetting AjaxControlID="btnSave">  
            <UpdatedControls> 
                <telerik:AjaxUpdatedControl ControlID="RadScriptBlock1" /> 
            </UpdatedControls> 
        </telerik:AjaxSetting> 
    </AjaxSettings> 
</telerik:RadAjaxManagerProxy> 
 
<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">  
    <script language="text/javascript">  
        <%= _ScriptToLoad %> 
    </script> 
</telerik:RadScriptBlock> 
 
<asp:Label ID="lblValidation" runat="server" /> 
 
<asp:Button ID="btnSave" runat="server" Text="Save" /> 
 

VB Code
Public _ScriptToLoad As String 
 
Protected Sub btnSave_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles btnSave.Click  
    _ScriptToLoad = "D = new Date(); var H = D.getHours(); if (H > 12) {H = H-12};document.getElementById('" & lblValidation.ClientID & "').innerHTML = 'Saved at ' + H + ':' + D.getMinutes();" 
End Sub 
 

Dustin
0
Chuck
Top achievements
Rank 1
answered on 24 Nov 2012, 02:31 PM
Dustin,

Many belated thanks for this!  I was using a different approach and setting the script on a label (which I had seen suggested elsewhere).  This worked for full postbacks but not for Ajax postbacks.  Your solution works for both and seems like so much less of a hack.
Tags
Ajax
Asked by
John Snyder
Top achievements
Rank 1
Answers by
Steve
Telerik team
John Snyder
Top achievements
Rank 1
Chuck
Top achievements
Rank 1
Share this question
or