C# .NET - How to Call JavaScript Function on PageLoad Event?
Asked By Muthu Kumar on 10-May-12 09:29 AM
i have 1 drop down list and and one text box
i need to validate both drop down and text box
i validate drop down list using java script
i validated text box normal asp required field validation
when i click the submit button drop down list validation not working ..
i tried it page load event but it was not working
on load()
this.btnSubmit1.Attributes.Add("onClick", " Validatestate() return;");
jscript
function Validatestate() {
if (document.getElementById("ddpstate").disabled == false) {
if (document.getElementById("ddpstate").value == "") {
document.getElementById("Label2").style.display = "block";
return false;
}
else {
return true;
}
}
this is my java script
how can call this java script function on page load event using C#
Thanks
Venkat K replied to Muthu Kumar on 10-May-12 12:32 PM
This syntax on the Attributes add click is wrong:
this.btnSubmit1.Attributes.Add("onClick", " Validatestate() return;");
this above statement should be:
this.btnSubmit1.Attributes.Add("onClick", " return Validatestate();");
so if the javascript function return true then the Click event of the button at server side will be fired and if the javascript function return false then the on click of button won't be fired.