Monday, August 10, 2009

Useful Javascript with Asp.Net

Open Popup Window in the Center of the screen.
=============================================
function OpenPop(ItemID)
{
var LeftPosition = (screen.width) ? (screen.width-505)/2 : 0;
var TopPosition = (screen.height) ? (screen.height-250)/2 : 0;

window.open('Pop_QuoteInfo.aspx?id='+ItemID,'MyTitle','height=250,width=505, top='+ TopPosition +', left='+LeftPosition+', scrollbars=yes, resizable=yes, status=no');
}

Javascript Trim() Function
=========================

function trim(inputString) {
// Removes leading and trailing spaces from the passed string. Also removes
// consecutive spaces and replaces it with one space. If something besides
// a string is passed in (null, custom object, etc.) then return the input.
if (typeof inputString != "string") { return inputString; }
var retValue = inputString;
var ch = retValue.substring(0, 1);
while (ch == " ") { // Check for spaces at the beginning of the string
retValue = retValue.substring(1, retValue.length);
ch = retValue.substring(0, 1);
}
ch = retValue.substring(retValue.length-1, retValue.length);
while (ch == " ") { // Check for spaces at the end of the string
retValue = retValue.substring(0, retValue.length-1);
ch = retValue.substring(retValue.length-1, retValue.length);
}
while (retValue.indexOf(" ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
retValue = retValue.substring(0, retValue.indexOf(" ")) + retValue.substring(retValue.indexOf(" ")+1, retValue.length); // Again, there are two spaces in each of the strings
}
return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

How to set Default Button in Asp.Net
===============================

Just, Simply add this code to your page_load() event in code file.

txtUserID.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnLogin.UniqueID + "').click();return false;}} else {return true}; ");

Enable / Disable Validator Control by Javascript
========================================

function EnableDisableControl()
{
var ctl1 = document.getElementById('RequiredValidator1');

// Disable
ValidatorEnable(ctl1, false);

// Enable
ValidatorEnable(ctl1, true);
}

Set/Get value from Popup page and Parent Page
========================================

Get Value in Popup page from Parent Page
---------------------------------------------------
// Write this Javascript in Popup Page

var ParentIDValue = window.opener.document.getElementById('').value;
alert(ParentIDValue);

Set Value in Parent Page from Popup Page
--------------------------------------------------

// Write this Javascript in Popup Page

window.opener.document.getElementById('').value = "Your Value From Popup Page..";

That's it !!

Hope you will like it.

No comments: