Monday, August 25, 2008

How to validate length of input into a multi-line textbox

You can validate the length of input of an ordinary TextBox using the MaxLength attribute.  However, this doesn't work if the TextBox has a TextMode of Multi-line.  In those instances, use this piece of code.

<script type="text/javascript" language="javascript">

function MaxLength()

    {

var txt = $get("<%=ResponseNote_TextBox.clientid %>");

var msg = $get("<%=Message_Label.clientid %>");

if (txt.value.length > 500)

        {

            SelectText();

            msg.className = "Failure";

            msg.innerHTML = "* Response has more than 500 characters";

        }

else

        {

if (msg.innerHTML == "* Response has more than 500 characters")

            {

                msg.innerHTML = "";

            }

        }

    }

function SelectText()

    {

var txt = $get("<%=ResponseNote_TextBox.clientid %>");

var rng = txt.createTextRange();

            rng.moveStart("character", 501);

            rng.moveEnd("character", 0);

            rng.select();

    }

function RemoveCRLFs()

    {

// debugger;

var txt = $get("<%=ResponseNote_TextBox.clientid %>");

var str = window.clipboardData.getData("Text");

    str = str.replace(/(\s*(\r?\n|\r))+$/, '');

    txt.value = str ;

    }   

</script>

Page_load:

ResponseNote_TextBox.Attributes.Add("onblur", "javascript:MaxLength()")

ResponseNote_TextBox.Attributes.Add("onpaste", "javascript:RemoveCRLFs()")

Thursday, August 21, 2008

RegisterForEventValidation can only be called during Render();

This error comes when you are exporting the GridView control from Visual Studio. To solve this error you have to turn the eventValidation off.  You can do this in the web.config file but if you do, the eventValidation will be turned off for all the pages.

<pages enableEventValidation ="false" ></pages>

Alternatively, you can do this in the Page directive which will turn off the validation for a single page.

<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true"

CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>

Monday, August 11, 2008

How to Drop a Column

  • Discover whether there is a constraint;

DECLARE @ConstraintName NVARCHAR(50);
SELECT @ConstraintName = [name]

FROM sys.objects o

WHERE o.[parent_object_id]=OBJECT_ID('Schema.TableName')

AND o.type='D'

AND o.[name] LIKE '%First5LettersOfColumnName%'

  • If there is, remove it;

IF NOT (@ConstraintName IS NULL)

   EXECUTE ('ALTER TABLE Schema.TableName 
   DROP CONSTRAINT ' + @ConstraintName)
GO

  • Remove the column

IF EXISTS(SELECT * FROM sys.columns WHERE [name]='ColumnName 
AND [object_id]=OBJECT_ID('Schema.TableName'))

      ALTER TABLE Schema.TableName

      DROP COLUMN ColumnName;

GO