Ajax, Asp.net, Javascripts

Cancel previous Ajax requests on new request

If you are using ajax in web application , then there might be possibility that user does repetitively action such as click on button more than once which sends same asynchronous requests for every single click,
Before completing first request, another request for the same functionality is get sent and so on…

Here is the simple solution that helps to cancels previous ajax request,

Copy following two java scripts functions on page. it will cancel previous asynchronous requests. It works in asp.net ajax.

  
function pageLoad()
        {
Sys.WebForms.PageRequestManager.getInstance( ).add_initializeRequest(cancelPostBack);
         }
function cancelPostBack(sender, args)
        {
if (Sys.WebForms.PageRequestManager.getInstance( ).get_isInAsyncPostBack())
            {
              args.set_cancel(true);
            }
        }
Asp.net

Set themes from Global.asax

We can able to set themes dynamically depending on users prefrence from global.asax. for following e.g i stored theme name in session after user logged in.

//Global.asax
void Application_PreRequestHandlerExecute(object src, EventArgs e)
{
Page p = this.Context.Handler as Page;
if (p!= null)
{
p.PreInit += new EventHandler(page_PreInit);
}
}

void page_PreInit(object sender, EventArgs e)
{
Page p = this.Context.Handler as Page;
if (p != null)
{
if(Session["Theme"]!= null)
p.Theme = Session["Theme"].ToString();
else
p.Theme = "DefaultTheme";
}
}
Asp.net

Error Handling in asp.net

ASP.NET provides several levels at which you can handle and respond to errors that may occur when you run an ASP.NET
application. ASP.NET provides three main methods that allow you to trap and respond to errors when they
occur: Page_Error, Application_Error, and the application configuration file (Web.config).

1. In Page

public void Page_Error(object sender,EventArgs e)
{
        Exception objErr = Server.GetLastError().GetBaseException();
        string err =   "Error Caught in Page_Error event

" + "
Error in: " + Request.Url.ToString() + "
Error Message: " + objErr.Message.ToString()+ "
Stack Trace:
" + objErr.StackTrace.ToString(); Response.Write(err.ToString()); Server.ClearError(); }

2.In Global.asax

using System.Diagnostics;
protected void Application_Error(object sender, EventArgs e)
{
  Exception objErr = Server.GetLastError().GetBaseException();
  string err =   "Error Caught in Application_Error eventn" +
                 "Error in: " + Request.Url.ToString() +
                 "nError Message:" + objErr.Message.ToString()+
                 "nStack Trace:" + objErr.StackTrace.ToString();
  EventLog.WriteEntry("Sample_WebApp",err,EventLogEntryType.Error);
  Server.ClearError();
  //additional actions...
}

3. Web.config


   

Modes in web.config:-
On: Unhandled exceptions redirect the user to the specified defaultRedirect page.  This mode is used mainly in production.

Off: Users receive the exception information and are not redirected to the defaultRedirect page.  This mode is used mainly in development.

RemoteOnly: Only users who access the site on the local computer (by using localhost) receive the exception information.  All other users are redirected to the defaultRedirect page. This mode is used mainly for debugging.

Asp.net

Reset asp.net form

If we use third party controls on form then HTML reset button might not be work.

For this we need to do – add Onclick event to button as below ,

btnClear.Attributes.Add("onClick","document.forms[0].reset();return false;");