2014-05-08 2 views
-1

검사 된 레코드를 검색하는 함수를 구현하려고하면 함수는이를 ArrayList에 추가 한 다음 ArrayList를 ViewState에 저장합니다. 본질적으로, 테이블에서 선택된 행 (체크 박스를 통해)을 삭제하는 버튼이 있습니다. 그래서 Page_Load 이벤트 후, 내가 선택한 행을 삭제 버튼을 클릭하지만 난 nullreference 예외 얻을 : 더없이 사전if 문에서 NullReference 예외가 발생했습니다. 뭐가 잘못 되었나요?

+1

[디버거 사용] (http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) -'chkAll' 또는'chk'는'null'입니다. 'FindControl'에 대한 호출이 예상 한 것을 반환하지 않기 때문입니다. –

+0

디버거에서 실행하십시오. 변수는 null입니까? – BradleyDotNET

+1

'grdUnsubscribe'는 GridView이고'chkAll'은 그 행의 어디나 머리글/바닥 글에 있습니다. 행이 GridView가 아닌 ​​NamingContainer이기 때문에'GridViewRow.FindControl ("chkAll")'을 사용해야합니다. 헤더에 있다면'grdUnsubscribe.HeaderRow.FindControl ... '을 사용할 수 있습니다. –

답변

0

에서

protected void GetSelectedRecords() 
{ 
    ArrayList arr; 

    if (ViewState["SelectedRecords"] != null) 

     arr = (ArrayList)ViewState["SelectedRecords"]; 


    else 

     arr = new ArrayList(); 


    CheckBox chkAll = new CheckBox(); 

    chkAll = (CheckBox)grdUnsubscribe.FindControl("chkAll"); 




    for (int i = 0; i < grdUnsubscribe.Rows.Count; i++) 
    { 
     if (chkAll.Checked == true) 
     { 
      if (!arr.Contains(grdUnsubscribe.DataKeys[i].Value)) 
      { 
       arr.Add(grdUnsubscribe.DataKeys[i].Value); 
      } 
     } 

     else 
     { 
      CheckBox chk = (CheckBox)grdUnsubscribe.Rows[i].Cells[0].FindControl("chk"); 

      if (chk.Checked) 
      { 
       if (!arr.Contains(grdUnsubscribe.DataKeys[i].Value)) 
       { 
        arr.Add(grdUnsubscribe.DataKeys[i].Value); 
       } 
      } 

      else 
      { 
       if (arr.Contains(grdUnsubscribe.DataKeys[i].Value)) 
       { 
        arr.Remove(grdUnsubscribe.DataKeys[i].Value); 
       } 
      } 
     } 
    } 
    ViewState["SelectedRecords"] = arr; 
} 

감사 : 여기

An exception of type 'System.NullReferenceException' occurred 
    but was not handled in user code 
Additional information: Object reference not set to an instance of an object. 

을하는 기능입니다 예외의 세부 사항은 어떤 변수가 null인지 말할 수 없기 때문에 시작하기 좋은 곳은 예외를 throw하는 선을 보는 것입니다. 예외가이 라인에 발생된다고 가정하면, 예를 들어

:

if (ViewState["SelectedRecords"] != null) 

다음 문제의 ViewState가 널이다.

관련 문제