How to disable server side checkbox in javascript
CheckBox1.InputAttributes.Add(“disabled”, “true”);
What is the difference between RegisterStartupScript() method and RegisterClientScriptBlock() method?
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>”);
Decimal validation in javascript
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>
How to implement Auto-Logout functionality in asp.net
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’;
}
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>
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