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.



Muthu Kumar replied to Venkat K on 11-May-12 01:51 AM
hi friend 
i tried your coding also 
but it's not working 
my problem is if  ddp state enabled == true
i need to receive alert message on Label2 
Venkat K replied to Muthu Kumar on 12-May-12 03:04 AM
then why you can't think to change the condition for checking with enabled= true

  if (document.getElementById("ddpstate").enabled== true) {
 //put the alert logic here


dipa ahuja replied to Muthu Kumar on 13-May-12 09:12 AM
Page.RegisterClientScriptBlock("", "<script>alert('mail sent');</script>");