Handsontable Grid - loading and saving data from aspx web page - c#

Is it possible to load and save data to/from a grid using c# code in the back from an aspx page, or does one have to use a web service (or PHP)? I have tried and failed using JSON.Net to map a very simple structure to code a backend structure
Is it possible to use JQuery (an ajax GET I presume) to make a call to a method in the backend code file (.aspx.cs)? I have tried using code from various posts on this forum, but there is little information on the backend code (c#), and all seem to refer to web services. Any help/advice would be much appreciated.
Here is the JavaScript code associated:
var handsontable = $container.data('handsontable');
$(document).find('button[name=load]').click(function () {
$.ajax({
url: "Default.aspx/getJSData",
dataType: 'json',
type: 'GET',
//contentType: "application/json; charset=utf-8",
success: function (res) {
handsontable.loadData(res.data);
$console.text('Data loaded');
},
error: function () {
$console.text('Load error');
}
});
});

You still need to do an Ajax call but you don't need to do a web service (you can). The function you want exposed to the Ajax call put the [WebMethod] Attribute on and an use a Script Manager with the EnablePageMethods attribute set to true.
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>
Method to Access:
[WebMethod]
public static void SomeFunction(string message, string name)
{
}
Ajax call using jQuery
(function($) {
$.ajax({
type: "POST",
url: "test.aspx/SomeFunction",
data: "{message:'Hello World', name: 'Bob'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert('success');
}
});
});
Reference: Using the WebMethod Attribute

Related

Asp.Net WebApi Routing when published under DefaultWebSite and seperate WebSite

From Asp.Net MVC UI(View) I am calling a Asp.Net WebApi through Ajax Call.
$.ajax({
type: "POST",
contentType: "application/json",
url: "api/UpdateEmployees",
data: undefined,
success:
function () {
EmployeesPage();
},
error: function (err) {
Error(err);
}
});
When I publish the WebSite in IIS as a Site it is working fine.But if I publish the WebSite under "Default Web Site" it is not working because it is expecting the url as "EmployeeWebSite/api/UpdateEmployees" where EmployeeWebSite is my virtual directory under "Default Web Site".
I should append url based on where the WebSite is hosted.Can anyone help me with this? I want my website to work in both cases.
I am deploying both ways using localhost.
use this code in layout.chtml or In your current View
<script type="text/javascript">
var baseurl = '#VirtualPathUtility.ToAbsolute("~/")';
</script>
After that u can use your Base URL varible
$.ajax({
type: "POST",
contentType: "application/json",
url: baseurl +'api/UpdateEmployees',
dataType: "json",
success:
function () {
EmployeesPage();
},
error: function (err) {
Error(err);
}
});
Tip : Dont put undefined in your $.ajax if you not parsing any data Remove it .
Have a grate day

Call code-behind function from javascript in visual webpart

