2012-06-19 1 views
3

사용자가 Gridview에서 편집 모드로 전환 할 때 RegisterClientScriptBlock을 사용하여 사용자에게 JS 경고를 보내지 만 어떤 이유로 인해 내 페이지가 오류가 발생하고 이유를 파악할 수 없습니다.RegisterClientScriptBlock에 도움이되지 않는 오류가 발생했습니다.

이것은 문제를 일으키는 방법입니다. 이 오류는 스크립트가 등록 된 마지막 줄에서 발생합니다. (나는이 주석 처리하면 페이지가! 잘 작동)

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 콘솔에서 throw되는 오류 또한이 오류가 $ ScriptResource에 작성 오류가 발생한 것을 말한다

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

입니다 .axd. 그러나 이것이 실제 문제가 무엇인지보고 할 때 발생하는 오류라고 생각합니다. 그래서 나는 완전히 혼란 스럽습니다.

도움을 주시면 대단히 감사하겠습니다. 감사.

+0

내 생각에 이런 식으로'typeof (Page)'입니다. 'this.GetType() '으로 변경해보십시오. – CAbbott

+0

GetType()으로 변경 한 후에도 동일한 문제가 발생합니다. : –

+0

그게 짐작 이었어. :)'페이지'및'ClientScript'가 객체인지 확인하기 위해 디버깅을 시도 했습니까? 그렇지 않으면 나는뿐만 아니라 난처한 상황이다. – CAbbott

답변

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); 
1
이는 내 유일한 부분 갱신을 할 때 전화가 뒤에 코드에서 스크립트를 등록 함께 할 것으로 보인다

업데이트 패널. 스크립트 관리자에서 EnablePartialRendering="false"으로 설정하면 정상적으로 작동합니다. 부분 렌더링을 허용하는 것처럼 오류가 발생합니다.

+0

다음 대답으로 수락 – Damith

+0

:) 충분히 중요하지 않아서 2 일을 기다려야합니다 ... –

+0

RegisterStartupScript에 4 번째 매개 변수를 추가하고 (True로 설정) 지저분한 '