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");
}
Related
I have jquery code on cc.js that I need to execute when an asp.net button is clicked.
The code on cc.js has a variable called settings and then submits these settings to an API (POST action) with the following code:
'Transaction Submit'
$.ajax(settings).done(function (response) {
console.log(response);
});
Basically I need this button <asp:Button ID="BtnSubmit" runat="server" Text="Submit" /> to execute that jquery code. How can I do so?
You are looking for the OnClientClick property
<asp:Button ID="BtnSubmit" runat="server" OnClientClick="JSFunctionName(); return false;" Text="Submit" />
You will need to add the additional return false statement to prevent a post back from occurring if you button element is located inside a form.
This is assuming you want to decouple your front end html from your js code by using an inline call. If that is not the case then you mod your cc.js to autobind on the element as others have mentioned. One thing they left out is that in that process is that IF you button is inside a form then you need to prevent the auto-post back like so:
$(document).ready(function() {
$("#BtnSubmit").click(function(){
JSFunctionName();
e.preventDefault();
});
});
If the ID of the button is BtnSubmit, then you should be able to add a event handler like this.
$(document).ready(function() {
$("#BtnSubmit").click(function(){
alert("button clicked - do stuff here.");
});
});
change the alert("button clicked - do stuff here."); to your ajax request code.
You can write the following code to create an event on Button click to call the jquery method..
Basically, you take the ID by $("#btnSubmit") and on its click event write your Ajax function.
$("#BtnSubmit).on('click',function() {
// your ajax code here
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<asp:Button ID="BtnSubmit" runat="server" Text="Submit" />
Hope this helps.
Just Add a click event with your button id using jquery..
$("#BtnSubmit").on('click',function(){
console.log('success...');
//ajax call here...
});
You can use Ajax within JS functions. I'm not sure what version of .NET you're using, but I'll answer with what I know for MVC 5.
Here's how I would do it:
HTML
<button id="SubmitBtn">Submit</button>
JS(JQuery)
$('#SubmitBtn').on('click', function () {
//Transaction Submit
$.ajax({
type: "POST",
url: '/ProjectName/Controller/Method',
data: { methodParameter1: data1, methodParameter2: data2 },
success: function (data) {
//do whatever
}
error: function (response) {
//do whatever
}
});
});
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 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
I have two textboxes, ID and Name. Based on the combination of these two, my address dropdown should get populated.
I have written an sp to bind items to my dropdown. But the text_changed event of Name textbox causes AutoPostback. I want my dropdownlist to bind automatically when I leave the textbox without causing any refreshing of the page. On page load all 3 should be blank.
Please let me know how to accomplish this using javascript and jquery.
Please excuse for any typing mistakes.
You could either user jQuery Ajax or UpdatePanel
Using JQuery Ajax...
$.ajax({
data: data,
url: "ur url",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (result) {
//ur code to bind data to dropdown
}
error: function OnError(result) {
//ur code
}
});
Using Update Panel you can call server side method to bind data to dropdown
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtBox1" OnTextChanged="txtBox1_TextChanged" AutoPostBack="true" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
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);
}
});