c# - Defining Label.Text leads to a NullReferenceException -


I'm trying to set a label to show some text when an error occurs.

  if (userEmail! = Null) {i // if exists in the same email pnlError.Visible = Visible; LblError.Text = "Error: The email you entered is already assigned to an account.";   

}

When I am creating, I do not get any error, which would suggest me to find it in the ASPX code.

P> Here's the markup:

  & lt; Asp: panel id = "pnlError" runat = "server" visible = "incorrect" EnableViewState = "false" & gt; & Lt; Label id = "lblError" & gt; & Lt; / Labels & gt; & Lt; / ASP: Cell & gt;   

As you can see it is wrapped in a panel. I can fix the visibility of the panel only in the same way as Label.Text

and here it is defined in aspx.designer.cs:

  protected global :: system Web. UI. WebControl Panell PNLER; Protected global :: system. Web.UI.WebControls.Label lblError;   

It is notable that whenever I change any other webcontroll element into markup, such as buttons or panels, aspx.design.cs reborn, but it failed to include lblError Remains label I tried to delete and then manually tried to rebuild the design manually.

As the label is within one panel, you need to find it:

  if (userEmail! = Null) {// If the same email exists, then pnlError.Visible = Visible; Var lblError = ((label) (pnlError.FindControl ("lblError"))); If (lblError! = Null) {lblError.Text = "Error: The email you entered ..."; }}   

Edit:

You can use better asp control

  & lt; Asp: Label ID = "LblError" runat = "server" & gt; & Lt; / ASP: Labels & gt;   

Then you do not need to find it

  pnlError.Visible = Visible; LblError.Text = "Error: The email you entered ...";    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -