๐ฌ ASP.NET โ Discussion Forum โ Build a Simple Forum with ASP.NET Web Forms
๐งฒ Introduction โ What Is an ASP.NET Discussion Forum?
A discussion forum is a web page where users can post messages and view responses in real time. Building a forum using ASP.NET Web Forms helps you understand how to:
- Accept and process user input
- Display dynamic content on the same page
- Preserve messages across postbacks using ViewState
๐ฏ In this tutorial, you’ll learn:
- How ASP.NET Web Forms structure works with
.aspxand.aspx.csfiles - How to accept and display user messages dynamically
- Why
runat="server", ViewState, and server-side event handlers are essential - How to format and secure user input
- Full output preview and line-by-line code explanation
๐งฑ Understanding ASP.NET File Structure for Web Forms
An ASP.NET Web Forms page typically includes two files:
| ๐ File | ๐ง Purpose |
|---|---|
Forum.aspx | Frontend file (markup) with HTML + ASP.NET controls (TextBox, Button) |
Forum.aspx.cs | Backend logic (code-behind) in C# to handle button clicks, update output |
This separation of concerns allows you to design UI in Forum.aspx and write business logic in Forum.aspx.cs.
๐ Markup File โ Forum.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Forum.aspx.cs" Inherits="ForumDemo.Forum" %>
<html>
<body>
<form runat="server">
<h3>๐ฌ ASP.NET Forum</h3>
<asp:TextBox ID="txtPost" runat="server" TextMode="MultiLine" Rows="4" Columns="50" />
<br /><br />
<asp:Button ID="btnSubmit" runat="server" Text="Post Message" OnClick="btnSubmit_Click" />
<br /><br />
<asp:Literal ID="litMessages" runat="server" />
</form>
</body>
</html>
๐ Why This File Is Needed:
- This
.aspxfile defines the UI elements:TextBoxโ for user inputButtonโ to trigger a post submissionLiteralโ to display formatted output
runat="server"is critical. It tells ASP.NET to convert these controls into server-side objects, which you can reference and manipulate in C# insideForum.aspx.cs.- The directive at the top (
<%@ Page ... %>) connects this page to its code-behind (Forum.aspx.cs), where we write the logic for posting and displaying messages.
โ๏ธ Code-Behind File โ Forum.aspx.cs
public partial class Forum : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && ViewState["Messages"] != null)
{
litMessages.Text = ViewState["Messages"].ToString();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// 1. Read input from TextBox
string newPost = txtPost.Text;
// 2. Retrieve old messages from ViewState
string existingPosts = ViewState["Messages"] != null
? ViewState["Messages"].ToString()
: "";
// 3. Add new post at the top with timestamp
string formattedPost = $"<b>{DateTime.Now:hh:mm tt}</b>: {Server.HtmlEncode(newPost)}<br/>";
existingPosts = formattedPost + existingPosts;
// 4. Save updated posts back into ViewState
ViewState["Messages"] = existingPosts;
// 5. Display updated messages
litMessages.Text = existingPosts;
// 6. Clear input field
txtPost.Text = "";
}
}
๐ Why This File Is Needed:
Forum.aspx.csholds all event-driven logic triggered by user actions.- The
btnSubmit_Clickmethod runs when the button is clicked. - It:
- Reads the message from the TextBox
- Retrieves previous messages from ViewState
- Formats the new message with timestamp
- Saves everything back into ViewState
- Outputs the result using the
Literalcontrol
ViewState allows temporary, per-session storage without a database, making it ideal for this beginner demo.
๐งช How It Works Together โ Flow Summary
| ๐ Step | ๐ What Happens |
|---|---|
| 1 | User types a message and clicks Post Message |
| 2 | Button click triggers btnSubmit_Click() in code-behind |
| 3 | Message is formatted and appended to previous posts (retrieved from ViewState) |
| 4 | Messages are stored back in ViewState |
| 5 | The page reloads and displays all messages using litMessages.Text |
๐ฅ๏ธ Output Example in Browser
Initial View:
[TextBox Here]
[Post Message Button]
After Two Submissions:
10:43 AM: Welcome to the forum!
10:41 AM: Hello everyone!
Messages appear newest first, and are preserved during the session via ViewState.
๐ Why Use Literal Instead of Label?
Labelauto-encodes HTML, which is great for plain text.Literallets you render raw HTML (like<br/>)โideal for message boards, formatted posts, and styled output.
๐ง Key Takeaways
- ASP.NET Web Forms apps consist of a UI file (
.aspx) and a logic file (.aspx.cs) runat="server"enables HTML elements to be used in C# codeViewStateallows temporary message storage without a database- Use
Literalfor rendering dynamic, formatted HTML content - All interaction is event-driven via methods like
btnSubmit_Click
โ Frequently Asked Questions (FAQs)
โ Why separate the .aspx and .cs files?
โ
It keeps the UI (markup) separate from the logic (C#). This improves readability, maintainability, and reuse of code.
โ Is ViewState secure for storing user content?
โ
It’s encoded and works for small, non-sensitive content. For production forums, use Session or Database storage instead.
โ Can I use this without a database?
โ
Yes. This example uses ViewState to keep messages per user session. But for shared/global forums, use a database.
โ How can I improve this forum?
โ
Add login features, reply threads, pagination, persistent storage (SQL), and admin moderation using ASP.NET MVC or Core.
Share Now :