Is it even possible? To call a code-behind c# function from javascript in a visual web part?
It is a complex function so converting all my codes to client side is not an option. I want the logic that is there in this function to happen without a page refresh. This is the background of my issue.
Thanks guys..
You can use jQuery ajax to call server side method and get the response to be used in javascript. This article has simple and good example to show what you need to do.
Code behind
public partial class _Default : Page
{
[WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
}
Javascript
$.ajax({
type: "POST",
url: "PageName.aspx/MethodName",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
}
});
Why don`t you use a Webservice (Ajax-Enabled WCF Service) which can be called via AJAX?
I think this would be the clean way. Put your logic in an extra class and use this class in the webservice and your webpart. Then you cann call the Method from Code and from Javascript.

Jquery Ajax MVC controller URL issue

I have a jquery code which opens a dialog. This dialog is a partial view which renders from the response of a jquery Ajax call "http://test.com/ControllerA/ViewDetails". Ajax call code looks like below
$.ajax({
url: "ViewDetails",
type: "GET",
dataType: "html",
The dialog box has button which has to make another Jquery Ajax call (this has go against a different controller and action). Ajax code looks like below.
$.ajax({
url: "ControllerB/Search",
type: "GET",
dataType: "html",
The above ajax call fails to find search action because The URL will get changed to http://test.com/ContollerA/ControllerB/Search.
I feel this is something related to a route config. But i need some directions from you all.
$.ajax({
url: "#Url.Action("ViewDetails", "ControllerA")",
type: "GET",
dataType: "html",
and
$.ajax({
url: "#Url.Action("Search", "ControllerB")",
type: "GET",
dataType: "html",
This way you're using the route table and not generating urls willy nilly
The best way is to use
url: '../ControllerA/ViewDetails'
it worked for me when the culture is in url

Calling a function in the .aspx.cs code behind file with jQuery load()

So I have something like this situation:
$(document).on('click', 'a[data-link]', function () {
var $this = $(this);
url = $this.data('link');
$("#imagePreview").load("imageProcess.aspx?" + url);
where url holds GET parameters. But imageProcess.aspx is different than the file I'm in (dashboard.aspx) and I need to reference some panels within my dashboard.aspx file. So my question is, using the .load() function, or even any function that could get the job done, how do I call a function, with GET parameters, in the dashboard.aspx code behind file? I'm fairly new to the .NET framework so I apologize if the question sounds elementary.
In your imageProcess.aspx.cs create a webmethod like:
[WebMethod]
public static string YourMethod(your parameters)
{
//Do Your Work
}
and in your dashboard page, in javascript use jquery to send request your webmethod like:
$.ajax({
type: "POST",
url: "imageProcess.aspx/YourMethod",
data: "{parameter1Name:'" + JSON.stringify(parameter1value) + "', Parameter2Name:'" + JSON.stringify(parmeter2Value) + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
// do your success work, keep in mind that your returned data will be in data.d
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
// do your failuer work
}
});
I hope it will give you a guidance to achieve your task.
Just to make sure: You are trying to access functionality from a different aspx page than the one you are currently on.
I'm not entirely sure if you can do that the easy way by java script. Maybe someone else knows a better way, but the way I would do it is creating ashx service page which will handle your request so you can provide the data you need (in your case an image)
For more information see http://www.dotnetperls.com/ashx

jquery ajax call return value

I have an asp.net application with a static page method. I'm using the below codes to call the method and get its returned value.
$.ajax({
type: "POST",
url: "myPage/myMethod",
data: "{'parameter':'paramValue'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {alert(result);}
});
What i got returned is [object Object].
Below is my static method. And I also have EnablePageMethods="true" EnablePartialRendering="true" in my ScriptManager.
[WebMethod]
[ScriptMethod]
public static string myMethod(string parameter)
{
return "Result";
}
Is there a way for me to get the returned value?
Try using Chrome developer tools or the firebug plugin from Firfox. Not sure if IE's developer tools lets you inspect the ajax calls?
The resulting string you are looking for is actually within the result object. You need to look at the d variable. I remember reading somewhere why this was, I think it is ASP.NET playing around :|
Try:
success: function(data) {alert(data.d);}
c#
[WebMethod]
public static string GetTest(string var1)
{
return "Result";
}
Hope this helps.
Its just that you are stuck at the .d that is introduced in the JSON response from ASP.NET 3.5. To quote Dave Ward,
If you aren’t familiar with the “.d”
I’m referring to, it is simply a
security feature that Microsoft added
in ASP.NET 3.5’s version of ASP.NET
AJAX. By encapsulating the JSON
response within a parent object, the
framework helps protect against a
particularly nasty XSS vulnerability.
So just check whether .d exists and then unwrap it. Change your success function like this.
success: function(result) {
var msg = result.hasOwnProperty("d") ? result.d : result;
alert(msg );
}
What about this?
$.ajax({
type: "POST",
url: "myPage/myMethod?paramater=parameter",
success: function(result) {
alert(result);
}
});
I found out the solution.
You can use parseJSON to get the result
http://api.jquery.com/jQuery.parseJSON/
or change the datatype to html to see the actual value.
http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests
Thank you guys for your help.

Categories

Resources