Sunday 11 October, 2009

ASP.Net AJAX Control Toolkit’s New Release, AsyncFileUpload Control


Ajax Control Toolkit is not new for every .NET developer. A new version of the AJAX Control Toolkit is now available for download from the CodePlex website. This new version of the AJAX Control Toolkit contains two new controls:
  1. SeaDragon Java Script Code (SJC) - The SJC control allows SeaDragon scripts to be used to display an image, and to zoom in and out of that image using mouse button keys without resizing the window. I saw the demo and it's a really cool control.
  2. AsyncFileUpload - Finally, we have a control which uploads file asynchronously. This new control enables you to perform file uploads without doing a postback. The control displays a throbber image during upload and raises client and server events when the upload is complete. Check the live demo here.
Note: This control will only work with .NET 3.5 or higher version.

PingBack From :Code Project

Difference between obj.ToString() vs Convert.ToString(obj) vs (string)obj vs obj as string

obj.ToString()


.ToString() can be called from any object. It returns a string that represents the current Object. ToString() is a method of object, and it will always work on a non-null reference. For example:

object str = "test string";
string newTestString = str.ToString();

str.ToString() will return "test string" as string .But

object str = null;
string newTestString = str.ToString();


Will throw the exception:“Object reference not set to an instance of an object.”
The above exception came because str is null we can not perform any operation on any object that has null reference.

Convert.ToString(obj) :

Convert.ToString(obj) means we are calling a method and passing obj as argument.Internally it will call ToString(object value) method and then ToString(object value, IFormatProvider provider) method is called.

(string)obj :
First it should be clear that it is a cast operation, not a function (or method) call. We should use this if we are sure the object we are passing is of type string or it has an implicit or explicit operator that can convert the object to a string. This will return null if the object itself is null . See examples.

object str = "test string";
string newTestString = (string)str;

(string)str will return "test string" as string.But you cant do tht for Int.See below example:

object i = 2;
string str = (string)i;

The above code will throw this error:
Unable to cast object of type 'System.Int32' to type 'System.String'.Cannot cast expression of type int to string
Int doesn't have an explicit operator to string.When casting a number to a string, there is a "loss" in data. A string cannot perform mathematical operations, and it can't do number related things.

obj as string :
It is safe cast operation. It is similar like (string)obj but instead of throwing an exception it will return null if cast operation fails. For example:

object i = 2;
string str = i as string;

str will return null.

Code Translation for .Net c# < = > VB.Net

This service will translate the code for you, just start typing the code or upload a file to convert it.

For now it only supports from VB.NET to C# and from C# to VB.NET.

VB.NET to C# and from C# to VB.NET Click Here

To use it you can either:
Start typing your code.
Copy and Paste the code in the Code Text Box.
Translate an entire file using the file upload.

Disclaimer:
No copy is done whatsoever of the code that you either type, or upload for translation. Everything is processed in the server in memory and returned immediately to the browser

Saturday 10 October, 2009

How to control maxlength of multiline textbox in aspnet

This is known issue in ASP.NET Standard Text box control when set its TextMode="MultiLine" the MaxLength property does not work.

There is a workaround to accomplish this using two ways :
1. RegularExpressionValidator : Following is the markup that will restrict the text box to maximum of 300 characters.
<asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine" Height="100px"
Width="320"></asp:TextBox> 
<asp:RegularExpressionValidator
ID="regComments" runat="server" ControlToValidate="txtComments"
ValidationExpression="^[\s\S]{0,500}$" ErrorMessage="Maximum 300 characters are allowed in comments box." Text="Maximum 300 characters are allowed in comments
box." > 
</asp:RegularExpressionValidator>

2.By JavaScript :
The MaxLength property of textbox control works fine if Multiline mode is not set, but if TextMode='Multiline' is set in aspx page then this property is of no use and user can input any number of characters inspite of setting maxlength.
Single line textboxes render as an HTML input tag - which supports a max
length.

The Multiline Textbox, renders as an HTML TextArea which doesn't support a
length property.

So, one workaround is to roll your own by calling a JavaScript function that
checks the length of the text entered. The function gets called on every
keypress and denies further data entry when the max length is reached. Of
course the JavaScript function needs to ignore some keys. This one should
work.

This example requires just very basic knowledge of java script so don’t be scared I promise this will be one of the easiest java scripts that can solve a complex problem.

Add the following to your Multiline box in aspx page:
<asp:TextBox Rows="5" Columns="80" ID="txtCommentsForSearch" MaxLength='500' onkeyDown="checkTextAreaMaxLength(this,event,'500');"  TextMode="multiLine" runat="server"> </asp:TextBox>
        

*txtCommentsForSearch-This is asp.net control that is having multiline property set

I have used MaxLength='500', same property you have to use in underlying javascript file also. I have also passed this length to the calling javascript method, so that in case the MaxLength is not accessible then can be picked from parameters of javascript method.

Add the following to your javascript file:
function checkTextAreaMaxLength(textBox,e, length)
{
    
        var mLen = textBox["MaxLength"];
        if(null==mLen)
            mLen=length;
        
        var maxLength = parseInt(mLen);
        if(!checkSpecialKeys(e))
        {
         if(textBox.value.length > maxLength-1)
         {
            if(window.event)//IE
              e.returnValue = false;
            else//Firefox
                e.preventDefault();
         }
    }   
}

function checkSpecialKeys(e)
{
    if(e.keyCode !=8 && e.keyCode!=46 && e.keyCode!=37 && e.keyCode!=38 && e.keyCode!=39 && e.keyCode!=40)
        return false;
    else
        return true;
}