ITDeveloper
ITDeveloper

Reputation: 47

how to redirect from aspx page to a method in another aspx page

I have a Delete button in aspx page form when I click on it I want to be redirected to a specific method in another aspx page to be executed which is the delete method in the page that has the gridview to delete that row. How can I do that in asp.net c# in WebForms please? Thanks

Upvotes: 0

Views: 839

Answers (1)

Win
Win

Reputation: 62260

Traditionally, client browser cannot call directly to a specific method in ASP.NET Web Form.

However, you can make a web method, and make a call like this -

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <button type="button" onclick="postData();">Post Data</button>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
        </script>
        <script type="text/javascript">
            function postData() {
                var user = { id: 123 };
                $.ajax({
                    type: "POST",
                    url: '<%= ResolveUrl("~/default.aspx/delete") %>',
                    data: JSON.stringify(user),
                    contentType: "application/json",
                    success: function (msg) {
                        console.log(msg.d);
                    }
                });
            }
        </script>
    </form>
</body>
</html>

Code Behind

public partial class Default : System.Web.UI.Page
{
   [System.Web.Services.WebMethod]
   public static string Delete(string id)
   {
      // Do something
      return new JavaScriptSerializer().Serialize(id + " is successfully deleted");
   }
}

Upvotes: 1

Related Questions