Javascript:
Writing JavaScript in code behind:
/*this is for showing alert*/
string strScript = "";
strScript = "<script language='javascript'>";
strScript += "alert('Candidate Moved Successfully'); ";
strScript += "</" + "script>";
RegisterClientScriptBlock("sCloseScript", strScript);
/*this is for showing alert and also closing the pop up and refreshing the parent form*/
string strScript = "";
strScript = "<script language='javascript'>";
strScript += "alert('Candidate Moved Successfully');window.opener.document.forms(0).submit();self.close()";
strScript += "</" + "script>";
RegisterClientScriptBlock("sCloseScript", strScript)
Make Invisible a particular column:
document.getElementById("trShortList").style.display = 'block';
document.getElementById("trImg").style.visibility = "hidden";
document.getElementById("cmbScoreMinD").disabled = true
Make Visible a particular column:
document.getElementById("trShortList").style.display = 'none';
document.getElementById("trImg").style.visibility = "visible";
document.getElementById("cmbScoreMinD").disabled = false
To check all Checkboxes in a Form:
<asp:checkbox id=”chk” onclick=”chkall(this)”/>
/*this function is used for selecting check box in data grid*/
Var win;
function chkall(chkid)
{
for (var i=0; i < document.frmInbox.elements.length;i++)
{
var e = document.frmInbox.elements[i];
if (e.type == "checkbox")
{
e.checked = chkid.checked;
}
}
}
To select at least one check box in the form
/*If you have select atleast one check box on the form*/
var delstart="false";
var elem=document.getElementById("frmInbox").elements;
for(var i=0; i< elem.length;i++)
{
if(elem[i].type=="checkbox")
{
if(elem[i].status==true)
{
delstart="true";
}
}
}
if(delstart=="false")
{
alert('Select at least one applicant.');
return false;
}
To close the child (pop up) window when the main window is closed:
var win;
function winclose()
{
if(win && win.open && !win.closed)
{
win.close();
}
}
Function Call in body tag: onunload="winclose();"
Calling Popup from javascript:
function CurrentOpening()
{
var Url='OpenCurrentOpening.aspx';
window.open(Url,'Opening','top=100,left=40,height=355,width=922,location=no,scrollbars=no,menubar=no,status=no,toolbar=no,resizable=0,directories=no');
}
To allow the user to upload only one type of document:
if(document.getElementById("txtUpldResume").value != "")
{
var vSpImgA = document.getElementById("txtUpldResume").value;
var vFilelstIndxA = vSpImgA.lastIndexOf('\\');
var vFileNameA = vSpImgA.substring((parseInt(vFilelstIndxA,10)+1),vSpImgA.length);
var vSpltDotA = vFileNameA.split(".");
var vWebImgExtnA = "doc,xls"; //It will allow only file type of DOC or XLS
var vImgExtnlstIndxA = vWebImgExtnA.indexOf(vSpltDotA[(vSpltDotA.length- 1)].toLowerCase()); //this will check whether the uploaded file is there in the
variable vWebImgExtnA, if its not finding any then it will return -1
if(vImgExtnlstIndxA == -1)
{
alert('Please select the file in *.doc Format.');
return false;
}
}
Confirm using java script:
function ConfirmDelete()
{
var vCnfm = confirm('Are you sure ? You Want to Delete');
if(vCnfm == true)
{
return true;
}
else
{
return false;
}
}
Date Validations:
var vExpDate = document.getElementById("txtExpiryDate").value;
var vHidDate = document.getElementById("txtHTodayDate").value;
var vEDate = new Date(vExpDate);
var vHDate = new Date(vHidDate);
if(document.getElementById("txtExpiryDate").value != "")
{
if (vEDate < vHDate)
{
alert("Expiry date cannot be less than Today Date.");
return false;
}
}
To get dropdown list selected value:
if(document.getElementById("ddlPGQual").options[document.getElementById("ddlPGQual").selectedIndex].text == "Others")
Or
fnJobType()
{
var intSelIndex = document.getElementById("ddlJobType").selectedIndex;
var strSelText = document.getElementById("ddlJobType").options[intSelIndex].text;
if(strSelText == "Contract")
{
document.getElementById("trContract").style.display = "block";
}
else
{
document.getElementById("trContract").style.display = "none";
}
}
Or
function fnQualification(vDropDownId,vTextBoxId)
{
var intSelIndex = document.getElementById(vDropDownId).selectedIndex;
var strSelText = document.getElementById(vDropDownId).options[intSelIndex].text;
if(strSelText == "Others")
{
document.getElementById(vTextBoxId).style.visibility = "visible";
}
else
{
document.getElementById(vTextBoxId).style.visibility = "hidden";
}
}
Calling the above function:
function PageLoadFunction()
{
document.getElementById("txtBasicQual").style.visibility = "hidden";
document.getElementById("txtPGQual").style.visibility = "hidden";
fnJobType();
fnQualification("ddlQualification","txtBasicQual");
fnQualification("ddlPGQual","txtPGQual");
}
To show modal dialog:
function ShowPopup()
{
window.showModalDialog('YourBreakStarts.aspx','New Employee','dialogHeight:300;dialogWidth:400;status:0;titlebar:0;toolbar:0;fullscreen:0;channelmode:0;help:0;');
}
To validate data grid rows:
function validate()
{
if(document.getElementById("dgWhitebound"))
{
dgs = document.getElementById("dgWhitebound");
for(var i=1;i {
if(dgs.rows[i])
{
if(dgs.rows[i].cells[4])
{
if(document.getElementById("ddlStatus").options[document.getElementById("ddlStatus").selectedIndex].text == dgs.rows[i].cells[4].innerText)
{
if(dgs.rows.length == 3) //Including Header & If only one callback is selected.
{
alert('Selected status option is already selected.'); document.getElementById("ddlStatus").focus();
return false;
}
else if(dgs.rows.length == 4) //Including Header & If two callbacks are already selected.
{
alert('Two Call Back Pendings are Completed for this Case.');
document.getElementById("ddlStatus").focus();
return false;
}
}
}
}
}
}
To Block Ctrl A in web page
function checkKP()
{
if(event.ctrlKey)
{
if((event.keyCode == 65) || (event.keyCode == 67) || (event.keyCode == 97) || (event.keyCode == 99))
{ event.returnValue = false; }
}
}
Validating Float Number
function fnValidateFloat(txt)
{
var keycode = event.keyCode;
var sFloat;
sFloat = txt.value.split(".");
var slength;
if(sFloat.length > 2)
{
alert('Please the enter amount in valid format.');
txt.focus();
return false;
}
else if(sFloat.length == 2)
{
if(sFloat[1].length > 2)
{
alert('Two digits can be entered after point.');
txt.focus();
return false;
}
else
return true;
}
else
return true;
}
To Copy Grid Data:
function doCopy()
{
document.Form1.btnCopy.disabled = true;
dg = document.getElementById("dgReport").id;
str = "<html><body><table >"+document.getElementById("dgReport").innerHTML+"</table></body ></html>";
document.Form1.hgridtext.value = str;
CopiedTxt = document.Form1.hgridtext.createTextRange();
CopiedTxt.execCommand("Copy");
document.Form1.hgridtext.value = "";
document.Form1.btnCopy.disabled = false;
alert("Data Copied. Now you can paste data in a excel sheet.");
}
Print Using JavaScript:
function CallPrint(strid)
{
document.getElementById(strid).style.visibility = "visible";
var prtContent = document.getElementById(strid);
var strOldOne=prtContent.innerHTML;
var WinPrint = window.open('','dsds','left=10,top=10,width=1,height=1,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
prtContent.innerHTML=strOldOne;
document.getElementById(strid).style.visibility = "hidden";
}
To check whether all the dropdowns are selected
function sValidate(iFrm)
{
var bFlg = '1';
for (i=0;i {
if (iFrm.elements[i].type == "text")
{
if(iFrm.elements[i].id.indexOf("_") != -1)
{
var vSpltId = iFrm.elements[i].id.split("_");
if(vSpltId[0] == "GrdStaging")
{
if (iFrm.elements[i].value == "")
{
bFlg = '0';
break;
}
}
}
}
}
if(bFlg == '0')
{
alert('All Text Fields Must be Filled...');
return false;
}
else
{
return true;
}
}
To check whether similar data are given for gridview textbox
function sValidText(iFrm)
{
var bFlg = '1';
var myArray = new Array();
for (i=0;i {
if (iFrm.elements[i].type == "text")
{
if(iFrm.elements[i].id.indexOf("_") != -1)
{
var vSpltId = iFrm.elements[i].id.split("_");
if(vSpltId[0] == "GrdStaging")
{
if(myArray.length == 0)
{
myArray.push(iFrm.elements[i].value);
}
else if(myArray.length>0)
{
var vBlFlg = '0';
for(j=0;j {
if(myArray[j].toUpperCase() == iFrm.elements[i].value.toUpperCase())
{
vBlFlg = '1';
bFlg = '0';
break;
}
}
if(vBlFlg == "0")
{
myArray.push(iFrm.elements[i].value);
}
}
}
}
}
}
if(bFlg == '0')
{
alert('Staging options are similar. Kindly change it.');
return false;
}
else
{
return true;
}
}
To close pop up window and refresh the parent page
string strScript="";
strScript = "<script language='javascript'>";
strScript += "window.opener.document.forms(0).submit();self.close()";
strScript += "</" + "script>";
RegisterClientScriptBlock("sCloseScript", strScript);
How to add and remove to Array in Java Script
var sel = new Array();
Rome From Array
var i = "";
if((sel.length) > 0)
{
for(i=0;i {
if(sel[i] == (document.Form1.selvalue1.value+"|"+rid+"|"+cid))
{
sel.splice(i,1);
document.Form1.oldvalue.value = sel;
}
}
}
Adding to Array
sel.push("("+document.Form1.selvalue1.value+"|"+rid+"|"+cid+")");
var len= document.Form1.lstbox.length;
var contSearch = 1;
var selIndex;
if(len != 0)
{
document.Form1.txtnumber.value = "";
if(document.Form1.lstbox.selectedIndex == -1)
{
alert("No item selected.");
return false;
}
while(contSearch > 0)
{
selIndex = document.Form1.lstbox.selectedIndex;
if(selIndex >=0)
{
document.Form1.lstbox.options[selIndex] = null;
--numitems;
}
else
{
contSearch=0;
}
}
}
else
{
alert("No Items in the List Box");
return false;
}
To add from DropDown to ListBox and check whether already added
var len1 = document.Form1.lstbox.length;
var lvalue = document.Form1.drpbox.options[document.Form1.drpbox.selectedIndex].value;
var i;
if(len1 != 0)
{
for(i=0;i {
if(lvalue == document.Form1.lstbox.options[i].value)
{
cnt = 1;
alert("Item already exists."); return false;
}
}
}
else
{
alert("No items in the listbox");
return false;
}
To add to List Box
var addOption;
addOption = new Option(“A”,”A”);
document.Form1.lstbox.options[numitems++] = addOption;