This is critical things I was looking for many days..that is how to add javascript event to the server side ASP.NET control. This is very needed often but I could not manage it for a long time. However, this time I got some. We have to follow 2 steps.
1. Define client side function
2. Add function/event to the control.
1. Define the client side function
Suppose a text box...I want to limit total chars in it is 100. If we want to use javascript for this then code will be like this. This code will be scripted in the .aspx page...that means will be in the script tag in the main page.
function allowInsert(total) /*here total=100*/
{
var mybox=document.getElementById("mytextbox"); /* server control ID="mytextbox"*/
var text=mybox.value;
var len=text.length;
int mylen=0;
mylen=parseInt(len);
/*testing condition*/
if(mylen< total)return true;
else return false;
}
2. Adding function to the server control
In Page_Load event of .aspx.cs (code-behind) page we have to add the following line
Let mytextbox is the ID of the control.So....
mytextbox.Attributes.Add("onkeyup","javascript:return allowInsert(100)");
Now when a char will be entered in mytextbox then each time this function will be checked
whether it is allowed or not.
thanks.
Masud(14.7.9)
No comments:
Post a Comment