如何在页面加载时使用codebehind将html代码部分插入特定位置

Dil*_*n87 5 html c# asp.net codeblocks code-behind

我有一个aspx页面,我需要检查并在页面上显示错误消息page_load.错误消息如下:

<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>
Run Code Online (Sandbox Code Playgroud)

在解决了一些条件之后,应该将这个代码块添加到页面中......并且该部分和其他一些函数在代码中实现了behide函数 page_load()

我如何只使用page_load()函数中的代码后面而不在aspx文件中内联编写?

Hal*_*yon 9

创建一个带有ID和runat ="server"的div:

<div ID="divErrorMessage" runat="server" class="divErrorMessage"></div>
Run Code Online (Sandbox Code Playgroud)

然后从代码后面的Page_Load事件中,您可以设置div的内部html:

divErrorMessage.InnerHtml = "Your message";
Run Code Online (Sandbox Code Playgroud)

设置标志,runat ="server"使控件在后面的代码中可用