ASP.NET 2.0 Handling Page Refresh / Back from Data Insertion
I know this is nothing new. There are plenty of resources out there for this but none showing you an example which consists of 1 Master Page, 1 Web Form and 2 User Controls.
Here is what I want to achieve.
Basically the 2 user controls are Banking and Logistics Solutions. Same coding like Dino's one but I modify it a bit to fit into different tables.
Before I move on, I will need to justify a bit. Hehe :P
First I had extended Dino Esposito example from his article "Build Your ASP.NET Pages on a Richer Bedrock" http://msdn2.microsoft.com/en-us/library/ms379557(VS.80).aspx
I will admit it might not be the best solution in this world, however it serves the purposes of preventing Page Refresh (tested in FireFox and IE 7).
However there are tutorials like this http://aspalliance.com/687 and http://jarednevans.typepad.com/technoblog/2005/01/jareds_techno_b.html.
Basically the 1st one recommends you to do in Database level. But think about it, page refresh suppose to be handle on the User Interface side. Isn't it better to handle on the Presentation layer instead at the Data Layer. Ya I know through the author's recommendation it is faster a lot. But just I feel it is not right. Correct me if I am wrong.
The 2nd one is quite good and shorter than Dino's one. However there are issues with handling the back button. And sometimes the refresh doesn't work. I am not able to capture the problem actually, so I do not want to go beyond that and take risks for my project.
I am done with those justifications. Back to the post.
There is an issue that you need to take care of which Dino's sample source code will not help you.
Instead of having those data access code in Default.aspx, and now you have to place the functionalities in the 2 user controls. Take note, I am just extending his example and I do not include any best practices. :)
So there will be an issue on parent (default.aspx) /child (banking.ascx) communication like child needs to access methods like TrackRefreshRate()
Furthermore you cannot create a new class maybe MsdnControl.cs which extends from System.Web.UI.UserControl and use methods like RegisterHiddenField.
How are you able to solve this?
Take note again, this is 1 technique. You can use other techniques if you like.
In order for Child user control to access the Msdn.Page (base class) method, do this:
a) Create a new class and place it under App_Code. Name it IRefresh.cs
IRefresh.cs
public interface IRefresh
{
void TrackRefreshState();
bool IsRefresh { get; }
}
There will be 2 elements here as there are 2 methods which the child control that need to call from the base class.
b) Now from the Default.aspx code behind, implements this IRefresh. Remember to implement the property and method as well. This is like wrapping the base class Msdn.Page.
Default.aspx
public partial class _Default : Msdn.Page, IRefresh
{
... // the original code
#region IRefresh Members
void IRefresh.TrackRefreshState()
{
// Call the method from the parent base class
this.TrackRefreshState();
}
public bool IsRefresh
{
get { return this.IsPageRefresh; }
}
#endregion
}
c) In order for your child user control to call the methods in _Default, do this:
((IRefresh)Page).TrackRefreshState();
OR
(!((IRefresh)Page).IsRefresh)
AddRecord(FName.Text, LName.Text);
Easy right.
Just a few tips for people who read Dino's article:
a) You will face problem when debugging your code, and always get a object instance error at this code RefreshAction.cs
private static void EnsureRefreshTicket(HttpContext ctx)
{
if (ctx.Session[LastRefreshTicketEntry] == null)
ctx.Session[LastRefreshTicketEntry] = 0;
}
So how can you continue debugging?
The way which I use is to remove this code from web.config:
<httpModules>
<add name="MainModule" type="Msdn.RefreshModule, MsdnExt" />
</httpModules>
So while you are doing your testing, you will assume that Page Refresh/Back are handled. For deployment purpose, all you need is paste that code earlier into web.config. It works :)
b) There will be no issues if you have more than 1 user control like my case.
Assume this:
Banking Solutions - 3 rows which I had added earlier before adding into Logistics Solution
Logistics Solutions - I row *just added and then I press F5
It still maintains the same 3 rows in Banking and 1 in Logistics.
Hope you find my blog interesting. :)
Thanks.