Tuesday, April 26, 2011

Validator controls taking up space in Asp.Net

If you are using the built-in ASP.net Validation Controls such as the RequiredFieldValidator or the RegularExpressionValidator to validate user input, as you should be, you may have notices that in your UI even though they are not active (hidden) they are still taking up space in your layout and may be throwing things off.
It turns out that the Validation Controls in ASP.net  have an attribute you can add to their markup called ‘Display’. By default if you do not add and set this attribute in your control’s markup it will default to ‘Static’. There are 2 other options available to you ‘None’ and ‘Dynamic’. It is due to the ‘Static’ option that you are seeing it take up space even when it is not active.
To fix the problem set the ‘Display’ attribute in the control’s markup to the ‘Dynamic’ value and poof, the space is no longer taken up when inactive and is dynamically added when active.
Here is what each option actually does to the Validator’s HTML markup when set.

Static (Default): Causes the required space for the error message to be taken up in the UI’s HTML no matter if it is inactive or active.


<span style="color: Red; visibility: hidden;">Invalid</span>

Dynamic: Causing the element to not display at all, but when activated the space needed to display the error is reclaimed and UI elements are shifted to make room. 


<span style="color: Red; display: none;">Invalid</span>

None: Causing the element to not display at all even when activated, the error message still displays in a Validation Summary if provided but nothing will appear in the UI where the Validator was originally positioned.

<span style="color: Red; display: none;"></span>
 

Sunday, April 24, 2011

Maintaining the Scroll Position after post back in ASP.NET

Some of us may come across this situation, ie maintaining the scroll position after the page is post back. This is very useful when we have a gridview or data repeater or some thing like that, which shows a large amount of data and a button controls is place below page, so that user has to scroll a bit to reach there. After clicking on the button, the page is posted back, but the page is scrolled to top.
If you want to maintain the scroll position, add this attribute on the @page directive



<%@ Page Language="C#" MaintainScrollPositionOnPostback="true"



ERROR: A potentially dangerous Request.Form value was detected from the client in ASP.NET

This occurs when we try to post html code. The server will validate the postdata, if it find that its an HTML code, error something similar to this is shown. This actually helps to prevent running  malicious script. If you really want to post the html content add this to the page directive validateRequest=”false”. 

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" ValidateRequest="false" %>


You can also do this from the web.config like this

<pages  validateRequest="false" />

Tuesday, April 19, 2011

File Size Validation in FileUpload control in ASP.NET

When we build file uploading feature in asp.net we will have requirements to restrict users to upload file above a specified size. By default, JavaScript will not allow us to validate file size or upload files due to security reasons. Hence, it is not possible to validate file size in client side in asp.net when we use FileUpload control.

To validate the file size and prevent users from uploading file over a specified size we need to write server side validation function.

This little article will help us to achieve this requirement using CustomValidator's server side validation.

<asp:FileUpload ID="FileUpload1" runat="server" />
            <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="FileUpload1"
                ErrorMessage="File size should not be greater than 1 KB." OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>

..........

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (FileUpload1.FileBytes.Length > 1024)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
 The above code will prevent users to upload file with size more than 1 KB.

Ajax CalendarExtender displaying at wrong position in Chrome

< script type ="text/javascript" language ="javascript">     function onCalendarShown(sender, args)...