C#处理回调结果

C# handling callback result

本文关键字:结果 回调 处理      更新时间:2023-09-26

e我在以下情况下有点挣扎。我已经在我的网页中实现了ICallBackEventHandler,除了javascript函数无法读取从后端返回的值之外,一切都运行顺利。在控制台中,post会显示正确的返回值,但函数的参数始终为空。

public partial class Intro : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
private string eventType = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        //ICallBack event handler
        ClientScriptManager cm = Page.ClientScript;
        string cbReference = cm.GetCallbackEventReference(this, "arg", "HandleResult", "");
        string cbScript = "function RaiseEvent(arg){" + cbReference + ";}";
        cm.RegisterClientScriptBlock(this.GetType(), "RaiseEvent", cbScript, true);
        //End of ICallBack event handler
    }

 }
public void RaiseCallbackEvent(string eventArgument)
    {
        eventType = eventArgument;
    }
    public string GetCallbackResult()
    {
        return "simple";
    }

然后在前端,我有以下场景:我用这个触发按钮点击事件:RaiseEvent("start"),我用这个函数处理结果:

function HandleResult(arg) {
            alert(arg); // HERE THE ARGUMENT IS ALWAYS NULL OR EMPTY !!!
        }

请帮助我找出为什么它运行不正常,以及为什么我无法访问返回参数的值。提前谢谢。

这通常是由于未使用页面造成的。IsPostBack

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
    //ICallBack event handler
    ClientScriptManager cm = Page.ClientScript;
    string cbReference = cm.GetCallbackEventReference(this, "arg", "HandleResult", "");
    string cbScript = "function RaiseEvent(arg){" + cbReference + ";}";
    cm.RegisterClientScriptBlock(this.GetType(), "RaiseEvent", cbScript, true);
    //End of ICallBack event handler
    }
}