Home  »  Code Snippets  »  ASP.Net  »

create simple guest book in ASP.Net and C#.Net

 Posted By : Manoranjan SahooPosted Date : 16/11/2010 01:58:47 PM Category : ASP.Net Points : 8 (Rs 3.00)

Sometimes we need to put guest book in our website. Suppose we don't have any database  to save the data. At that time, what can we do to save the data?

We can use simple text file to save the data.

Download Full Source Code Here

check below code :

in page.ASPX page :

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GuestBook</title>
    <style >
    hr
    {
        height:1px;
        background:#446791;
    }
    .time
    {
        font-size :11px;
        font-family :Arial;
    }
    .name
    {
        font-family :Arial ;
        font-size :11px;
        font-weight :bold;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:Timer ID="Timer1" runat="server" Interval="2000" ontick="Timer1_Tick">
        </asp:Timer>
        <asp:UpdatePanel ID="UpdatePanel1" UpdateMode ="Conditional"  runat="server">
        <Triggers >
           <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
        </Triggers>
        <ContentTemplate >
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
            <div style ="width :815px; height :500px; overflow :auto ">
                <asp:Label ID="lblMsg" style="padding:5px" runat="server" Text="" Width ="800px" BorderColor ="Teal" BorderWidth ="1px" BorderStyle ="Solid" BackColor ="AliceBlue"  ></asp:Label>
            </div>
        </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel2" UpdateMode ="Conditional" runat ="server" >
        <Triggers >
        <asp:PostBackTrigger ControlID ="btnSend" />
        </Triggers>
        <ContentTemplate >
        <br /><br />
            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate ="txtName" ErrorMessage="Enter Your Name"></asp:RequiredFieldValidator>
            <br />
            <asp:TextBox ID="txtMsg" TextMode ="MultiLine" Width="500px" Height="60px" runat="server" ></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate ="txtMsg" ErrorMessage="Enter Message"></asp:RequiredFieldValidator>
            <asp:Button ID="btnSend" runat="server" Text="Submit" Font-Bold ="true"  onclick="btnSend_Click" />
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

 

Now put below code in asp.cs file :

page.aspx.cs

using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        getMsg();
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Random rnd = new Random();
        int i = rnd.Next(14);
        getMsg();
        Label1.Text = "Guest Book Refreshed At : " + DateTime.Now.ToString();
       
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        WriteMsg("<span class='name'>Posted By :<i> " + txtName.Text + "</i></span> <span class='time'> Posted At : " + DateTime.Now.ToString() + "</span><br>" + txtMsg.Text + "<br><hr>");
        txtName.Text = "";
        txtMsg.Text = "";
    }
    void getMsg()
    {
        StreamReader SR = File.OpenText(Server.MapPath("guestbook.txt"));
        string msg = "";
        lblMsg.Text = "";
        while ((msg = SR.ReadLine()) != null)
        {
            lblMsg.Text += msg + "";
        }

        SR.Close();
    }
    void WriteMsg(string msg)
    {
        StreamWriter TxtWtr = new StreamWriter(Server.MapPath("guestbook.txt"), true);
        TxtWtr.WriteLine(Convert.ToString(msg));
        TxtWtr.Close();
    }
}

Download Full Working Source Code from below links :

1. http://uploading.com/files/1832m6ee/GuestBook.zip/

2. http://www.filesonic.com/file/1459648234/GuestBook.zip

Thank You.

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.