๐Ÿ Getting Started with ASP.NET
Estimated reading: 5 minutes 41 views

๐Ÿ’ฌ 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 .aspx and .aspx.cs files
  • 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.aspxFrontend file (markup) with HTML + ASP.NET controls (TextBox, Button)
Forum.aspx.csBackend 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 .aspx file defines the UI elements:
    • TextBox โ€“ for user input
    • Button โ€“ to trigger a post submission
    • Literal โ€“ 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# inside Forum.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.cs holds all event-driven logic triggered by user actions.
  • The btnSubmit_Click method 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 Literal control

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
1User types a message and clicks Post Message
2Button click triggers btnSubmit_Click() in code-behind
3Message is formatted and appended to previous posts (retrieved from ViewState)
4Messages are stored back in ViewState
5The 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?

  • Label auto-encodes HTML, which is great for plain text.
  • Literal lets 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# code
  • ViewState allows temporary message storage without a database
  • Use Literal for 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ’ฌ ASP.NET โ€“ Discussion Forum

Or Copy Link

CONTENTS
Scroll to Top