Combination of JQL POST with classic asp

I've tried many ways to accomplish this but its not happening .. here's the deal. I have data displayed in rows of a table, each row has its own id representing the id of the person I want to delete. I am using jquery to collect this id from the string where my button is located, here is the code how I do it, I just write it work so you can understand what I am trying to accomplish:

var customer_id = $(this).parents("tr").attr('id');

      

When I test it with an alert it works, here is the tricky part that I just get stuck. I have a file named Delete.aspx, here is its content

<%

      

string p = Request ["value"]; int pInt = Int32.Parse (p);

var dataContext = new CustomersDataContext();
var customer = from m in dataContext.Customers
               where m.id == pInt
               select m;
dataContext.Customers.DeleteAllOnSubmit(customer);
dataContext.SubmitChanges();
%>

      

Now I'm trying to send the values ​​to Delete.aspx to delete a person with a specific ID and it works by typing Delete.aspx in the browser? value = 7, it will remove the person with id 7. Now where I am really stuck. From Default.aspx I am trying to link to Delete.aspx and pass the person id using jquery like this:

$(".btn-delete").click(function() {
                 var answer = confirm("If you press OK, this customer will be deleted?")
                 var customer_id = $(this).parents("tr").attr('id');
                 if (answer) {


                      $.ajax({
                     type: "POST",
                     url: "Delete.aspx",
                     data: "{value: '" + customer_id + "'}",
                     contentType: "application/json; charset=utf-8",
                     dataType: "json",
                     success: function(msg) {
                     AjaxSucceeded(msg);
                     },
                     error: AjaxFailed

                     });

                     $(this).parents("tr").animate({ backgroundColor: "#fbc7c7" }, "fast")
                .animate({ opacity: "hide" }, "slow")
                     return false;
                 }
                 else {
                     alert("Customer has not been deleted!")
                 }


             });

             function AjaxSucceeded(result) {
                 alert(result.d);
             }
             function AjaxFailed(result) {
                 alert(result.status + ' ' + result.statusText);
             }

         });   

      

So now the result is: When I click the button and confirm deletion, I get "500 Internal server error", is this a fix or is there another easy way to do the same?

thank


I changed the code .. but still I need help .. I feel like I'm so close, at least point me in the right direction ..

here is my Delete.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Second_Question
{
    public partial class Delete : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string p = Request["value"];
            int pInt = Int32.Parse(p);

            var dataContext = new CustomersDataContext();
            var customer = from m in dataContext.Customers
                           where m.id == pInt
                           select m;
            dataContext.Customers.DeleteAllOnSubmit(customer);
            dataContext.SubmitChanges();


        }
    }
}

      

here is my Delete.aspx

any ideas? thank

+2


source to share


1 answer


The "Internal Server 500 Error" usually occurs because you have server side code that either did not compile correctly or threw an unhandled exception.

I would advise you to try a few changes:

Move your delete routine to a specific method, decorated with the [WebMethod] attribute. Make sure your code includes System.Web.Services.

Have jQuery ajax call to include "/Delete.aspx/MyDeleteMethod" instead of the whole page.

Have you thought about security for this app? What happens if someone catches your customer_id and changes it on the fly using Firebug?



EDIT: Ok, here is what I would like to suggest for server-side code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services

namespace Second_Question
{
    public partial class Delete : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static void DeleteCustomer(int CustID)
        {

            var dataContext = new CustomersDataContext();
            var customer = from m in dataContext.Customers
                           where m.id == CustID
                           select m;
            dataContext.Customers.DeleteAllOnSubmit(customer);
            dataContext.SubmitChanges();


        }
    }
}

      

And my jQuery will look like this:

$.ajax({
      type: "POST",
      url: "Delete.aspx/DeleteCustomer",
      data: "{CustID: " + parseInt(customer_id) + "}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
                     AjaxSucceeded(msg);
               },
      error: AjaxFailed
 });

      

+1


source







All Articles