2011-05-10 2 views
0

다음 코드를 사용하고 있습니다.FindControl을 사용할 때 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.

의 .ascx 파일 : 뒤에

<div class="DemoArea"> 
    <asp:Button ID="btnCaseComplete" runat="server" Text="Case Complete" CssClass="btn_contentlist" 
     onclick="btnCaseComplete_Click" OnClientClick="scroll(0,0);$.loading({mask: true, effect: 'ellipsis update'});"/> 
     <ComponentArt:Dialog ID="caDropDownDialog" runat="server" Modal="true" Alignment="MiddleCentre" AllowDrag="true" AllowResize="false" AnimationDuration="1000" 
          CloseTransition="Fade" RenderOverWindowedObjects="true" ShowTransition="Fade" AnimationType="Outline" CssClass="ModalMask"> 
          <Header><p class="header">Case Complete</p></Header> 
          <Content> 
           <asp:Panel ID="panSelectArea" runat="server" CssClass="modalMaskContent"> 

            <p><span class="red">Please Note:</span>Once you click 
             <span class="bold">Ok</span>, your 
              case will be Submitted to ACR and you will not be able to edit the Case again. 

              <span class="style2">To continue editing the case, click </span> 
              <span class="bold">Cancel</span>. You will be taken back 
              to the Case Wizard and your case will not be submitted to ACR.</p> 
            </asp:Panel> 
          </Content> 
          <Footer> 
           <center class="modalMaskFooter"> 
            <asp:Button ID="btnOK" runat="server" CausesValidation="false" 
             CssClass="btn_contentlist" OnClientClick="caDropDownDialog.IsShowing=false" Text="OK" /> 
            <asp:Button ID="btnCancel" runat="server" CausesValidation="false" 
             CssClass="btn_contentlist" OnClientClick="caDropDownDialog.Close();" Text="Cancel" /> 
           </center> 
          </Footer> 
         </ComponentArt:Dialog> 
    </div> 

코드 :

CaseContentList obj = new CaseContentList(); // creating the object of case content list control to this page 

     LinkButton lbtn = (LinkButton)((DataList)obj.FindControl("dlstContentList")).FindControl("lbtnDisplay"); 
     if (lbtn.Text == "Final Page") 
     { 
      caDropDownDialog.IsShowing = true; 
     } 
     else 
      Page.ClientScript.RegisterStartupScript(this.GetType(), "Window", "alert('Add Final Page First.');", true); 

그러나 그것은 오류를주고 '개체 참조가 개체의 인스턴스로 설정되지 않았습니다'.

+1

이것은 당신이 중 하나를 잘못 캐스팅하거나 올바른 개체를 참조하지 않는 것을 의미합니다, 귀하의 영문 코드 –

+1

를 붙여주십시오 나는 당신'의 FindControl 한 생각 '명령문이 실패합니다. 설립 된 컨트롤을 사용하기 전에 null 검사를 추가하십시오. –

+0

어떤 코드에서이 코드를 호출합니까? – TcKs

답변

1
CaseContentList cl = (CaseContentList)this.Parent.TemplateControl.FindControl("ContentList"); 
     if(cl.IsFinalPage) 
      caDropDownDialog.IsShowing = true; 
     else 
      Page.ClientScript.RegisterStartupScript(this.GetType(), "Window", "alert('Please add Final Page to complete your case');", true); 
0

가 DataList로하고 당신이하는 LinkButton을 찾고 있다면, 그때 당신은 그을 ListItem에 컨트롤을 찾을 수 있습니다, 당신이 원하는 하나에 대한 DataList에있는 각을 ListItem을 통해 루프 필요가 기대

으로 당신이 할 수있는 DataList 내 ListItem을 통해 루프

foreach (DataListItem dataListItem in obj.Items) { 

if (dataListItem .ItemType == ListItemType.AlternatingItem | dl.ItemType == ListItemType.Item) { 
     // 
     // find the control here. 
} 
} 
+0

유 PLZ 말해 루프를 통해 찾을 수있는 루프를 – Rocky

+0

편집 된 답변을 확인하십시오. 귀하의 DataList를 통해 루프 DataListItems –

+0

obj.Items는 정의를 포함하지 않는 오류를 제공하고 있습니다 – Rocky

1

"FindControl()"메소드 중 하나의 결과로 인해 발생합니다. 결과는 NULL이며 제어 인스턴스가 아닙니다. 당신은 널 (null)를 확인해야한다

가 :

bool found = false; 
var dlstContentList = obj.FindControl("dlstContentList"); 
if (null != dlstConentList) { 
    var lbtnDisplay = dlstContentList.FindControl("lbtnDisplay"); 
    found = (null != lbtnDisplay); 
} 

if (found) { 
    // ... do something 
} 
else { 
    // ... do something else 
} 
+0

내 코드를 한번 확인한 다음 – Rocky

+0

알려주십시오. 상황은 같습니다. 컨트롤이 생성되기 전에 코드를 적절히 실행해야합니다. 컨트롤은 페이지의 "Init"단계에서 생성됩니다. – TcKs

관련 문제