Create simple guest book application using ASP.Net and C#.Net


In this article i am going to describes How to create simple guest book application using ASP.Net and C#.Net? If you want to create a simple guest book for your website then it is the right application for you. This is very simple application which use c#.net's IO namespace.

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.

So i am going to show the code which will do the same.

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();
}
}

Thank You.

Reference: http://dotnetsquare.com/codesnippets/7-create-one-simple-guest-book-in-ASPNet-and-C-Net


Attachments

  • GuestBook.zip (41322-181817-GuestBook.zip)
  • Comments

    Guest Author: Khaled19 Mar 2012

    Hello,first achievements for your performance edition DMCReally good I'm writing from Italy I am fond of technicians 1200 and I'd like to know some information about the Technicians 1200 gold you received as a prize to the dc!how many pairs are there? What are they made? I know that they are not on the market but that may have value?Amazing ..I searched for information but have not found anything .Thank you for your kindness.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: