Friday, September 22, 2006

Validating User Input string length in a text box.

In this article we will see how we can validate or limit the length of the user input string.

One way is sitting the max length property of the textbox, but that will only validate the max no of characters that can be entered in the textbox. But what is we need to do a validation like the password should be minimum of 6 characters in length and should not exceed 15 characters.

 Is it possible, yes it is, we can use java script to validate this. The following is the script.

[code]

function checkCharLen()
{
if (form1.text1.value.length >=6 && form1.text1.value.length <=15)
          { return true; }
else
          { alert(“Should be 6 to 15 characters”); 
             form1.text1.focus(); return false;
          }
}

[/code]

We need to call this function in the OnBlur event of the textbox.  OnBlur event will fire when the focus is lost.

 

Posted by Sadha in 12:17:51 | Permalink | Comments (2)

Exporting a Data Grid to Excel

This article will teach you about exporting a data to excel.

In most of the application we need to export the data to excel. We need to do a lot of formatting to the data (tabular form, seeting the widht, height of the cell etc). Formattign the data using excel automation is a tedious job.

 The method which i am going to tell you now is going to be very simple. Excel can also be compared with HTML table, Excel work sheet is a matrix of rows and columns.

Generating an excel from aspx is very simple we need to format the html table with data and the change the contect type alone and print the table on the page using response.write. For changing the content type use the following code.

[code]
  Response.ClearContent()
  Response.ClearHeaders()
  Response.ContentType = “application/vnd.ms-excel”
[/code]

 Again most of us fail to format the data propoerly in a HTML table. But we all know how to work with the datagrid and also that the final rendered out put of the datagrid is HTML tags that is a table.

 So all we need is bind the dataset to the datagrid, format the datagird, then get the HTML output (render the html output) and print it. Use the following code for that.

 [code]
   Dim dg As New DataGrid
   Dim sw As New System.IO.StringWriter
   Dim htw As New System.Web.UI.HtmlTextWriter(sw)
   dg.RenderControl(htw)
   Response.Write(sw.ToString)
   Response.End()
[/code]

Is it not simple

Posted by Sadha in 11:50:33 | Permalink | Comments (1) »