0

e 我在以下情况下有点挣扎。我已经在我的网页中实现了 ICallBackEventHandler,一切运行顺利,除了从后端返回的值对于 javascript 函数不可读。在控制台中,帖子显示了正确的返回值,但函数的参数始终为空。

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";
    }

然后在前端我有以下场景:我使用 this 触发按钮单击事件:RaiseEvent("start")并且我正在使用 this 函数处理结果:

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

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

4

1 回答 1

0

这通常是由于没有使用 Page.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
    }
}
于 2014-08-05T21:16:50.420 回答