繁体   English   中英

RegisterClientScriptBlock上的无用错误

[英]Unhelpful error on RegisterClientScriptBlock

当用户在gridview上进入编辑模式时,我使用RegisterClientScriptBlock向用户发送JS警报,但由于某种原因导致我的页面出错,我无法弄清楚为什么......

这是导致问题的方法。 该错误发生在注册脚本的最后一行。 (如果我评论这个页面工作正常!)

    protected void EditRecord(object sender, GridViewEditEventArgs e)
    {

        gvStockItems.EditIndex = e.NewEditIndex;
        // Gather current Search info
        string strPartNo = Session["currentSearchTerm"].ToString();
        BindData();
        gvStockItems.SelectedIndex = gvStockItems.EditIndex;
        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "thisIsTest", "<script language=\"text/javascript\">alert(\"oops\");</script>");
    }

JS控制台中引发的错误是

Uncaught Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.

它还说在ScriptResource.axd中的Error $ Create中发生了这个错误,但我认为这是在报告真正的问题时发生的错误,所以我完全被难倒了。

任何帮助是极大的赞赏。 谢谢。

删除Page.ClientScript.RegisterClientScriptBlock并尝试使用RegisterStartupScript

// Define the name and type of the client scripts on the page.
String csname1 = "thisIsTest";
Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
    StringBuilder cstext1 = new StringBuilder();
    cstext1.Append("<script type=text/javascript> alert('oops!') </");
    cstext1.Append("script>");

    cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
}

或者如果你有ScriptManager那么

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "thisIsTest", "alert('oops!');", true);  

它似乎与在更新面板中仅进行部分更新时来自代码的调用注册脚本有关。 如果我在脚本管理器中设置EnablePartialRendering="false" ,它一切正常。 如果我允许部分渲染,则会发生错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM