Rafael Sanchez Rafael Sanchez - 2 months ago 12
ASP.NET (C#) Question

How to Display string text in html body?

I'm just starting with ASP.NET, C# and HTML.

I want to display a text message in the browser if an email was sent successfully.

How do I show in the HTML body the value of a variable that was previously defined in the script part of the

.aspx
file?

<script runat="server">
void SendAnEmail()
{
...
...
...
try
{
smtp.Send(message);
string theResult = "Success! :)";
}
catch (System.Net.Mail.SmtpException)
{
string theResult = "Failure! :(";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Send Mail</title>
</head>
<body>
<form id="form1" runat="server">
<div>
??????
</div>
</form>
</body>
</html>

Answer

You may want to use the asp:Literal server control which it's syntax is: <asp:Literal runat="server" ID="LiteralText"/>

And assign text to it in code behind: LiteralText.Text = theResult;

Comments