Tuesday, December 12, 2006

Function which returns the "Selected Texts" of a multiselect listbox

Function ReturnSelectedTexts(ByVal objControlName As Object) As String
'This function returns the selected texts of the multiselect listbox
Dim strSelectedTexts As String
Dim lstItem As ListItem
For Each lstItem In objControlName.items
If lstItem.Selected = True Then
strSelectedTexts = strSelectedTexts & lstItem.Text & ", "
End If
Next
strSelectedTexts = Trim(strSelectedTexts)
strSelectedTexts = Left(strSelectedTexts, Len(strSelectedTexts) - 1)
Return strSelectedTexts
End Function

'----Usage
lblSearchCriterias.Text = "Email verified: " & ReturnSelectedTexts(lstEmailVerified)

'--It will give the selected items as comma separated.

Cheers
Rajesh Sivaraman.

Sunday, December 10, 2006

Disallow some characters in a string...

function disallowCharacters(strText)
{
//The parameter to the function is the string which is to be validated
//The variable "strNotAllowed" below this line should contain the characters
// which are not allowed in the string
var strNotAllowed = ";:/\,&\\";
var checkStr = strText;
var allValid = true;
for (i = 0; i < strNotAllowed.length; i++)
{
if (strText.indexOf(strNotAllowed.charAt(i)) > 0)
{
allValid = false;
}
}
return allValid;
}

//----------------------------------
//how to use

if (document.Form1.txtemail.value!="")
{
//alert(disallowCharacters(document.Form1.txtemail.value));
if (!disallowCharacters(document.Form1.txtemail.value))
{
//alert('Invalid Character');
alert("Please enter a valid Email Address");
document.Form1.txtemail.focus();
return false;
}
else
{
bemail1=new RegExp(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/);
bemail2 = bemail1.exec(document.Form1.txtemail.value);

if(!bemail2)
{
alert("Please enter a valid Email Address");
document.Form1.txtemail.focus();
return false;
}
}
}

Function which allows only integers...

function IsInteger(varInt)
{
var checkOK = "0123456789";
var checkStr = varInt;
var allValid = true;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
return allValid;
}

//--------------------

//how to use

if (document.Form1.txtMainTelephone.value!="")
{
if (!IsInteger(document.Form1.txtMainTelephone.value))
{
alert("Invalid character entered in main telephone number!\nPlease enter only numbers.");
document.Form1.txtMainTelephone.focus();
return false;
}
}

Wednesday, September 27, 2006

Useful JS functions

//Trim function

function trimAll(sString)
{
while (sString.substring(0,1) == ' ')
{
sString = sString.substring(1, sString.length);
}
while (sString.substring(sString.length-1, sString.length) == ' ')
{
sString = sString.substring(0,sString.length-1);
}
return sString;
}

//Usage
if (trimAll(document.form1.txtForename.value)=="")
{
alert('Please enter ForeName');
document.form1.txtForename.focus();
return false;
}
////////////////////////////////////////////////////////////////

Wednesday, September 13, 2006

25 years ago......

25 years ago......

A program was ... a television show
An application was .. for employment
Windows were..... something u hated to clean
A cursor ... used profanity
A keyboard was ...a piano
Memory was..... something u lost with age
A CD was... a bank account
If u unzipped in public u went to jail
Compress was something u did to garbage
A hard drive was a long trip on the road
Log on was adding wood to fire
A backup happened to your toilet
A mouse pad was where a mouse lived
Cut.. u did with scissors
paste.. u did with glue
A web was a spiders home
And a virus was the flu!!

.. Times surely have changed :-)

Monday, September 11, 2006

how to make 2D arrays in javascript??

A basic way to make a 2D array 20 by 20 is like this:

var UserBoard = new Array(20);
for(var i=0;i<=21;i++)
{
UserBoard[i]==new Array(20);
}
UserBoard[4,5]="test";
alert("test:" + UserBoard[4,5]);

Reason I'm posting this is just for reference purposes since I've been trying to do this for hours, thinking it was UserBoard[4][5] but it's UserBoard[4,5] that you access it by. Got this from here.

Split like function in SQL SERVER 2000

