Showing posts with label ASP.NET Webforms. Show all posts
Showing posts with label ASP.NET Webforms. Show all posts

Saturday, May 11, 2013

Client and Server side validations with the ASP.NET Validation controls


If you have ever wondered why it is always recommended to perform both Client side and Server side validation in your web application, the answer is it is very easy to bypass client side validation(easily emulated by downgrading the client target to downlevel in asp.net Page directive)  and submit invalid data in your forms. Needless to say, this could cause all kinds of errors to being vulnerable to cross-site scripting, sql injection etc.

The asp.net Validator controls support both client side and server-side validations by default and out of the box. However, what is not commonly known is that you have to write code to enforce the server side validation! So if you are using these validation controls without checking the Page.IsValid property, and the user disables client side scripting on her browser, then your page is still vulnerable and prone to errors. All it does by default on the server-side when we have these validators, is run the Page.Validate() routine to set the flag for the Page.IsValid property.

Most of the time we do not notice this behavior because we have JavaScript turned on and the client side validations fire and we think we are all set. So remember to check for IsValid from the button event handler which submitted the form, before using the data submitted in the form. Also, the Validate() function is run after the Page_Load event fires, so the .IsValid property is not available in the Page_Load event. Trying to even read that property to check if the data submitted are valid on the Page_Load would throw an exception. If you need to do so, call the Validate function before checking IsValid in Page_Load like so:

protected void Page_Load(object sender, EventArgs e)
{
        if (IsPostBack)
        {
            this.Validate();
            if (this.IsValid)
            {
                  //use your data
            }
       }
}

And one last caveat, in case you are not aware, the checkbox asp.net control do not support the ASP.NET validators, so you can't use them with this control.

Sunday, January 30, 2011

Using jQuery in Webforms 2.0 with Master pages

If you want to use jQuery in Webforms 2.0 with master pages, one way to get jQuery working in other pages is to reference the jQuery scripts in the HEAD tag using ResolveUrl method in the SRC attribute of the SCRIPT tag, like so:

ResolveUrl("~/Scripts/jquery-1.4.1.js")

Note: The jQuery source file is located under a Scripts folder here.

I have seen plenty of examples of slideToggle() and the show()/hide() methods on the internet using a simple div/panel container. This dont work the way you want it, if there is a gridview with a button (say for inline editing with an edit button) nested inside the parent container. The postback caused by clicking the button inside the grid is likely to mess up whatever code you have to create the slideToggle etc.

Has anyone of you encountered a similar issue?

Saturday, December 25, 2010

Server-Side Asynchronous Processing in ASP.NET

One can use Asynchronous methods in ASP.NET when we expect long running I/O, database queries or Web Service/WCF tasks to release the .NET CLR thread for reuse by other ASP.NET requests from the thread pool. Allows your app to scale well and ensures that some users performing long running tasks do not hold up other users.

So using either the AddOnPreRenderCompleteAsync or the RegisterAsyncTask methods, one can start coding async tasks. The former has some drawbacks, like multiple calls on it are executed only sequentially..call it the "Async synchronous" method :-) while RegisterAsyncTask can work multiple threads in parallel and maintain/flow the HttpContext as well. Using the former as an example, say in the Page_Load event

if  (!IsPostBack) {  AddOnPreRenderCompleteAsync( new BeginEventHandler(BeginAsyncProcessing),  new EndEventHandler(EndAsyncProcessing) );
}

Next you can define your event handler with the following signature for the event handler delegate. For eg,
{ IAsyncResult BeginAsyncProcessing(object sender, EventArgs e, AsyncCallback cb, object state)   
    // TODO: Perform async operation and return IAsyncResult
}

You need to pass in an AsyncCallback object, and the method returns the result in an IAsyncResult object. Similary for the asynchronous end event handler below.

{ void EndAsyncProcessing(IAsyncResult ar)   
   //TODO: Get results of asynch operation
}


I shall be following up this post with how to do the asynchronous processing another way using the generic ASHX HTTP handler.