Return to Snippet

Revision: 32389
at November 6, 2011 00:18 by housecor


Updated Code
[WebMethod]
		public static string DeleteCaseNewsItem(int ID)
		{
			SSHPortalDataClassesDataContext db = new SSHPortalDataClassesDataContext();
			var newsItem = (from n in db.CaseNews
 							where n.CaseNewsID == ID
							select n).FirstOrDefault();

			if (newsItem == null) return "false";

			db.CaseNews.DeleteOnSubmit(newsItem);
			db.SubmitChanges();

			return "true";
		}

//and the js
	$('.delete_file').click(function () {
		if (confirm('Delete this file?')) {
			$(this).parent().slideUp('slow');
			$.ajax({
				type: "POST",
				url: "Manage.aspx/DeleteUploadedFile",
				data: "{ID: '" + $(this).attr('rel') + "' }",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function (msg) {
					if (msg.d != "true") alert('There was an error deleting the file.');
				},
		error: function (response) {
			var error = $.parseJSON(response.responseText);
			alert('Sorry, an error occurred. Please contact support. The error was: ' + error.Message);
		}
			});
		}
		return false;
	});

//---------------------------------
//a totally separate example getting a complex object returned in JSON.

$(document).ready(function () {
	makeDropdownID = $('#ddModels').data('make-dropdown-id');
	$('#' + makeDropdownID).change(function () {
		$.ajax({
			type: "GET",
			url: "/services/Inventory.svc/GetModels",
			data: "inventoryType=A&make=" + $(this).val(),
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function (data) {
				if (data.d.length == 0) { //so user just unselected make and went back to "Select Make" on that dropdown. Or error occured and no models were found.
					$('#ddModels').empty().append('<option value="">Select Make First</option>');
				} else {
					$('#ddModels').empty().append('<option value="">Select Model</option>');
				}

				$.each(data.d, function () {
					$('#ddModels')
							.append($('<option></option>')
							.attr('value', this.Name)
							.text(this.Name));
				});
			}
		});
	});
});


//Another separate example using JSON.stringify and json2.js library as a shim for older browsers that can't natively convert JS objects to JSON to send a complex object via ajax:
	$.ajax({
		url: "ManageSegment.aspx/GetPricingPreview",
		data: "{'pricingRuleRequest':" + JSON.stringify(GetPricingRuleRequest()) + "}",
		success: function (response) {
			RenderPreview(response.d);
		},
		error: function (response) {
			HandleAjaxError(response);
		}
	});

//Above example assumes ajax has been configured with default settings to keep the .ajax call simple, like this:

//Sets some default configuration options for ajax calls to make jQuery ajax calls shorter.
function InitAjaxSetup() {
	$.ajaxSetup({
		type: "POST",
		contentType: "application/json; charset=utf-8",
		data: "{}",
		dataType: "json",
		error: function (response) {
			HandleAjaxError(response);
		}
	});
}

//Handles any errors returned from ajax calls
function HandleAjaxError(response) {
	var error = $.parseJSON(response.responseText);
	if (error.Message == 'Authentication failed.') {
		alert('Your session has timed out. You will now be redirected to the login page.');
		window.location.reload();
		return true;
	}
	alert('Sorry, an error occurred. Please contact support. The error was: ' + error.Message);
}

Revision: 32388
at October 7, 2011 03:53 by housecor


Updated Code
[WebMethod]
		public static string DeleteCaseNewsItem(int ID)
		{
			SSHPortalDataClassesDataContext db = new SSHPortalDataClassesDataContext();
			var newsItem = (from n in db.CaseNews
 							where n.CaseNewsID == ID
							select n).FirstOrDefault();

			if (newsItem == null) return "false";

			db.CaseNews.DeleteOnSubmit(newsItem);
			db.SubmitChanges();

			return "true";
		}

//and the js
	$('.delete_file').click(function () {
		if (confirm('Delete this file?')) {
			$(this).parent().slideUp('slow');
			$.ajax({
				type: "POST",
				url: "Manage.aspx/DeleteUploadedFile",
				data: "{ID: '" + $(this).attr('rel') + "' }",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function (msg) {
					if (msg.d != "true") alert('There was an error deleting the file.');
				},
		error: function (response) {
			var error = $.parseJSON(response.responseText);
			alert('Sorry, an error occurred. Please contact support. The error was: ' + error.Message);
		}
			});
		}
		return false;
	});

