开发者

Why this works out of UpdatePanel but not inside UpdatePanel?

开发者 https://www.devze.com 2022-12-24 04:53 出处:网络
I have copy and paste this Alert Class: http://madskristensen.net/post/JavaScript开发者_开发知识库-AlertShow%28e2809dmessagee2809d%29-from-ASPNET-code-behind.aspx

I have copy and paste this Alert Class: http://madskristensen.net/post/JavaScript开发者_开发知识库-AlertShow%28e2809dmessagee2809d%29-from-ASPNET-code-behind.aspx

It works except with a button inside an update panel but it shows no error. The asp code inside is executed but nothing shows up on client side.


Registering the Javascript with ScriptManager.RegisterClientScriptBlock rather than page.ClientScript.RegisterClientScriptBlock will take care of registering scripts during partial page updates. Here's the modified code:

public static void Show(string message)
{
    // Cleans the message to allow single quotation marks
    string cleanMessage = message.Replace("'", "\\'");
    string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";

    // Gets the executing web page
    Page page = HttpContext.Current.CurrentHandler as Page;

    if (page != null)
    {
        ScriptManager.RegisterClientScriptBlock(page, typeof(Alert), "alert", script, false);
    }

}


I suggest not to use updatePanels at all, implement scriptable webservice and access it via proxy kindly generated for you by asp.net it will save you tons of nerves as well as time, because updatepanel is really really buggy

0

精彩评论

暂无评论...
验证码 换一张
取 消