简体   繁体   English

如何从aspx调用非静态方法

[英]How to call a non-static method from aspx

I've a Method in the code behind of my aspx page, I need to call Two methods from Javascript, the problem that I'm Having is that I was trying to do it with a Json request and a WebMethod, but this method has to be static and the page components and other methods cannot be accessed from this method. 我的aspx页面后面的代码中有一个方法,我需要从Javascript调用两个方法,我遇到的问题是我试图使用Json请求和WebMethod来执行此操作,但是该方法具有为静态,并且无法从此方法访问页面组件和其他方法。

I was trying something Like: 我正在尝试类似的东西:

javascript Function javascript功能

function Func(Value) {
        var conf=confirm('Sure? '+valor)
        if (conf==true)
        {
        BlockAction();
        }
    }
       function BlockAction() {     
                 $.ajax({
                 type: "POST",
                 url: 'frmVentaTelefonica.aspx/BlockAction',
                 data: "",
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 success: function (msg) {
                     $("#divResult").html("success");
                 },
                 error: function (e) {
                     $("#divResult").html("Something Wrong.");
                 }
             })}; 

Code-behind code: 后台代码:

   [WebMethod]                               
    public static  void BlockAcction()
    {
        try
        {
            frmVentaTelefonica venta = new frmVentaTelefonica();    
            venta.ConsultarVentaTelefonica();
            venta.ImprimirTiquetes();
        }
        catch (Exception e)
        {                
            throw;
        }
    }

I want to call those two methods when the confirm is true. 当确认为真时,我想调用这两个方法。

Update: In need to accesses to two methods like this: 更新:需要访问两个这样的方法:

 public void ConsultarVentaTelefonica()
        {
            DatosImpresion = new List<Impresion>();
            IServicioVentas servicioVentas;
            servicioVentas = SATWebServiceLocator<IServicioVentas>.ObtenerServicio();
            string Tiquetes = string.Empty;
            foreach (GridDataItem dataItem in gridInfoVentaTelefonica.MasterTableView.Items)
            {
                if ((dataItem.FindControl("CheckBox1") as CheckBox).Checked)
                {
                    Tiquetes = Tiquetes + (dataItem["Tiquete"]).Text + ",";
                }
            }
            Tiquetes = Tiquetes.TrimEnd(Tiquetes[Tiquetes.Length - 1]);
            Tiquetes = " " + Tiquetes + " ";
            DataSet dsResultado = servicioVentas.EntregaTelefonica(sessionR8A.Turno.IdTurno, Tiquetes);
            if (dsResultado.Tables.Count > 0 && dsResultado.Tables[0].Rows.Count > 0) 

Just run it when is true, those methods update in the database and print a ticket(first reading a grid checked items) 只要在true时运行它,这些方法就会在数据库中更新并打印票证(首先读取网格检查的项目)

If you are trying to update the UI controls, or read their values, then what you are describing is the UpdatePanel control. 如果您试图更新UI控件或读取它们的值,那么您所描述的就是UpdatePanel控件。 A page webmethod cannot update any control and refresh the UI (unless through JavaScript). 页面Web方法无法更新任何控件和刷新UI(除非通过JavaScript)。 If you want to update the state of the page async, UpdatePanel is what you are looking for. 如果要更新页面异步状态,则需要使用UpdatePanel。

If you are trying javascript just because you dont want to refresh the page, then go for Update Panel . 如果您只是因为不想刷新页面而尝试使用javascript,请转到“更新面板”。 The answer for your question is 'No' you cant access non-static methods like how you want to do. 您问题的答案是“不”,您无法访问非静态方法,例如您想做的事。

The reason it supports only static methods is that page instantiation is not done, if you want to use non static web methods then go for web service(.asmx). 它仅支持静态方法的原因是页面实例化没有完成,如果您要使用非静态Web方法,则可以使用Web服务(.asmx)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM