Search code examples
c#asp.netlabelupdatepanel

Only first Label Not Showing on ASP.NET C# page


I have multiple asp.net labels inside 2 update panels. I am handling them all the same but the first one does not show up. For simplicity sake I have everything happening on one button when in reality the code happens in different places but I stepped through it in Debug and confirmed this is the order. I am thinking it has to do with the 2 update panels, that a change it UpdatePanel3 doesn't update UpdatePanel2, but not sure.

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="LabelError" runat="server" 
            style="float: left" ForeColor="Red"></asp:Label>

....

<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
     <ContentTemplate>
        <asp:Label ID="LabelError2" runat="server" Text="Password" 
            style="float: left" Visible="False"></asp:Label>

        <asp:Button ID="ButtonShow" runat="server" Text="Submit" 
                  onclick="ButtonShow_Click" />
                  </ContentTemplate>

In the code I do the following.

protected void ButtonShow_Click(object sender, EventArgs e)
{
  LabelError.Visible=true;
  LabelError2.Visible=true;

  LabelError.Style.Remove("display");
  LabelError.ForeColor = System.Drawing.Color.Red;

  LabelError2.Style.Remove("display");


  if (LabelError.Visible)
  {
      LabelError.Text="This is Label Error";
      LabelError.Style.Add("display", "block");
  }
  else
  {
      LabelError.Style.Add("display", "none");
  }

  if (LabelError2.Visible)
  {
       LabelError2.Text="This is Label Error2";
       LabelError2.Style.Add("display", "block");
  }
  else
  {
      LabelError2.Style.Add("display", "none");
  }
}

LabelError2 pops on the screen. LabelError does not. Using IE, hitting F12 and looking at the DOM LabelError isn't even there. What am I doing wrong? Why won't LabelError show up?


Solution

  • Taking this theory into account I tried adding UpdatePanel2.Update(); at the end of ButtonShow_Click() This worked. It showed up.