//---------------------------------
//a totally separate example getting a complex object returned in JSON.

$(document).ready(function () {
	makeDropdownID = $('#ddModels').data('make-dropdown-id');
	$('#' + makeDropdownID).change(function () {
		$.ajax({
			type: "GET",
			url: "/services/Inventory.svc/GetModels",
			data: "inventoryType=A&make=" + $(this).val(),
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function (data) {
				if (data.d.length == 0) { //so user just unselected make and went back to "Select Make" on that dropdown. Or error occured and no models were found.
					$('#ddModels').empty().append('<option value="">Select Make First</option>');
				} else {
					$('#ddModels').empty().append('<option value="">Select Model</option>');
				}

				$.each(data.d, function () {
					$('#ddModels')
							.append($('<option></option>')
							.attr('value', this.Name)
							.text(this.Name));
				});
			}
		});
	});
});

Revision: 32387
at October 7, 2011 03:51 by housecor


Updated Code
[WebMethod]
		public static string DeleteCaseNewsItem(int ID)
		{
			SSHPortalDataClassesDataContext db = new SSHPortalDataClassesDataContext();
			var newsItem = (from n in db.CaseNews
 							where n.CaseNewsID == ID
							select n).FirstOrDefault();

			if (newsItem == null) return "false";

			db.CaseNews.DeleteOnSubmit(newsItem);
			db.SubmitChanges();

			return "true";
		}

//and the js
	$('.delete_file').click(function () {
		if (confirm('Delete this file?')) {
			$(this).parent().slideUp('slow');
			$.ajax({
				type: "POST",
				url: "Manage.aspx/DeleteUploadedFile",
				data: "{ID: '" + $(this).attr('rel') + "' }",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function (msg) {
					if (msg.d != "true") alert('There was an error deleting the file.');
				}
		error: function (response) {
			var error = $.parseJSON(response.responseText);
			alert('Sorry, an error occurred. Please contact support. The error was: ' + error.Message);
		}
			});
		}
		return false;
	});

//---------------------------------
//a totally separate example getting a complex object returned in JSON.

$(document).ready(function () {
	makeDropdownID = $('#ddModels').data('make-dropdown-id');
	$('#' + makeDropdownID).change(function () {
		$.ajax({
			type: "GET",
			url: "/services/Inventory.svc/GetModels",
			data: "inventoryType=A&make=" + $(this).val(),
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function (data) {
				if (data.d.length == 0) { //so user just unselected make and went back to "Select Make" on that dropdown. Or error occured and no models were found.
					$('#ddModels').empty().append('<option value="">Select Make First</option>');
				} else {
					$('#ddModels').empty().append('<option value="">Select Model</option>');
				}

				$.each(data.d, function () {
					$('#ddModels')
							.append($('<option></option>')
							.attr('value', this.Name)
							.text(this.Name));
				});
			}
		});
	});
});

Revision: 32386
at October 7, 2011 03:42 by housecor


Updated Code
[WebMethod]
		public static string DeleteCaseNewsItem(int ID)
		{
			SSHPortalDataClassesDataContext db = new SSHPortalDataClassesDataContext();
			var newsItem = (from n in db.CaseNews
 							where n.CaseNewsID == ID
							select n).FirstOrDefault();

			if (newsItem == null) return "false";

			db.CaseNews.DeleteOnSubmit(newsItem);
			db.SubmitChanges();

			return "true";
		}

//and the js
	$('.delete_file').click(function () {
		if (confirm('Delete this file?')) {
			$(this).parent().slideUp('slow');
			$.ajax({
				type: "POST",
				url: "Manage.aspx/DeleteUploadedFile",
				data: "{ID: '" + $(this).attr('rel') + "' }",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function (msg) {
					if (msg.d != "true") alert('There was an error deleting the file.');
				}
error: function (response) {
alert('Sorry, an error occured. Please contact us for assistance. Error: ' + response);
}
			});
		}
		return false;
	});

//---------------------------------
//a totally separate example getting a complex object returned in JSON.

$(document).ready(function () {
	makeDropdownID = $('#ddModels').data('make-dropdown-id');
	$('#' + makeDropdownID).change(function () {
		$.ajax({
			type: "GET",
			url: "/services/Inventory.svc/GetModels",
			data: "inventoryType=A&make=" + $(this).val(),
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function (data) {
				if (data.d.length == 0) { //so user just unselected make and went back to "Select Make" on that dropdown. Or error occured and no models were found.
					$('#ddModels').empty().append('<option value="">Select Make First</option>');
				} else {
					$('#ddModels').empty().append('<option value="">Select Model</option>');
				}

				$.each(data.d, function () {
					$('#ddModels')
							.append($('<option></option>')
							.attr('value', this.Name)
							.text(this.Name));
				});
			}
		});
	});
});