ALTER PROC dbo.USP_GET_OPERATOR_CHAT_MESSAGES_TESTING(
@ChatIds VARCHAR(500)
)
AS
--USP_GET_OPERATOR_CHAT_MESSAGES_TESTING '1071,1072'
SET NOCOUNT ON
BEGIN
DECLARE @Str Varchar (100)
DECLARE @tab TABLE (ID INT)
SET @Str = @ChatIds + ',' --'4,5,6,'- pls note the string ends with the delimited char i.e. comma
WHILE @str <> ''
BEGIN
INSERT INTO @TAB (ID)
SELECT SUBSTRING(@Str, 1, CHARINDEX (',', @Str) -1)
SET @str = SUBSTRING(@Str, CHARINDEX (',', @Str) + 1, LEN(@Str) - CHARINDEX (',', @Str))
END

SELECT MessageID, IsOperator, Message FROM tblChatMessage
WHERE ChatId IN (SELECT * FROM @tab) ORDER BY ChatID, MessageID
END

Wednesday, August 30, 2006

JS function for validating internet URL

I am posting this since most of the Regular Expressions I got from the NET where not working on
non-IE browsers..

function validate()
{

if (document.Form1.TextBox1.value!='')
{
var urlRegxp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
var url = document.Form1.TextBox1.value
if(urlRegxp.test(url) != true)
{
alert('Please enter a valid External URL');
document.Form1.TextBox1.focus();
return false;
}
}
}

Cheers
Rajesh Sivaraman.

Friday, August 18, 2006

Setting focus to a textbox from codebehind

Dim fun As String

fun = fun & "Script language='javascript'" & vbCrLf

fun = fun & "function setFocus() {" & vbCrLf

fun = fun & "document.getElementById('txtDomainName').focus();" & vbCrLf

fun = fun & "}" & vbCrLf

fun = fun & "window.onload = setFocus;" & vbCrLf

fun = fun & "/Script" & vbCrLf

RegisterClientScriptBlock("Focus", fun)


On the 2nd and 7th lines above provide angle brackets "<" or ">" accordingly as blogger is not allowing me to post like as it is ;-)

Tuesday, July 11, 2006

Getting file name from URL...

What I wanted to achieve was, if the current page is
http://www.dubaidonkey.com/index.aspx I wanted to retrieve the name of the current page that is "index.aspx". And here goes the code for this. I dont know whether there is some easy method for this but this works fine for me...

Dim strURL() As String, strFileName As String
strURL = Request.Url.Segments
strFileName = strURL(strURL.Length - 1)

Hope someone will find it useful ;-)
Cheers
Rajesh Sivaraman.

Friday, June 23, 2006

Assigning a value to password textbox

Sometimes we need to display a page containing password in edit mode.
So here is the code in .NET for the same.

txtPassword.Attributes("value") = dsMember.Tables(0).Rows(0).Item("Password")
or simply txtPassword.Attributes("value") = "your password"

Friday, June 16, 2006

Redirect to login and redirection to same page using querystring

In Login.aspx.vb Login button click event

If Not Request.QueryString("Ref") = Nothing Then
Response.Redirect(Request.QueryString("Ref"), False)
Else
Response.Redirect("default.aspx", False)
End If

In all other pages' load event
If Session("UserID") Is Nothing Then
'Response.Redirect("Login.aspx", False)
Response.Redirect("Login.aspx?Ref=" & HttpContext.Current.Request.Url.PathAndQuery.ToString(), False)
Else
.
.
.
.
.
End if

Friday, March 31, 2006

Saturday, March 25, 2006

A good article on ASP.NET Performance

Got a good artice on ASP.NET performance today. Worth reading.
You too have a look.

http://msdn.microsoft.com/msdnmag/issues/05/01/ASPNETPerformance/

Tuesday, February 21, 2006

hosts.sam

Today I learnt something new, this is not regarding .NET but networking you can say.
We wanted to test our application by creating many subdomains in the pc, ie; our application needed subdomains for proper testing. Something like http://a.rajesh/dd/index.aspx

Just follow the steps below to achieve this.
Open
C:\WINDOWS\system32\drivers\etc\hosts.sam and in the end added the following

127.0.0.1 localhost
127.0.0.1 a.localhost
127.0.0.1 b.localhost
127.0.0.1 rajesh.rajesh
127.0.0.1 a.rajesh

and saved it.
Then open your browser and type http://a.rajesh/dd/index.aspx and there it goes your localhost will work same as the subdomains... :)

Friday, February 17, 2006

