3

ユーザーがグリッドビューで編集モードになったときに、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 でこのエラーが発生したと書かれていますが、これは本当の問題が何であるかを報告する際に発生するエラーだと思うので、完全に困惑しています。

どんな助けでも大歓迎です。ありがとう。

4

2 に答える 2

3

削除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);  
于 2012-06-19T14:26:59.340 に答える
1

更新パネル内で部分的な更新のみを行う場合に、コード ビハインドからスクリプトを登録する呼び出しに関係しているようです。EnablePartialRendering="false"スクリプトマネージャーで設定すると、すべて正常に動作します。部分的なレンダリングを許可したかのように、エラーが発生します。

于 2012-06-19T14:45:42.373 に答える