Archive

Posts Tagged ‘Javascript’

How to disable server side checkbox in javascript

CheckBox1.InputAttributes.Add(“disabled”, “true”);

Categories: ASP.NET, Javascript Tags: ,

What is the difference between RegisterStartupScript() method and RegisterClientScriptBlock() method?

March 24, 2010 Leave a comment

Both, RegisterStartupScript() method and RegisterClientScriptBlock() method will inject Javascript code that will fire during start up of subsequent postback.

The RegisterClientScriptBlock method inserts the client-side script immediately below the opening tag of the Page object’s element. The code cannot access any of the form’s elements because, at that time, the elements haven’t been instantiated yet.

The RegisterStartupScript method inserts the specified client-side script just before the closing tag of the Page object’s element. The code can access any of the form’s elements because, at that time, the elements have been instantiated.

RegisterClientScriptBlock is meant for functions that should be “available” to the page. For this they are rendered at the start of the HTML file. In this case the page content will be blocked.

RegisterStartupScript is meant for commands that should execute on page load (at the client), so that page needs to be available for the script. This script is rendered at the end of the HTML file. In this case the content of the page will diplayed first and then script will run.

The choice of which method to use really depends on the “order” in which you want your script to be run by the browser when rendering the page.

code

if (!Page.IsStartupScriptRegistered(“CH”))

Page.RegisterStartupScript(“CH”, “<script>alert(‘Hello Friend’);</script>”);

if (!Page.IsClientScriptBlockRegistered(“CH”))

Page.RegisterClientScriptBlock(“CH”, “<script>alert(‘Hello Friend’);</script>”);

Display the progress bar on page load and postback

Just copy the following javascript code on the aspx page.
———————–code————————————–
<script language=”javascript”>
ShowWindowLoading();
//This method will fire before any controls/content is loaded on the client-side
function ShowWindowLoading()
{

try
{
var divLoading = document.getElementById(“divLoadingMsg”);
var fromleft=(alertSize()/2)/2 +50;
if (divLoading == null || divLoading == ‘undefined’)
{

var k=”<table>”;

k +=”<tr>”;
k+=”<td>”;
k +=”<img ID=’LoadingImg’  src=’../images/loading1.gif’ />”;
k +”</td>”;
k +=”</tr>”;
k +=”</table>”;

document.write(“<div id=’divLoadingMsg’ style=’position:absolute; left:”+ fromleft +”px;  background-color:#ececec;’>”+ k + “</div>”);
}
else
{
divLoading.style[“display”] = “”;
}

}
catch (ex)
{

alert(ex);
}
return true;
}
function alertSize() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == ‘number’ ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in ‘standards compliant mode’
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
return myWidth;

}
function HideWindowLoading()
{
try
{
document.getElementById(“divLoadingMsg”).style[“display”] = “none”;
}
catch (ex) { }
}

if(window.addEventListener){ // Mozilla, Netscape, Firefox

window.addEventListener(‘load’, HideWindowLoading,false);

} else { // IE
window.attachEvent(‘onload’, HideWindowLoading);
}

</script>

——————————-End——————————————

Decimal validation in javascript

December 29, 2009 Leave a comment

This validation checks the length (2) after decimal places and number is less than 100 and only numbers and decimal can be entered in the textbox.

Copy and paste the following html.

—————————————————–

<head>
<script>
function numbersonly(e,control){
var unicode=e.charCode? e.charCode : e.keyCode
if (unicode!=8 && unicode!=46){ //if the key isn’t the backspace key (which we should allow)
if (unicode<48||unicode>57) //if not a number
return false //disable key press
var character = String.fromCharCode(unicode);
var val = control.value+character

if(val>100)
{
return false
}

if(String(val).indexOf(“.”) !=-1)
{
if (String(val).indexOf(“.”) < String(val).length – 3) {
return false
}
}
}
}

</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<input type=”text” id=”txt” onKeyPress=”return numbersonly(event,this)” />
</form>
</body>

Categories: Javascript Tags:

How to implement Auto-Logout functionality in asp.net

August 10, 2009 Leave a comment

Here is small code which auto logout the user after 3 minutes.

Untitled Page

// initialize timers
var timer1, timer2;

// resetTimer is a function which reset the timer. if user press any key or move mouse then the timer will reset.
document.onkeypress=resetTimer;
document.onmousemove=resetTimer;

function resetTimer()
{

document.getElementById(‘message’).style.display=’none’;

//clearTimeout() – cancels the setTimeout(). it is inbuilt javascript function.

clearTimeout(timer1);
clearTimeout(timer2);

// waiting time. it is 3 minutes
var waitmin=3;

// alert the user one minute before
timer1=setTimeout(“alertuser()”, (60000*waitmin)-1);

// logout user
timer2=setTimeout(“logout()”, 60000*waitmin);
}

function alertuser()
{
document.getElementById(‘message’).style.display=’block’;
}

function logout()
{
window.location.href=’login.aspx’;
}

Your session is going to expire in 1 minute.
Categories: ASP.NET, Javascript Tags: ,

set cursor position through javascript and c#

In c#(asp.net)

protected void Page_Load(object sender, EventArgs e)
{
StringBuilder myJavaScript = new StringBuilder();

myJavaScript.Append(“<script language=’Javascript’> function setfocus(){“);
myJavaScript.Append(“document.getElementById(\”” + txtUserName.ClientID + “\”).focus();”);
myJavaScript.Append(“} setfocus();</script>”);
this.RegisterStartupScript(“SetFocusScript”,myJavaScript.ToString());

}

In javascript:

<script type=”text/javascript”>
function SetCursorToTextEnd(textControlID)
{
var text = document.getElementById(textControlID);
if (text != null && text.value.length > 0)
{
if (text.createTextRange)
{
var FieldRange = text.createTextRange();
FieldRange.moveStart(‘character’, text.value.length);
FieldRange.collapse();
FieldRange.select();
}
}
}
</script>

Categories: Javascript Tags:

jQuery instead of Javascript

Trying to use jQuery instead of Javascript (you can find it on http://www.jquery.com), it has number of advantages,

1. The biggest advantage of jQuery is it has CrossBrowser compatibiltiy, Every browser supports jQuery.

2. Easy to use.

3. It has lots of plugin

For more information go to http://www.jquery.com

Leave a Comment :, more…

Categories: Javascript, JQUERY Tags: ,