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

Asked

Viewed 104 times

0

I click the button and it does not add me to div, but sends it to the database, someone can 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>

1 answer

2

The attribute runat="server" should only be used for ASP.Net components, those where Markup starts with <asp: or any other tagprefix 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 it doesn’t seem necessary to have this component redeveloped on the server side. Another thing that doesn’t make sense is you add controls Literal just to include a raw HTML in the page... You could use a <div> simple and then yes, within it use a <asp:Literal> to receive the new content.

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>";
}

Browser other questions tagged

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