I want to get access to a method in code behind when I clicked an span in my view aspx:
DEFAULT.ASPX VIEW CODE:
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<%-- MY SPAN --%>
<span runat="server" onclick="ShowChartSpider(this.id)" id="group_2" style="cursor: pointer" class="pull-right">My Span</span>
<%-- JAVASCRIPT CODE --%>
<script type="text/javascript">
function ShowChartSpider(group_id) {
$.ajax({
type: "POST",
url: "Default.aspx/MethodToCreateChart",
dataType: "json",
data: "{'parameter1':" + JSON.stringify(group_id) + "}",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert("all correct");
},
error: function (data) {
alert("no");
}
}
);
}
</script>
</asp:Content>
DEFAULT.ASPX.VB CODE BEHIND:
<WebMethod()>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Shared Sub MethodToCreateChart(sender As Object, e As EventArgs)
' My code to create the chart .....
End Sub
if I run the page, and inspects the page with the browser to see errors, none appear, but the code does not reach the breakpoint that I put in the method in codebehind.
What I´m doing wrong?
I would appreciate suggestions, thanks.
First check your server allow non HTTPS request.
i had that type of issue and my server didn't allow me to do that.
it so then disable that and test.
Then check the response status.
error: function(xhr, status) {
alert(xhr.status); }
let us know that is the outcome.
--Ruhul
Go to "RouteConfig.vb" under "App_Start" folder.
Change following line
settings.AutoRedirectMode = RedirectMode.Permanent
To
settings.AutoRedirectMode = RedirectMode.Off
i think your method code returning something like this.
Return Default.aspx/MethodToCreateChart
so me your MethodToCreateChart logic.
you can try with below sample method. your internal server error are coming because you are returning somethng from your method.
Public Shared Function MethodToCreateChart(parameter1 As String) As String
Return "Hello " & Environment.NewLine & "The Current Time is: " & _DateTime.Now.ToString()
End Function
I think you should remove 'runat=server' attribute from span tag.
You use aspx file same as ashx files. So have a look at below links:
1. How do you debug ASP.net HTTPHandler
2. Can't debug ASHX handler
Related
I have a website that uses a ReportViewer to display a SSRS serverside report. I also have controls/textboxes that are used to update the data behind using jQuery/AJAX. I want to try to avoid any page reloads.
I am now trying to refresh the ReportViewer after I have updated my data, without having the page reloaded. Unfortunately, I don't understand if there is a way to (re)load the new data into the ReportViewer. Using the refreshReport method in the ReportViewer control only rerenders with the old data.
Data gets updated with this code, the ReportViewer / report refreshes, but with the old data.
Can I get the new data from the database into the report without having some kind of page reload?
<script type="text/javascript">
function SaveData() {
var data = { text: $('#textBox').val() }
$.ajax({
type: "POST",
url: "ajax.aspx/SaveData",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
}
});
}
function OnSuccess(response) {
var clientViewer = $find("MainReportViewer");
if (clientViewer) {
clientViewer.refreshReport();
}
}
</script>
<rsweb:ReportViewer ID="MainReportViewer" runat="server" ProcessingMode="Remote" Width="100%" Height="100%" AsyncRendering="False" SizeToReportContent="True" ShowToolBar="False" ShowParameterPrompts="False">
<ServerReport ReportPath="/Rep/Rep" ReportServerUrl="http://localhost/reportserver" />
</rsweb:ReportViewer>
<button id="SaveButton" name="SaveButton" type="button" onclick="SaveData()" class="ui-button ui-widget ui-corner-all ui-button-icon-only"><span class="ui-icon ui-icon-disk"></span> </button>
Follow the below steps to make it work:
Use The Timer like ASP.net Timer and JavaScript Timer
refresh Data From Database every Tick and Use Tick Event
the Elapse Time Out then Your Data Reload withOut Page Refresh
Dude these steps definitely work.
any queries please ask in comments.
I am calling .asmx service from inside script tag in aspx page. When I view the page source it's showing my webservice.
How to protect the webservice from expose?
cause when some one view the page source then he can see the web service name as well as its all methods name.
.aspx Page:
<asp:PlaceHolder ID="divBody" runat="server">
<div class="aa-dashboard-wrapper">
<asp:Textbox id="textbox1" runat="server"></asp:Textbox>
</div>
</asp:PlaceHolder>
Here is my script tag, that is placed at the bottom of my aspx page.
$("#textbox1").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/WebService.asmx/webServiceMethod") %>',
data: "{ 'keyword': '" + request.term + "'}",
dataType: "json",
timeout: 20000,
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
},
error: function (response) {
},
failure: function (response) {
}
});
},
select: function (e, i) {
},
minLength: 2
})
</script>
While you can't stop users from seeing the URL of your service, you can stop them viewing the documentation for it - just put this inside the system.web tag of your web.config
<webServices>
<protocols>
<remove name="Documentation" />
</protocols>
</webServices>
See https://support.microsoft.com/en-us/kb/815149 on 'Removing the Documentation Protocol'
The best way is to have a JS file and place your AJAX call there. Even in this case, user can browse and view the JS, but the chances are little less.
There is no way to completely hide your API endpoints from someone who wants to find it. Even if you somehow obfuscate the calling of it in some javascript construction, it will still be visible in Chrome web inspector. And at the most basic level, I can still see it when I intercept the communication with packet sniffers.
I am playing with C# and jQuery (Ajax). In a effort to keep the C# code as short and clean as possible when creating a Ajax request, without using the Encosia example (in short without ASP.NET for the Ajax calls). The response can be XML, JSON or plain (parts) HTML.
I got a working version, but is this preferable to do? Or should I consider a other method?
I do get 1 warning (for the ajax.aspx page) in VS2010 express (a search of this error doens't gives me a satisfied answer)
Warning
Validation (): Element 'html' occurs too few times.
default.aspx
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div id="Result"></div>
<script type="text/javascript" language="javascript">
$(function () {
$.ajax({
url: "AJAX.aspx",
success: function (data) { $("#Result").html(data); }
});
});
</script>
</asp:Content>
ajax.aspx (there is only this line on this page)
<%# Page Language="C#" AutoEventWireup="true"
CodeFile="AJAX.aspx.cs" Inherits="AJAX" %>
ajax.aspx.cs
using System;
using System.Text;
using System.Data;
public partial class AJAX : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.Write("Hello world");
Response.End();
}
}
well it's like the error says: you have to have <html> tags to have a valid html document. Since your page doesn't seem to use a master page, there's probably no html tags being rendered. There's short and concise and then there's too short.
One thing I think you should add to your code is in relation to the jQuery ajax call.
You have not explicitly specified all of the settings. I use the following to set up my default values for all of my ajax calls, which can be overridden at any stage by an individual ajax call:
$.ajaxSetup({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
error: function (XMLHttpRequest, textStatus, errorThrown) {
utilities.ajaxErrorAlert(XMLHttpRequest, textStatus, errorThrown);
}
});
I have a page which displays the following content when it is loaded:
<div>
<img src="loading.gif" alt="Loading" /> Generating PDF Document, please wait.
<div>
At the same time, I want to run this method the code behind automatically:
private void GeneratePdf()
{
...
}
Lastly, I want to redirect the user to the File.pdf path once the GeneratePdf() method is completed.
How do I do this?
You need to use the Page_Load event as explained here. Additionally, this is a great explanation of the page life cycle. You should take a look at it. Here is some sample code from the above link:
<script runat="server">//This script runs on the server and dishes up some output for the page.
Sub Page_Load
lbl1.Text="The date and time is " & now()
End Sub
</script>
<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>//This generates the textbox that will be given a value from the above script.
</form>
</body>
</html>
Hope this helps you!
I suggest to use GeneratePDF with ajax, so you can use like
$.ajax({
url: '/pdf/fiele_to_generate_pdf.aspx',
type: 'POST',
contentType: "application/json; charset=utf-8",
data: $.toJSON(jsonObj),
processData: false,
dataType: 'html',
async: false,
success: function(html) {
window.location.href = '/pdf/example.pdf';
}});
so for loading you can use jquery, there is a good plugin named BlockUI you can use it before your ajax call.
You can use the asp:Timer control to indicate when your code-behind should be called
<asp:Timer runat="server" ID="timer1" Interval="3000" OnTick="timer1_Tick"></asp:Timer>
Then implement the Timer's Tick event. This will be called after 3 seconds (3000ms) as specified in the markup
protected void timer1_Tick(object sender, EventArgs e)
{
GeneratePdf();
Response.Redirect("~/somepath/generatedPDF.pdf");
}
I have a website where i want my visitors to invite their friends. I'm using the Requests Dialog for this. But now i want to add a dynamic text message to this invite.
The dynamic text is generated in an c# code behind file. But the Request Dialog is triggered with javascript.
Here is the Request Dialog part on my aspx page:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/nl_NL/all.js">
</script>
<script>
FB.init({
appId: '[MY-APP-ID]', cookie: true,
status: true, xfbml: true
});
function inviteFriends() {
FB.ui({ method: 'apprequests', message: '[THE DYNAMIC MESSAGE I WANT TO SEND]', data: 'tracking information for the user' });
}
</script>
Is it possible to call this javascript form codebehind code and pass the message parameter too?
Thanks in advance!
You can use <%= ... %> to expose your code to page (assuming you have message in requestsDialogMessage variable):
FB.ui({
method: 'apprequests',
message: '<%= requestsDialogMessage %>',
data: 'tracking information for the user'
});
Other way may be exposing the message to some JavaScript variable and using it within FB.ui call:
<script>
var exposedData = {
requestsDialogMessage: '<%= requestsDialogMessage %>'
};
</script>
And later in your JS code:
FB.ui({
method: 'apprequests',
message: exposedData.requestsDialogMessage,
data: 'tracking information for the user'
});