March 2008 - Posts
In this blog i will talk about how to redirect the website visitors to a page that display a " web site under maintenance" ,
you can implement this functionality in your Global application file ( Global.asax file) ,
you can use Application_BeginRequest event handler which will be called when your website receives a new request ,
this example will put your website in a maintenance mode if the current day is Saturday and for a one hour (from 10 -11)
add the following function to Global.asax file ,
(if you don't have this file in your website , in visual studio , right click on the website , select add new Item , select global Application Class)
Private Sub Application_BeginRequest(ByVal source As Object, ByVal e As EventArgs)
Dim application As HttpApplication = CType(source, _
HttpApplication)
Dim context As HttpContext = application.Context
If Now.DayOfWeek = DayOfWeek.Saturday AndAlso Now.Hour = 10 Then
context.RewritePath("~/UnderMaintanace.aspx")
End If
End Sub
adding the above code will redirect your website users to UnderMaintanace.aspx page if the day is Saturday and if the time between 10 -11 .
- Note that i used RewritePath and not Response.redirect because the second one will make a new request to the website and so we will have infinite loop (until we redirect to an HTML page which will not handled by asp net runtime and the function will not get executed )
- the above solution use the date and time of the week to put the website in a maintenance mode , but you can extend this to more practical scenarios ,
for example , you can use a settings file for your website, so that the administrator can set some flag and put the website in maintenance mode !
- Another thing you may want to do is to use http module instead of using the Global application file , so that you can have a reusable module that can be plugged to any new website you design.
Further resources:
Regards,
Anas Ghanem
did your windows XP take a long time to loggoff ? My windows was taking more that a minute to logoff the user ,it just show me logging off , but the computer is Idle...
this happened because there maybe a handle to a registry key that prevent the windows from saving and clearing handle.
I was solving this problem by creating a new user account...
of course this is not a practical solution , so i searched the interenet andfound that tool,
And i just want to share it with you ...
you can download it here
Regards,
There is many reasons to prevent that , like preventing mutiple Database Calls , Or Even Preventing them from submitting the form twice and so save the bandwidth and server resources .
this is a one solution for that Issue ,
Use a hiddenField control to remeber the Click counts ,and when the Linkbutton clicked you check to see if the click counts >0 ,
if yes then you will cancel the click event ,
To Accomplish this , Add a hiddenField Server control to your page as follows :
<asp:HiddenField ID="HiddenField1" runat="server" Value="0" />
Now Register Onclick Attribute for the Linkbutton , in page load add this code :
If Not IsPostBack Then
LinkButton1.Attributes("onclick") = String.Format("javascript:var Count = document.getElementById('{0}'); if (Count.value>0) return false ;Count.value =Count.value+1; ", HiddenField1.ClientID)
End If
How To Test that :
Add the hidden field and register the Onclick attribute as mentioned ,
Now click the LinkButton for the first time , the form must be submitted (postback must occured)
Try to click the LinkButton for the second time, you will notice that there is no postback happened .
Regards,
Anas ghanem
By default .net runtime uses the cookies to remember the session Id between the requests ,
but when using Cookie less sessions ,the runtime inserts the Session Id to the requested url ,
this way the runtime can remember the session id and prevent the session loss .
The problem is , when using the Menu and TreeView Controls , these controls doesn't handle this issue ,
so when those controls display there data from the Site maps, they didn't append the session id to the Navigation Urls of there Items ,
and so when the User Navigate to a page using those controls , he will redirected to a Url that didn't contains the session Id,
and so the runtime can't extract the session id , Hence the session will be lost .
The Solution:
the solution is to Manually Append the Session Id to NavigateUrl of the Items for those Navigation controls,
we can use HttpContext.Current.Response.ApplyAppPathModifier to modify the Item Urls as Follows:
For the Menu Control , we can use MenuItemDataBound Event Handler to accomplish this ,
protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
{
// appened the SessionId to Menu Item URL to Avoid sessin loss
e.Item.NavigateUrl = HttpContext.Current.Response.ApplyAppPathModifier(e.Item.NavigateUrl);
}
For the TreeView Control, we can use TreeNodeDataBound to accomplish this
protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
e.Node.NavigateUrl = HttpContext.Current.Response.ApplyAppPathModifier(e.Node.NavigateUrl);
}
Regards,
Some times you may have a page with output cache enabled , and for some reason you don't want to use its output cache ,
Take this seanrio:
you have a page that displays a Dynamic data , and you have 2 users
- Viewers: they just View your page.
- Editors: they need to Edit the data in the page , and they must see the latest changes they did .
and you decided to Enable output Caching on the page to enhance the Performance ,
Now the problem is : the Editors can't Edit and See the Latest chages they did , because the page output is cached ,
you need to enforce the page to show and render the latest data without using its current cached output,and without affecting the current cached output.
The solution :
you need to use HttpCachePolicy.AddValidationCallback Method , so that you can register an output Cache validation Callback ,
Set the Output Caching for your page By setting the output cache in ASPX code as follows:
<%@ OutputCache VaryByParam="none" Duration="600" %>
and in Code behind :
Page_Load:
Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If HttpContext.Current.User.Identity.IsAuthenticated Then
Response.Cache.AddValidationCallback(New HttpCacheValidateHandler(AddressOf ValidateCache), Nothing)
End If
End Sub
And the ValidateCache method:
'' this method will invoked every time the page requested
Public Shared Sub ValidateCache(ByVal Currentcontext As HttpContext, ByVal data As Object, ByRef status As HttpValidationStatus)
If Currentcontext.User.IsInRole("Editors") Then
' Dont use the output cache for the Editors
' and force the page to Excute ( handled as a cache miss)
status = HttpValidationStatus.IgnoreThisRequest
Else
' nothing needed , just use the Output cache
' also if you want to Invalidate the page Output cache ,
' you need to set the status value to HttpValidationStatus.Invalid
End If
End Sub
How to Test :
Add a break point on the Page_laod event Handler, Page_Load will not called for Non Editors Users (Unless the output Cache Expired )
Hope it Helps.
Anas Ghanem
More Posts