Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Can anyone give me a detailed explanation or resource regarding what happens when calling a
java-script function using Client-Script, inside an asp.net button click event. And my problem is if I call a java-script function from the 1st line, event won't stop and populate the java-script function. Instead it'll go through the whole C# code and then populate the java-script function.

Ex:

C#
<script type="text/javascript">
        function ShowPopUp() {
            var result = confirm("Do you want to proceed? ");
            alert(result);
        }
    </script>

C#
protected void btnClick_Click(object sender, EventArgs e)
{
   ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopUp();", true);
   for (int i = 0; i < 10; i++)
   {

   }
}

C#
<asp:button ID="btnClick" runat="server" text="Click here" OnClick="btnClick_Click" />
Posted
Updated 6-Jul-14 21:06pm
v2

I would suggest you to use OnClientClick Event.

So, the code would look like...
ASP.NET
<asp:Button ID="btnClick" runat="server" Text="Click here" OnClick="btnClick_Click" OnClientClick="return ShowPopUp();" />

And update the JavaScript function as...
JavaScript
<script type="text/javascript">
    function ShowPopUp() {
        return confirm("Do you want to proceed? ");
        //alert(result);
    }
</script>


So, when user would click Ok in confirm box, it would return you true, hence it will then fire the Button Click Server Side Event. Otherwise, it would return false and stay as it is.
 
Share this answer
 
Comments
rosharavinda 7-Jul-14 3:25am    
Thanks Tadit for your solution, but I want to know what happens when we call JS function using Client-script inside the C# button click Event. Please explain me what happens inside C# button click event.
Yes, actually you are right. It executes all the code and then executes the JavaScript code.
well what sais Tadit Dash is true.

usually i don't like to call javascript from c#, i prefer (if possible) to to the opposite,


here is a more client-like solution (need to include jquery.js in your page) :

HTML
<input type="button" id="btn" />
<script>
    $(function() {
        $("#btn").on("click", function() {

            if (confirm("Do you want to proceed? ") === true) {

                var jqxhr = $.ajax("YourPage.aspx/YourWebMethod")
                    .done(function() {
                    alert("success");
                })
                    .fail(function() {
                    alert("error");
                })
                    .always(function() {
                    alert("complete");
                });
            }
        });
    });
</script>


serverside in YourPage.aspx.cs
C#
[WebMethod] 
public static void YourWebMethod()
{
   for (int i = 0; i < 10; i++)
   {
     //..
   }
}


i like to keep every "gui" manipulation inside javascript, that's why i suggest this approach
 
Share this answer
 
v3

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