How to catch div through another class

Asked

Viewed 605 times

1

I have a .aspx where I put a div alert. I have a class Message to handle system messages. To treat these messages when instantiating the class, step as parameter to page, in the method step as parameter the message and message type. But the Page.findcontrol("alert") is returning to me null and I can’t 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");
}

1 answer

2

For an HTML tag to be visible in code-Behind you need to put the attribute runat="server", otherwise only 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, to be able to tell ASP.NET what it should do.

With this it is possible to achieve consistency and extensibility.

There’s a post by Mike Schinkel explaining runat="server" in English here: http://mikeschinkel.com/blog/whyrunatserverforaspnetpart2/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.