Monday, June 6, 2011

asp.net : Implement theme for web application using global.asax file

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in a Web application, across an entire Web application, or across all Web applications on a server.
Themes are made up of majorly by cascading style sheets (CSS), images, and other resources like skins. . Themes are defined in special directories in your Web site or on your Web server App_Themes

JavaScript : Get querystring key's value for current page url.

Querystring is very most used technique to pass values across pages to manage web application state. All server-side programming languages like c#, vb.net, java etc have built-in functions to retrieve querystring values of a URL. In web browsers you can access the querystring with client-side JavaScript, but there is no standard way to parse out the name/value pairs
So here is a function to return a parameter you specify. The following javascript code snippet facilitates Javascript's built in regular expressions to retrieve value of the key. Optionally, you can specify a default value to return when key does not exist.
JavaScript Function
function getQuerystring(urlkey, default_)
  {
     if (default_==null) default_="en";
        urlkey = urlkey.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regex = new RegExp("[\\?&]"+urlkey+"=([^&#]*)");
        var qs = regex.exec(window.location.href);
        if(qs == null)
           return default_;
        else
           return qs[1];
  }
How to use this function

var langauge_value = getQuerystring('lang');
This function will help to retrieve current value for lang key in querystring. Call to this function will return 'en'.

Friday, April 10, 2009

Custom Validator for FileUpload in asp.net 2.0

Validation Web controls are ASP.NET 2.0 Web controls designed to validate form fields control inputs with some rules apply to it. Previously it used to achieve with the javascript/vbscripts functions on user controls like text box and drop down list. it was bit annoying and time consuming and also code grows to longer line which is difficult to manage.

asp.net 2.0 introduces new validation controls to resolve these issues. One of which I am discussing here,

CustomValidator- Checks the form field's value against custom validation logic that you, the developer, provide with the help of client script written in javascript.


javascript function

function check_ClientValidate(source, args)
{
var _pageStatus = true;
if(args.Value != "")
{ _pageStatus = true;}
else
{
con = false;
document.getElementById("CustomValidator1").innerHTML = "Please Select file!";
}
if( _pageStatus == true)
{
var file1 = args.Value.substring(args.Value.length - 3, args.Value.length); //doc
if( file1 == "doc")
{
_pageStatus = true;
}
else
{
_pageStatus = false;
document.getElementById("CustomValidator1").innerHTML = "You can upload doc file only.";
}
}
if( _pageStatus == true)
{ args.IsValid = true;}
else
{ args.IsValid = false;}
}

You should also provide server validation code as well to perform both ways. this is to minimize error on server side in case client side validation get failed. [ techincal reasons..] Implement same logic here in server validation function to avoid runtime error.


asp.net page controls .aspx.vb file

Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
Dim _pageStatus As Boolean = True
If args.Value <> ("") Then
_pageStatus = True
Else
_pageStatus = False
CustomValidator1.Text = "Please select File!"
End If
If _pageStatus Then
Dim file1 As String = args.Value.Substring(args.Value.Length - 3, args.Value.Length)
If file1.Equals("doc") Then
_pageStatus = True
Else
_pageStatus = False
CustomValidator1.Text = "You can upload file such as .doc"
End If
End If
If _pageStatus Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub

this will solve your problem of file upload with limited extension.