About the author

Miron Abramson
Me
Software Engineer,
CTO at PixeliT
and .NET addicted for long time.
Open source projects:
MbCompression - Compression library

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2014

Creative Commons License

Blog Flux Directory
Technology Blogs - Blog Top Sites

Remove white spaces using HttpModule

Few weeks ago, Mads Kristens poseted a very usefull post about removing white spaces from your pages. For me, he found the best Regular Expression for this job.  He was using the Render method to do it, and it means that if you want to use it in existing site, you need to edit your base page and compile your site again.

I wanted to use it in some sites I have/manage, so I just insert his code into a new ResponseFilter and assign this filter in my Compression component, compile again this dll and replace it in all my sites. Importent thing is, that using this 'remove spaces metho' must folows the rule of that the current request is not made by MS-AJAX component/control, because the ViewState will be corrupted. This check can be easly made by this simple method:

internal static bool IsAjaxPostBackRequest(HttpContext context)
{
      return context.Request.Headers["X-MicrosoftAjax"] != null;
 }

Also note, that RequestFilters are excuted in order LIFO (the last added filter is the first one to be performed) , so if you use any compression filter (like me), make sure you register the compression filter before this one.

The ResponseFilter code can be download from here:

RemoveWhiteSpaces.cs (2.34 kb)

The compression module with this filter included can be download from this post: Compression component.  In this component you can control in wich pages you want to anable or disable the remove spaces option. (exclude specified pages from compression and remove spaces)

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.NET | C# | Server side
Posted by Miron on Thursday, December 27, 2007 1:00 PM
Permalink | Comments (3) | Post RSSRSS comment feed

A Custom Length Validator with server & client side validation

Almost evey TextBox input is going to be stored in Database (if it is not based on XML).  In the DB we need to define a maximum length for any characters field, so, we need to be sure that the input text in the TextBox in not longer then the maximum defined length in the DB where it going to be stored.

We all use Validators. I don't understand why MS didn't include in the built in controls a simple length validator (yes, I know it can be done by the RegularExpressionValidator, but why complicate things?) that simply validate maximum or minimum length of input text. Luckly, creating our custom validation control is much simplier then it sound. All we need to do is a class that inhirite from CustomValidator, override some methods, add some properties, create a JavaScript validation method, compile it into DLL and use it as any other Validator.

Override the ControlPropertiesValid() method:

private TextBox _textBox; 

 /// <summary>
 /// Check if the control is valid for validation
 /// </summary>
 /// <returns></returns>
  protected override bool ControlPropertiesValid()
  {
      Control ctrl = FindControl(ControlToValidate);
      if (ctrl != null)
      {
          _textBox = ctrl as TextBox;
          return (_textBox != null);
      }
      else
          return false;
  }

 override the EvaluateIsValid() method:

  /// <summary>
  /// Perform the validation
  /// </summary>
  /// <returns></returns>
  protected override bool EvaluateIsValid()
  {
      if ( string.IsNullOrEmpty(_textBox.Text) )
      {
          if (MinLength > 0)
              return false;
          else
              return true;
      }
      if (_textBox.Text.Length < MinLength || _textBox.Text.Length > MaxLength)
      {
          return false;
      }
      return true;
  }

 Override the AddAttributesToRender(HtmlTextWriter writer) method to support the client side validation:

  /// <summary>
  /// Add the maximum and minimum attribute for the control
  /// </summary>
  /// <param name="writer"></param>
  protected override void AddAttributesToRender(HtmlTextWriter writer)
  {
      base.AddAttributesToRender(writer);
      if (base.RenderUplevel)
      {
          writer.AddAttribute("MinLength", this.MinLength.ToString());
          writer.AddAttribute("MaxLength", this.MaxLength.ToString());
      }
  }

Register in the OnLoad event our client site script file:

  /// <summary>
  /// Add the controls script file to the page
  /// </summary>
  /// <param name="e"></param>
  protected override void OnLoad(EventArgs e)
  {
      if (Enabled && base.EnableClientScript)
      {
          Page.ClientScript.RegisterClientScriptResource(typeof(LengthValidator), "LengthValidator.LengthValidator.js");
      }
      base.OnLoad(e);
  }

 and ofcourse the client side validation method:

function ValidateLength(source, args) {
    var control = document.getElementById(source.controltovalidate);
    if( control )
    {
         args.IsValid = ( control.value.length >= source.getAttribute('MinLength') && control.value.length <= source.getAttribute('MaxLength') );
    }
}

The full class library project source  can be downloaded below. Just compile it, add it to the toolbox and use it as any other Validator

LengthValidator.zip (2.96 kb)

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by Miron on Saturday, December 08, 2007 7:52 AM
Permalink | Comments (2) | Post RSSRSS comment feed