ASP.NET调用codebehind功能,无需回发? [英] Call ASP.NET codebehind function without postback?

查看:99
本文介绍了ASP.NET调用codebehind功能,无需回发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaScript和Ajax其所有数据的查询和前端工作网页,我试图在Excel导出功能来写。看到在JavaScript来做到这一点的唯一方法似乎是使用ActiveXControl只能在IE使用,我想包括这个在我的背后网页中的codeFile的一个子。

这似乎是个好主意,但每当我按一下按钮,页面回,和所有在那里出口走了数据。如果我包括的OnClientClick =返回false;我的ASP:按钮,按钮不回传,但也分不执行。有什么办法在这里实现我的目标,而不必使用注册客户端脚本块?任何帮助是极大的AP preciated。

谢谢!

- 编辑 -

我在$就得到一个404的方法调用。下面是相关code。

  //服务器端
部分类报告
    继承System.Web.UI.Page    &所述; System.Web.Services.WebMethod()>
    公用Sub toXL()
        的Response.Write(< BR>< BR>此处< BR>< BR>中)
    结束小组
末级//客户端
    功能toExcel(){
        $阿贾克斯({
            键入:POST,
            网址:/report.aspx/toXL
            数据:{},
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSON
            成功:函数(MSG){
                如果(msg.error){
                    警报(msg.error);
                }
                返回false;
            }
        });
    }//按钮
< ASP:按钮的ID =用作MyButton=服务器的AutoPostBack =false的类型=按钮的OnClientClick =toExcel();返回false; />


解决方案

您可以desribe方法,一个WebMethod并把它从jQuery的AJAX web服务。和AJAX调用之后运行返回false。

 < ASP:按钮的OnClientClick =DoAction(); ID =身份识别码=服务器/>
     公共部分类_Default:页
        {
          [的WebMethod]
          公共静态字符串的MyMethod()
          {
    .......
          }
        }功能DoAction(){
    $阿贾克斯({
      键入:POST,
      网址:PageName.aspx /方法名
      数据:{},
      的contentType:应用/ JSON的;字符集= UTF-8,
      数据类型:JSON
      成功:函数(MSG){
        如果(msg.error){
    //显示错误
        }
    返回false;
      }
    });
}

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 .

<asp:button OnClientClick="DoAction();" id="myid" runat="server"/>


     public partial class _Default : Page 
        {
          [WebMethod]
          public static string MyMethod()
          {
    .......
          }
        }

function DoAction(){
    $.ajax({
      type: "POST",
      url: "PageName.aspx/MethodName",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        if(msg.error){
    //show  error 
        }
    return false;
      }
    });
}

这篇关于ASP.NET调用codebehind功能,无需回发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