How to open email address in default mail client from a datagrid hyperlink.

How to open email address in default mail client from a datagrid hyperlink?

I was trying this for a long time. How to open the email address (displayed in a datagrid) in the default mail client. If you look at this it will seem very simple.
<asp:HyperLinkColumn DataNavigateUrlField="email" DataNavigateUrlFormatString="mailto:{0}" DataTextField="email" HeaderText="Email"> </asp:HyperLinkColumn>


And here is a link that you will find useful "Common DataGrid Mistakes"
http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnaspp/html/aspnet-commondatagridmistakes.asp

Cheers
Rajesh Sivaraman.

Wednesday, February 15, 2006

Debugging JavaScript using VS.NET 2003

Hi all, earlier I had seen someone debugging JS using VS.NET IDE. And it took a long time for me to find it out. And here it goes...
Before that Long Live "FireFox" and the JavaScript console it provided, without which life would have been miserable.

Now How to Debug JavaScript using VS.NET 2003?
First go to IE (again IE because it is the favourite browser of all)
Tools-->Internet Options-->Advanced and uncheck 'Disable script debugging'.
Now add "debugger" at the place from where you want to start debugging. For Eg;
function change()
{
var img; img = 1;
var path;
debugger alert(img);
path="images\\" + String(img)+ ".gif";
document.Form1.img1.src = path;
//alert(path);
}


Now run the application and there you are debugging the JS....
ps: I have called the js function onload of the body tag.

Some Javascript today....

Vijay wanted a function which will display an image whose number he will pass, for eg if the name of the image is 1.gif he will pass "1" to it.
At first we had trouble of "unterminated string constant" error, later found out that since we had only one slash ("\") in the images (since the image was inside images folder), and javascript has "\" as escape character. So we need to place two "back slashes" and we got it right.
function change()
{
var img;
img = 1;
var path; path="images\\" + String(img)+ ".gif";
document.Form1.img1.src = path;
alert(path);
}
This is a function prototype which I made, parameters need to be added to it.

After making all this and giving Vijay the code, I thought of the great blunder made by me.For the virtual path of the image I was using "\"--> the network slash where as it should be "/". Randeep had told me the same more than once and I am making the same mistake.
So the function will become something like this
function change()
{
var img; img = 1;
var path; path="images/" + String(img)+ ".gif";
document.Form1.img1.src = path;
alert(path);
}

Nice to see that I am repeating the mistakes :-(

Tuesday, February 14, 2006

Got some free time today...

Today I got some free time @ office.. Once in a blue moon day you can say.
So browsed for some .NET stuff and I am adding the links below. Thinking Sometime in future I will need it.

Creating a Class Library (DLL)
http://www.c-sharpcorner.com/2/pr12.asp

New Certifications Microsoft
1. http://www.microsoft.com/learning/mcp/newgen/2. http://www.microsoft.com/learning/mcp/newgen/faq/

Passing values from a page to another by means of the Context objecthttp://www.devx.com/vb2themax/Tip/18847

BuildUrlWithQueryString - Building an Url with a list of parameters in the querystringhttp://www.devx.com/vb2themax/Tip/19481

ShowMessageBox - Showing a client-side message box at the next page loadhttp://www.devx.com/vb2themax/Tip/19678

Manage User Control Values Using Javascript in an .aspx File
http://www.devx.com/tips/Tip/28032

Check All CheckBoxes in an ASP.NET DataGrid Using a Single CheckBox
http://www.devx.com/tips/Tip/20238

How to refer assemblies if different versions are available http://support.softartisans.com/FileUpEEV4/doc/install/install.asp#applevel

Conditional JS validation for IE and Non IE browsers
http://geekswithblogs.net/timh/archive/2006/01/19/66383.aspx


If a key PaymentRequired is "false" or if the key itself is not there then I want to redirect to some page else I want to redirect to another page.

Key in web.conf


In codebehind

If ConfigurationSettings.AppSettings("PaymentRequired") = "false" Then
context.Server.Execute("ByPass.aspx")
Else
Response.Redirect("package_details.aspx", False)
End If

I had used context.server.Execute since the whole code was inside a Try Catch block. If you use Server.Transfer inside a Try-Catch block it will throw exception.

Don't preserve viewstate when doing a Server.Transfer
http://www.devx.com/vb2themax/Tip/18744
http://www.it-pedia.com/