Hide and show controls in ASP.Net C #

1

I am working on ASP: NET C #. I have an imageButton hidden in my MasterPage, for this I used a class defined in a style sheet. In the design mode I see that the control is hidden but when loading the page, the control appears visible.
What I want to do is that if the user is not logged in, do not see the button and in case you are logged in you can see it.
I already tried with:

  

ImageButton.visible = false; // but it does not work

<asp:UpdatePanel ID="updMiImageButton" runat="server">
     <ContentTemplate>
          <div class="cssMiImageButton" id="divMiImageButton">
              <asp:ImageButton ID="MiImageButton" runat="server" ImageAlign="Right" CssClass="cssMiImageButton" Height="50px" ImageUrl="~/Imagenes/miImagen_isologo.gif" Width="50px" OnClick="MiImageButton_Click" BorderStyle="None"/>
          </div>
     </ContentTemplate>
</asp:UpdatePanel>

I leave the aspx and aspx.cs code.
What I want is that when the user is logged in, see the button, for this I tried to set the css property of the button to visible from the Page_Load but it did not work either:

 protected void Page_Load(object sender, EventArgs e)
    {
        MiImageButton.Style["Visibility"] = "hidden";
        if (!Page.IsPostBack)
        {
            try
            {
                if (this.usuarioLogin != null)
                {
                    this.ArmarMenu();
                    MiImageButton.Style["Visibility"] = "visible";
                }
            }
            catch (Exception ex)
            {
                log.Error("Site.Master:Page_Load", ex);
                this.lblMensaje.Text = msgErrorGenerico;
            }
        }

    }

Update
I corrected the code with the help of @Thiago Loureiro. Now the button can not be seen when the page is started but the button is not visible to the user.
I leave the corrected code, add "UpdateMode" to the UpdatePanel and the updMiImageButton.Update () ;.
I leave the corrected codes

Aspx.cs:

protected void Page_Load(object sender, EventArgs e)
    {
        MiImageButton.Style["Visibility"] = "hidden";
        updMiImageButton.Update();
        if (!Page.IsPostBack)
        {
            try
            {
                if (this.usuarioLogin != null)
                {
                    this.ArmarMenu();
                    MiImageButton.Style["Visibility"] = "visible";
                    updMiImageButton.Update();
                }
            }
            catch (Exception ex)
            {
                log.Error("Site.Master:Page_Load", ex);
                this.lblMensaje.Text = msgErrorGenerico;
            }
        }

    }    

Aspx:

    <asp:UpdatePanel ID="updMiImageButton" UpdateMode="Conditional" runat="server">
     <ContentTemplate>
          <div class="cssMiImageButton" id="divMiImageButton">
              <asp:ImageButton ID="MiImageButton" runat="server" ImageAlign="Right" CssClass="cssMiImageButton" Height="50px" ImageUrl="~/Imagenes/miImagen_isologo.gif" Width="50px" OnClick="MiImageButton_Click" BorderStyle="None"/>
          </div>
     </ContentTemplate>
</asp:UpdatePanel>
    
asked by Pablo Matias 03.05.2018 в 17:54
source

2 answers

1

I solved it by removing the updatePanel and the contentTemplate, leaving the button only inside the div (for a theme of style and that fits the page well). Now it works as I need it, I leave the final code.

protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
        try
        {
            if (this.usuarioLogin != null)
            {
                this.ArmarMenu();
                MiImageButton.Style["Visibility"] = "visible";

            }
            else
            {
                MiImageButton.Style["Visibility"] = "hidden";

            }
        }
        catch (Exception ex)
        {
            log.Error("Site.Master:Page_Load", ex);
            this.lblMensaje.Text = msgErrorGenerico;
        }
    }

}  

ASPX:

<div class="cssMiImageButton" id="divMiImageButton">
          <asp:ImageButton ID="MiImageButton" runat="server" ImageAlign="Right" CssClass="cssMiImageButton" Height="50px" ImageUrl="~/Imagenes/miImagen_isologo.gif" Width="50px" OnClick="MiImageButton_Click" BorderStyle="None"/>
</div>
    
answered by 03.05.2018 / 20:49
source
1

After any command you have to fire the updatepanel and perform an update. For example

updMiImageButton.Update();

Also check the UpdatePanel options like the Triggers, you can place an event of a button as a trigger for that updatePanel

    
answered by 03.05.2018 в 18:54