Revision: 32385
at June 25, 2011 03:11 by housecor


Updated Code
[WebMethod]
		public static string DeleteCaseNewsItem(int ID)
		{
			SSHPortalDataClassesDataContext db = new SSHPortalDataClassesDataContext();
			var newsItem = (from n in db.CaseNews
 							where n.CaseNewsID == ID
							select n).FirstOrDefault();

			if (newsItem == null) return "false";

			db.CaseNews.DeleteOnSubmit(newsItem);
			db.SubmitChanges();

			return "true";
		}

//and the js
	$('.delete_file').click(function () {
		if (confirm('Delete this file?')) {
			$(this).parent().slideUp('slow');
			$.ajax({
				type: "POST",
				url: "Manage.aspx/DeleteUploadedFile",
				data: "{ID: '" + $(this).attr('rel') + "' }",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function (msg) {
					if (msg.d != "true") alert('There was an error deleting the file.');
				}
			});
		}
		return false;
	});

//---------------------------------
//a totally separate example getting a complex object returned in JSON.

$(document).ready(function () {
	makeDropdownID = $('#ddModels').data('make-dropdown-id');
	$('#' + makeDropdownID).change(function () {
		$.ajax({
			type: "GET",
			url: "/services/Inventory.svc/GetModels",
			data: "inventoryType=A&make=" + $(this).val(),
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function (data) {
				if (data.d.length == 0) { //so user just unselected make and went back to "Select Make" on that dropdown. Or error occured and no models were found.
					$('#ddModels').empty().append('<option value="">Select Make First</option>');
				} else {
					$('#ddModels').empty().append('<option value="">Select Model</option>');
				}

				$.each(data.d, function () {
					$('#ddModels')
							.append($('<option></option>')
							.attr('value', this.Name)
							.text(this.Name));
				});
			}
		});
	});
});

Revision: 32384
at September 23, 2010 11:45 by housecor


Updated Code
[WebMethod]
		public static string DeleteCaseNewsItem(int ID)
		{
			SSHPortalDataClassesDataContext db = new SSHPortalDataClassesDataContext();
			var newsItem = (from n in db.CaseNews
 							where n.CaseNewsID == ID
							select n).FirstOrDefault();

			if (newsItem == null) return "false";

			db.CaseNews.DeleteOnSubmit(newsItem);
			db.SubmitChanges();

			return "true";
		}

//and the js
	$('.delete_file').click(function () {
		if (confirm('Delete this file?')) {
			$(this).parent().slideUp('slow');
			$.ajax({
				type: "POST",
				url: "Manage.aspx/DeleteUploadedFile",
				data: "{ID: '" + $(this).attr('rel') + "' }",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function (msg) {
					if (msg.d != "true") alert('There was an error deleting the file.');
				}
			});
		}
		return false;
	});

Revision: 32383
at September 23, 2010 11:40 by housecor


Initial Code
[WebMethod]
		public static string DeleteCaseNewsItem(int ID)
		{
			SSHPortalDataClassesDataContext db = new SSHPortalDataClassesDataContext();
			var newsItem = (from n in db.CaseNews
 							where n.CaseNewsID == ID
							select n).FirstOrDefault();

			if (newsItem == null) return "false";

			db.CaseNews.DeleteOnSubmit(newsItem);
			db.SubmitChanges();

			return "true";
		}

//and the js
	$('.delete_file').click(function () {
		if (confirm('Delete this file?')) {

			$.ajax({
				type: "POST",
				url: "Manage.aspx/DeleteUploadedFile",
				data: "{ID: '" + $(this).attr('rel') + "' }",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function (msg) {
					if (msg.d == "true") {
						$(this).parent().slideUp('slow');
					} else {
						alert('There was an error deleting the file.');
					}
				}
			});

			
		}
		return false;
	});

Initial URL


Initial Description
IMPORTANT: Be sure to clean the solution and rebuild after creating the WebMethod or you may get a 500 error.

Initial Title
Webmethod called via jquery ajax

Initial Tags


Initial Language
C#