How to Run the JavaScript function from Code behind side in asp.net


Write the Sample JavaScript function in Souce code: For Example  I am  taking sample function as hide the division when radiobutton clicks Yes or No.
The following is the Hide() javascript function:

<script language="javascript" type="text/javascript">
        function Hide() {

            var yes = document.getElementById('rbtnlstUnderWarrenty_0');
            var no = document.getElementById('rbtnlstUnderWarrenty_1');
            if (yes.checked) {

                document.getElementById('divWarrenty').style.display = "block";

            }
            else if (no.checked) {

                document.getElementById('divWarrenty').style.display = "none";

            }

   </script>

Write the following code in code behind side to call the javascript file Hide() in button click protected void button_Click(object sender, EventArgs e)
{
       
     string Msg = "<script>Hide();</script>";             ScriptManager.RegisterClientScriptBlock(thistypeof(Page), "function", Msg,false);
}



1 comment:

  1. thank you for a very simple and easy-to-understand explanation.. there are multiple Google posts with PAGES of data trying to explain the very same thing.

    ReplyDelete