I click the button and it does not add the div - C # Asp.Net

0

I click the button and it does not add me to the div, but sends it to the database, can anyone help me?

C #

protected void img_btn_enviar_nome_Click(object sender, EventArgs e)
        {
            UtilizadoresBot msg_nome = new UtilizadoresBot
            {

                Nome = txt_msg_nome.Text,

            };
            db.UtilizadoresBot.InsertOnSubmit(msg_nome);
            db.SubmitChanges();

            div_conversa.Controls.Add(new LiteralControl("<p class='p_cliente'> 1 " + msg_nome + "</p>"));
            div_conversa.Controls.Add(new LiteralControl("<div style='clear: both'></div>"));

        }

HTML

<div id="div_conversa" class="div_conversa" runat="server"></div>
    
asked by anonymous 15.10.2018 / 00:37

1 answer

2

The runat="server" attribute should only be used for ASP.Net components, those in which the markup starts with <asp: or some other tagprefix that you have registered on your page. In this case the most appropriate would be to use a <asp:panel> that would be rendered as a <div> .

And in this scenario, you do not seem to need to have this component being serviced on the server side. Another thing that does not make sense is you add controls Literal just to include a raw HTML in the page ... You could use a <div> simple and then, within it use a <asp:Literal> to receive the new content. / p>

ASPX

<div id="div_conversa" class="div_conversa">
    <asp:Literal ID="conversa" runat="server"></asp:Literal>
</div>

Code-Behind

protected void img_btn_enviar_nome_Click(object sender, EventArgs e)
{

    UtilizadoresBot msg_nome = new UtilizadoresBot
    {

        Nome = txt_msg_nome.Text,

    };
    db.UtilizadoresBot.InsertOnSubmit(msg_nome);
    db.SubmitChanges();

    conversa.Text += "<p class='p_cliente'> 1 " + msg_nome + "</p>"
                        + "<div style='clear: both'></div>";
}
    
23.10.2018 / 18:53