2014-10-10 2 views
0

오류가 발생하면 텍스트를 표시하는 레이블을 설정하려고합니다. 이 오류가 발생하면 레이블에서 NullReferenceException이 발생합니다. 그것에서 찾을 수 있다는 것을 나에게 제안Label.Text를 정의하면 NullReferenceException이 발생합니다.

if (userEmail != null) 
      { 
       //If the same email exists 
       pnlError.Visible = Visible; 
       lblError.Text = "Error: The email you have entered is already assigned to an account."; 

} 내가 빌드 할 때

, 나는 오류를 얻을 : 여기

뒤에 코드에서 label.Text 코드 ASPX 코드.

는 여기가 마크 업에 있습니다

<asp:Panel ID="pnlError" runat="server" Visible="false" EnableViewState="false"> 
     <label id="lblError"></label> 
     </asp:Panel> 

당신은이 패널에 싸여 볼 수 있듯이.

protected global::System.Web.UI.WebControls.Panel pnlError; 
    protected global::System.Web.UI.WebControls.Label lblError; 

그것은 언급 할 때마다 것을 가치가있다 : 나는 그것이 aspx.designer.cs에 정의되어

그리고 여기 Label.Text와 같은 기능에 잘 패널의 가시성을 변경할 수 있습니다 마크 업에서 단추 나 패널과 같은 다른 WebControl 요소를 변경하면 aspx.design.cs가 다시 생성되지만 lblError 레이블은 포함되지 않습니다. 삭제를 시도한 다음 디자인을 수동으로 다시 생성 해 보았습니다. 레이블이 패널의 내부에 있기 때문에

+0

'runat = "server"'가 없습니다. –

+0

레이블 태그에 runat = "server"를 추가하면 약 6 개의 오류가 발생합니다. – Frayt

+0

나는 간단한 웹 애플리케이션을 만들었고, 당신이 가진 콘텐츠와'runat = "서버"를 추가했다. aspx.designer.cs 파일의'HtmlGenericControl'이기 때문에 코드를'lblError.InnerText'로 변경해야했습니다. 아마도 '

답변

0

당신은 그것을 찾을 필요가 :

if (userEmail != null) 
{ 
    //If the same email exists 
    pnlError.Visible = Visible; 
    var lblError= ((Label)(pnlError.FindControl("lblError"))); 
    if(lblError != null) 
    { 
     lblError.Text = "Error: The email you have entered......"; 
    } 
} 

편집 :

당신을보다 효율적으로 사용하는 ASP를 제어

<asp:Label ID="lblError" runat="server" ></asp:Label> 

다음에 필요 없어요 찾으면

pnlError.Visible = Visible; 
lblError.Text = "Error: The email you have entered......"; 
+0

NullReferenceError 예외를 해결하지만 레이블의 텍스트를 업데이트하지 않습니다. 라벨 텍스트는 마크 업에 포함 된 것과 동일합니다. – Frayt

+0

@Frayt 내 편집보기 – meda

+0

고마워, 내가 그걸 빨리 깨닫지 못했다고 믿을 수 없어! – Frayt

관련 문제