Monday, August 13, 2007

Create TextArea control with working Maxlength property

We have found that text area (Multiline textbox) control in asp.net doesn't support the multiline property even you define that property.

so here is the solution for this problem

Code in .net Page

protected override void OnPreRender(EventArgs e)
{

if (MaxLength > 0 && TextMode == TextBoxMode.MultiLine)
{

// Add javascript handlers for paste and keypress
Attributes.Add("onkeypress", "doKeypress(this);");
Attributes.Add("onbeforepaste", "doBeforePaste(this);");
Attributes.Add("onpaste", "doPaste(this);");

// Add attribute for access of maxlength property on client-side
Attributes.Add("maxLength", this.MaxLength.ToString());

// Register client side include - only once per page

}
}

Code in JavaScript file

// Keep user from entering more than maxLength characters
function doKeypress(control)
{

maxLength = control.attributes["maxLength"].value;

value = control.value;

if(maxLength && value.length > maxLength-1)
{

event.returnValue = false;

maxLength = parseInt(maxLength);

}
}

// Cancel default behavior
function doBeforePaste(control)
{

maxLength = control.attributes["maxLength"].value;

if(maxLength)
{

event.returnValue = false;

}
}

// Cancel default behavior and create a new paste routine
function doPaste(control)
{

maxLength = control.attributes["maxLength"].value;

value = control.value;

if(maxLength)
{

event.returnValue = false;

maxLength = parseInt(maxLength);

var oTR = control.document.selection.createRange();

var iInsertLength = maxLength - value.length + oTR.text.length;

var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);

oTR.text = sData;

}
}


using this code you can even check the copy paste and restrict the number of characters

now create a custom control and then use that control

set MaxLenght property of that control and you will be able to restrict the number of characters

Sunday, August 12, 2007

Code Optimization Tool for C#

I just find one nice tool for the day to day development, and instant code optimization at the time of writing the code.

Once you install this tool it will tell you in the IDE itself, that which part of the code need to be change, not use full, wrong interation looping, wrong condition and all such a small mistake that may happened into day to day activities.


You can download it from
http://www.jetbrains.com/resharper/download/index.html#cSharp

Install at your own risk as i have installed it on several PCs but on one PC it removed intellisense from IDE.

Friday, August 10, 2007

Can you call private member of a class from another class?

Can you call private member of a class from another class?

Very strange question right?

but answer is yes. Shocked???

Check out the following code.

It is done by dot net framwork reflection

suppose we have a class


public class Class1
{

public Class1()
{


}

private string IAmCalled()
{


return "I am private member and I am called by somebody. shame on me.";

}

}

Add following line to other class/page from which you want to access private member

using System.Reflection;

Just write follwoing code in other class/page and you will see the magic


Class1 cls = new Class1();

Type t = typeof(Class1);

object obj = t.InvokeMember("IAmCalled", BindingFlags.InvokeMethod BindingFlags.NonPublic BindingFlags.Instance, null, cls, null);

Response.Write(obj.ToString());

It works!!!

please share if you have some more information.

Wednesday, August 8, 2007

How to Check upload file type using RegularExpressionValidator

You can check upload file type client side using RegularExpressionValidator as shown below

use ValidationExpression as follows

ValidationExpression="(.*\.([jJ][pP][gG]|[jJ][pP][eE][gG]|[pP][nN][gG]|[tT][iI][fF][fF])$)"

This expression restricts upload to the file type of .jpg, .jpeg, .tiff
you can edit validation expression as per your need.

Tuesday, August 7, 2007

How to Create / Insert Table Data From Remote server in Sql Server 2005 to local server


OPENROWSET can be used to access remote data from OLE DB data sources only if the DisallowAdhocAccess registry option is explicitly set to 0. When this option is not set, the default behavior does not allow ad hoc access.

To set DisallowAdhocAccess :-

Execute below script:

EXEC sp_configure 'show advanced options', 1;
GO

RECONFIGURE;
GO

EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
GO

RECONFIGURE;
GO

After that execute following statement

select * into Destination_table from

(SELECT a.*

FROM OPENROWSET('SQLOLEDB','SERVER';'USER';'PASSWORD',

'SELECT * FROM Database.dbo.SourceTableName') AS a ) a

first it will create Destination_table in our local server , and after it will copy all data from Remote server database "Database.dbo.SourceTableName" into Destination_table.

How to select number of rows from table in sql server

Hello

Here are two ways to select number of rows when count is passed in parameter

1. using TOP

declare @count int

set @count = 5

select top(@count) * from temp_Users


2. using ROWCOUNT


declare @count int

set @count = 5

set rowcount @count

select * from temp_Users


Please comment more ways of performing this functionality.

set rowcount 0

Silverlight Tutorials, Silverlight Videos, Silverlight Articles, Silverlight Blog, Silverlight Example and Application

Good Website consist of plenty of links for Silverlight Introductions and Overviews :

It consist of Silverlight Introduction Resource, such how to get started with.

Silverlight Sites - It consist of Silverlight Site from microsoft.
Silverlight Blogs - It consist of blog post on silverlight from well known authors.
Silverlight Articles - It consist of various articles on silverlight
Silverlight Videos - It consist of Silverlight Videos.
Silverlight Applications - It consist of Silverlight Examples and Demo Applications.
Silverlight Downloads - It consist of different Silverlight Version to download.
Silverlight Resources - And more on Silverlight Resource.

Logon to get Silverlight Resource and Information

I found this interesting post on http://dotnetguts.blogspot.com/

I feel that i must share this with you.