0

Is there a way to make a code behind cal from the javascript alert box. I am using a button on grid view. Upon click of it..it should alert a confirm box...and later make a cal to code behind function. This is the part of code behind cal.

protected void GridView1_RowDataBound(object source, GridViewRowEventArgs e)
{
    ......
    btnAlertStatus.Attributes.Add("onclick", "javascript:if(confirm('Are you sure you want to send an email " + ID+ " ?') == true) EmailTest(ID); ");
    ......
}

protected void EmailTest(int ID)
{
}

This way I am not able to access the send_Email....is there a better way to cal this function in rowboundevent

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • you can assign btnAlertStatus click event handler to EmailTest Function and then add attribute for javacript but for onclientclick instead of onclick – SMK Aug 10 '12 at 07:11

3 Answers3

1

JavaScript runs on the Client.

ASP.NET (CodeBehind) runs on the Server.

You cannot execute a method that lives on the server, directly from the client. What you commonly do is that you do new request that tells the server to run the method and then return the result back to the client.

In your case, listen for when the alert box is closed and then perform a postback to the server. You can also use ajax as @Kek suggested, to do this, take a look at jQuery or ASP.NET Ajax.

Community
  • 1
  • 1
Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
1

You can use WebMethod, take a look at Using jQuery to directly call ASP.NET AJAX page methods

Charlie
  • 1,292
  • 8
  • 7
0

You can but you need to use ajax or Jquery

in code behind create this test method

[System.Web.Services.WebMethod]
public static string SayHi()
{
    return "Hi";
}

Then in your aspx page right below the form tag you add

<asp:ScriptManager ID="scriptManager" EnablePageMethods="true" runat="server"/>

then add this code also in your aspx page

<script>
        function GetHi() {

            PageMethods.SayHi(onComplete);
        }

        function onComplete(result) {
            alert(result);
        }

        GetHi();
</script>
JohnnBlade
  • 4,261
  • 1
  • 21
  • 22