How to get div through another class

1

I have .aspx where I put a div alert. I have a Message class to handle the system messages. To treat these messages when instantiating the class, I pass as parameter the page, in the step method as parameter the message and message type. But Page.findcontrol("alert") is returning me null and I can not understand why.

.aspx:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="row">
            <div id="alert">


            </div>
        </div>
</asp:Content>

Code-behind:

 Message mensagens = new Message(this);
  mensagens.ShowMensagem("mensagem", "alerta");

Message.cs:

private Page pagina;
public Message(Page pagina) {
    this.pagina = pagina;
}

public void ShowMensagem(string mensagem, string tipo){
    System.Web.UI.HtmlControls.HtmlGenericControl div = (System.Web.UI.HtmlControls.HtmlGenericControl)pagina.FindControl("ContentPlaceHolder1_alert");
}
    
asked by anonymous 03.10.2014 / 14:05

1 answer

1

For an HTML tag to be visible in the code behind you need to put the runat="server" attribute, otherwise it will only be visible on the front end.

You can mix HTML tags with ASP.NET tags. The difference is that HTML tags have the option of not needing runat="server" to work.

And that's exactly the function of this attribute, being able to tell ASP.NET what it should parsing.

With this you can achieve consistency and extensibility.

There is a post from Mike Schinkel explaining runat="server" in English here: link

    
08.10.2014 / 03:56