I have made a web page to display a graphic and then allow the user to enter some comments underneath it. The user is prompted for input and I am trying to store each comment in a List and then display the list as an ordered HTML list. Here is the relevant code:
<script runat="server">
private List<string> messages = new List<string>();
protected void Button2_Click(object sender, EventArgs e)
{
messages.Add(TextBox2.Text);
TextBox2.Text = "";
}
protected void Button3_Click(object sender, EventArgs e)
{
messages.Clear();
}
protected void Button4_Click(object sender, EventArgs e)
{
string output = "<ol>";
foreach (string message in messages)
{
output += "<li>";
output += message;
output += "</li>";
}
output += "</ol>";
Message.InnerHtml = Server.HtmlEncode(output);
}
protected void Chart1_Load(object sender, EventArgs e)
{
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Key Messages:<br />
<span id="Message" runat="server">
<asp:TextBox runat="server" ID="TextBox2" Width="500px"></asp:TextBox>
<asp:Button runat="server" ID="Button2" Text="Enter Message" Onclick="Button2_Click" />
<asp:Button runat="server" ID="Button3" Text="Clear Messages" onclick="Button3_Click" />
<asp:Button runat="server" ID="Button4" Text="Done" onclick="Button4_Click" />
</span>
</div>
</form>
</body>
</html>
The output from this is:
Key Messages:
<ol></ol>
So I have two mysteries:
1) Why isn't the text being entered into the textbox being added to the list
2) Why is the new HTML being displayed as plain text.
Any advice is appreciated.
Regards.