Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please Help me.

I have the following code.

The java script code has to be executed and send back the result so that i could execute my operation next line.

The problem is i get the confirm box after the click event gets executed.

Can any one provide me solution?

Thanks in advance

Java Script:
XML
<script type="text/javascript">
      function GetResult()
      {
       var ans = confirm('Are you the people from Planet Earth?');
        if(ans)
        document.getElementById("HiddenField1").value="YES";
        alert(ans)
       }
    </script>



c#:

C#
protected void Button2_Click1(object sender, EventArgs e)
      {
          bool india = true;
          if (india)
          {
              india = false;
          }
          if(!india)
          {
              ClientScript.RegisterStartupScript(GetType(), "text/javascript", "javascript:GetResult();", true);
              if (HiddenField1.Value == "YES")
              {
                // logic
              }
          }
      }
Posted
Updated 3-Aug-11 22:41pm
v2

You have to call the javascript in the button's OnClientClick event in design like below.
<asp:Button ID="btn" runat="server" OnClientClick="GetResult()"/>
 
Share this answer
 
Comments
walterhevedeich 4-Aug-11 4:53am    
Beat me to it. Have a 5.
Toniyo Jackson 4-Aug-11 4:56am    
Thanks walter :)
Rosarioraj 4-Aug-11 9:19am    
yes but u see.. I am executing some code in the button_click and then calling the javascript function. There makes the difference...
The problem is i get the confirm box after the click event gets executed.
That is because you are calling the Javascript function on the server side click event. Remove that line and on your asp:Button tag, add OnClientClick and then point it to your Javascript function.
 
Share this answer
 
Comments
Rosarioraj 4-Aug-11 9:19am    
yes but u see.. I am executing some code in the button_click and then calling the javascript function. There makes the difference...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900