How to insert a section of html code in a specific place using codebehind when loading a page

I have an aspx page where I need to check and display an error message on the page page_load. The error message below:

<div class="errorMessage">The user ID or password you entered does not match our records. Please try again. <br /> 
        You may also securely recover your <a href="#">User ID</a> or reset your <a href="#">Password</a> online. 
    </div>

this code block must be added to the page after checking some conditions ... and this part and some other functions are implemented in the code behavior function page_load()

How to do this using only the code behind the code in page_load(), without writing it inline in an aspx file?

+5
source share
3 answers

Create a div with id and runat = "server":

<div ID="divErrorMessage" runat="server" class="divErrorMessage"></div>

Then from your Page_Load event in the code behind you can set the internal html div:

divErrorMessage.InnerHtml = "Your message";

, runat = "server"

+9

, , page_load

protected void Page_Load(object sender, EventArgs e)
{

    Literal lit=new Literal();
    lit.Text = @"<div class='errorMessage'>The user ID or password you entered does not match our records. Please try again. <br /> 
                            You may also securely recover your <a href='#'>User ID</a> or reset your <a href='#'>Password</a> online. 
                        </div>";
    Page.Controls.AddAt(0,lit);

}

Literal HTML Text, aspx.

CSS, "Page.Controls", , "Panel1.Controls" ".

, , .

+4

, . div ASP.

<asp:Panel runat="server" ID="pnlErrorMessage">
    <div class="errorMessage">The user ID or password you entered does not match our records. Please try again. <br /> 
    You may also securely recover your <a href="#">User ID</a> or reset your <a href="#">Password</a> online. 
    </div>
</asp:Panel>

page_load:

pnlErrorMessage.Visible = true;

0

All Articles