【问题标题】:C# handling callback resultC#处理回调结果
【发布时间】:2014-08-05 20:42:05
【问题描述】:

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 !!!
        }

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

【问题讨论】:

    标签: c# javascript asp.net .net icallbackeventhandler


    【解决方案1】:

    这通常是由于没有使用 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
        }
    }
    

    【讨论】:

    • 不,这不是问题。
    猜你喜欢
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    相关资源
    最近更新 更多