当用户在 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);
关于c# - RegisterClientScriptBlock 上的无用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11102665/