通过客户端函数在服务器端调用非静态方法

时间:2022-02-10 16:08:11

I can get an object from the server side by using static receive callbackresult methods from server side.

我可以通过服务器端的静态接收callbackresult方法从服务器端获取一个对象。

But I want to run a non-static method in my page which populates an ajax accordion by calling a client side function.

但我想在我的页面中运行一个非静态方法,通过调用客户端函数来填充ajax手风琴。

The object I am calling from server side is a complex object which I can't use in client side if I get it by callbackresults.

我从服务器端调用的对象是一个复杂的对象,如果我通过callbackresults得到它,我不能在客户端使用它。

Is there any other solution that I can run a non static method in an aspx file by a client side control ?

有没有其他解决方案,我可以通过客户端控件在aspx文件中运行非静态方法?

Codes I am using so far ...

我到目前为止使用的代码......

       function ReceiveServerData(arg, context) {
    //Message.innerText = "Date from server: " + arg;
}

#region ICallbackEventHandler Members

public void RaiseCallbackEvent(String eventArgument)
{
    // Processes a callback event on the server using the event
    // argument from the client.
    Insert(); // this is running, but doesnt work !
    //printAlternativesFromAirport(eventArgument);
}

public string GetCallbackResult()
{
    // Returns the results of a callback event to the client.
return null;
}

#endregion


protected void Page_Load(object sender, EventArgs e)
{
    ClientScriptManager cm = Page.ClientScript;
    String cbReference = cm.GetCallbackEventReference(this, "arg",
        "ReceiveServerData", "");
    String callbackScript = "function CallServer(arg, context) {" +
        cbReference + "; }";
    cm.RegisterClientScriptBlock(this.GetType(),
        "CallServer", callbackScript, true);
 }

1 个解决方案

#1


0  

yes, you need to create a handler that will create the entire context needed for the page, which will run a full page life cycle ect, and is more recommended when you want to retrieve something like a user control or something big.

是的,您需要创建一个处理程序,该处理程序将创建页面所需的整个上下文,这将运行整个页面生命周期等,并且当您想要检索诸如用户控件或大型内容之类的内容时,更建议使用该处理程序。

    public void ProcessRequest(HttpContext context)
    {
        context.Response.Write(RenderView("~/_controltemplates/15/myDir/Templates/myUC.ascx"));
    }

    public static string RenderView(string path)
    {
        try
        {
            Page pageHolder = new Page();
            UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
            pageHolder.Controls.Add(viewControl);
            StringWriter result = new StringWriter();
            Log.Application.Debug(LOGPREFIX + "RenderView before Execute");
            HttpContext.Current.Server.Execute(pageHolder, result, true);
            return result.ToString();
        }
        catch (Exception ex)
        {
            Log.Application.ErrorException(LOGPREFIX, ex);
            throw;
        }
    }

but i think that it is not what you need, instead i would advice you to make an entity (class) to handle that insert function that will not need any httpContext and run it from a simple handler.

但我认为这不是你需要的,而是我会建议你创建一个实体(类)来处理不需要任何httpContext并从一个简单的处理程序运行它的插入函数。

another solution you might need, since maybe you do need all the postback info but do not want to make a full postback is to use AjaxPanel or even clear the Response and send "OK" instead.

您可能需要的另一种解决方案,因为您可能确实需要所有回发信息但不想进行完整回发是使用AjaxPanel甚至清除响应并发送“确定”而不是。

#1


0  

yes, you need to create a handler that will create the entire context needed for the page, which will run a full page life cycle ect, and is more recommended when you want to retrieve something like a user control or something big.

是的,您需要创建一个处理程序,该处理程序将创建页面所需的整个上下文,这将运行整个页面生命周期等,并且当您想要检索诸如用户控件或大型内容之类的内容时,更建议使用该处理程序。

    public void ProcessRequest(HttpContext context)
    {
        context.Response.Write(RenderView("~/_controltemplates/15/myDir/Templates/myUC.ascx"));
    }

    public static string RenderView(string path)
    {
        try
        {
            Page pageHolder = new Page();
            UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
            pageHolder.Controls.Add(viewControl);
            StringWriter result = new StringWriter();
            Log.Application.Debug(LOGPREFIX + "RenderView before Execute");
            HttpContext.Current.Server.Execute(pageHolder, result, true);
            return result.ToString();
        }
        catch (Exception ex)
        {
            Log.Application.ErrorException(LOGPREFIX, ex);
            throw;
        }
    }

but i think that it is not what you need, instead i would advice you to make an entity (class) to handle that insert function that will not need any httpContext and run it from a simple handler.

但我认为这不是你需要的,而是我会建议你创建一个实体(类)来处理不需要任何httpContext并从一个简单的处理程序运行它的插入函数。

another solution you might need, since maybe you do need all the postback info but do not want to make a full postback is to use AjaxPanel or even clear the Response and send "OK" instead.

您可能需要的另一种解决方案,因为您可能确实需要所有回发信息但不想进行完整回发是使用AjaxPanel甚至清除响应并发送“确定”而不是。