I’m working on a webpage that uses javascript and ajax for all of its data queries and front end work, and I’m trying to write in excel export functionality. Seeing as the only way to do this in javascript seems to be using ActiveXControl only available in IE, I want to include this as a Sub in a codefile behind my web page.
This seems to be a good idea, but whenever I click the button, the page posts back, and all of the data that was there to be exported is gone. If I include OnClientClick="return false;" on my asp:Button, the button doesn’t post back, but the Sub also doesn’t execute. Is there any way to accomplish my goal here without having to use register client script block? Any help is greatly appreciated.
Thanks!
–edit–
I am getting a 404 on $.ajax for the method call. Here is the relevant code.
//server side
Partial Class Report
Inherits System.Web.UI.Page
<System.Web.Services.WebMethod()>
Public Sub toXL()
Response.Write("<br><br>Here<br><br>")
End Sub
End Class
//client side
function toExcel() {
$.ajax({
type: "POST",
url: "/report.aspx/toXL",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.error) {
alert(msg.error);
}
return false;
}
});
}
//button
<asp:Button ID="MyButton" runat="server" AutoPostBack="false" type="button" OnClientClick="toExcel(); return false;" />
You can desribe method as a WebMethod and call it as a webservice from jquery ajax . And after ajax call is run return false .