On buttonSave click after saving the record successfully ,I want to show “Save successfully “ message on a label on a page for few seconds and then reload the page. Thanks in Advance
A:
Create a hidden label on the page with viewstate off.
<asp:Label runat="server" ID="lblOk" Text="Save successfully" BackColor="Blue" EnableViewState="false" Visible="false" />
When save is ok, fill make it show.
lblOk.Visible = True
At the same time add an http refresh to the page to make the page reload.
Response.AddHeader("Refresh", "5")
When your page refreshes, the control will be hidden again because it will reset back to invisible.
Carter
2010-07-27 17:24:45
Response.AddHeader("Refresh", "5") not reloading the page.
2010-07-27 17:36:47
@user...: It should take 5 seconds. It worked in my test page. Are you sure you aren't redirecting before that happens?
Carter
2010-07-27 17:44:20
This is how I am doing itIf CategoryId <> Guid.Empty Then lblPageStatus.Text = Resources.MfnWeb.SaveSuccessMessage.ToString ' if new category is added making following changes accordingly If hdnCatogoryId.Value.Trim = String.Empty Then Response.AddHeader("Refresh", "5") End If
2010-07-27 17:56:32
@User: It appears that "hdnCatogoryId.Value.Trim = String.Empty" is not evaluating to true, thus your header is not getting added.
Carter
2010-07-27 18:03:42
It's happening I put breakpoint on Response.AddHeader("Refresh", "5") it does go to this line. In the same if loop if I put Response.Redirect(Request.RawUrl, False) it does reload the page right a way.
2010-07-27 18:05:21
@User: Put any relevant code up in your question so we can troubleshoot.
Carter
2010-07-27 18:06:07
@User: Response.Redirect will stop code execution and clear any new headers. Response.AddHeader requires the page code to complete execution before the output will be sent.
Carter
2010-07-27 18:08:05
AddHeader is not working in this case.
2010-07-27 18:10:57
@User: You can also try creating a global variable/property like "DoRefresh" and setting it to true right there. Then evaluate the variable as the last line in your "PreRender" event. If DoRefresh Then Response.AddHeader("Refresh", "5").
Carter
2010-07-27 18:16:25
I created varible DoRefresh set it to true preRender is never fired up after saving the record.
2010-07-27 18:24:42
A:
How about a server-side literal control that contains the save message and some JavaScript to wait the allotted time and then redirect?
Alternatively, I would instead suggest that the redirect happen immediately and pass a message indicator to the resulting page to display the message. It seems to be the more standard approach. But, not knowing your setup, it's possible that it may not be acceptable for your needs.
David
2010-07-27 17:26:31
I want to reload same page. Becuase of ajaxification and creating user controls dynamically there are lot of issues so I need to Reload the page when new record is added. On the same page on the top I have a lable where I want to show the messae and reload the same page.
2010-07-27 17:38:56
Fair enough. Moving forward, you'll want to de-couple yourself from the page load event as much as possible in your designs. See the ASP .NET MVC implementation. But if Carter's refresh header suggestion isn't working for you (it should, but there may be some detail we don't know) then go ahead and try putting the JavaScript in the label. If it's set to Visible=false on the server side then it won't render client-side. Flipping that will render it and allow the client to execute the JavaScript.
David
2010-07-27 17:44:14
Do you mean to add javascript in the lable text to rolad the page after 5 seconds?
2010-07-27 18:39:18
Yes. Or if you want all of your JavaScript in the same place in the page (or in external files) then you can just create another Literal control (preferred over Label controls when you don't want any decoration around the content) where you would want the JavaScript to go and edit its Visible property accordingly.
David
2010-07-27 18:52:16
I have been using ScriptManager.RegisterStartupScript to run javascript from server side. I am not very good in javascript to wait for 5 seconds and load the same page again what would I put in the function or call directly here?
2010-07-27 19:01:11
Take a look at the Auto-Refresh using settimeout() here: http://www.quackit.com/javascript/javascript_refresh_page.cfm
David
2010-07-27 19:08:47
I have added following code still it did not refresh ( by the way isn't this code going to refresh every 5 seconds?) I want to refresh the page only once after user saves the record but after 5 seconds when he has time to read a succes message. Dim javaScriptFunction As String = "setTimeout('location.reload(true);',5000);" Me.Page.ClientScript.RegisterStartupScript(Me.GetType, "Reload", javaScriptFunction, True)
2010-07-27 19:24:25
How does it look when it renders in the browser? That will help discover why it's not working. Also, the way you're implementing it here may indeed refresh the page every 5 seconds, assuming you execute this on every page load. You only want to include this JavaScript to the client side on the page load in which you also populate the message. Also, welcome to the endlessly aggravating world of the WebForms page lifecycle and event model. Once you go MVC you'll never go back :)
David
2010-07-27 19:30:06