- forum.aspx :- The main forum page
1@ Page Language="C#" Debug="true"
1@ Assembly Name="System.Data"
1@ Import Namespace="System.Data.ADO"
1@ Import Namespace="System.Data"
1@ Import Namespace="System"
1<html><head>
2<title>Welcome to My Forum!</title>
3<script language="C#" runat="server">
4//execute this script when the page loads
5void Page_Load(Object Src, EventArgs E)
6{
7//Call the Method to DataBind the DataGrid
8Binding() ;
9}
10//This Method Connects to the Database, and DataBinds the Database to the DataGrid
11public void Binding()
12{
13//String to connect to the database, If your Database is in some other directory then change the path
14//To the Database below
15string strConn=@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source="+Server.MapPath(".\\\db\\\board.mdb") ;
16//Make a Connection to the Database
17ADOConnection myConn = new ADOConnection(strConn) ;
18//String to select records from the Database. newpost Table
19//I have used "ORDER BY postid DESC" since I want to show the latest post on the top
20//If you remove this clause then the oldest message will be shown first
21string strCom = "SELECT postid ,subject ,name ,replies ,views ,date FROM newpost ORDER BY postid DESC" ;
22//Open the Connection, Always remember to Open the connection before doing anything else
23myConn.Open();
24DataSet myDataSet = new DataSet();
25//Create a ADODataSetCommand and a DataSet
26ADODataSetCommand myCommand =new ADODataSetCommand(strCom,myConn);
27//Fill the DataSet
28myCommand.FillDataSet(myDataSet,"newpost") ;
29//Connection is closed
30myConn.Close();
31//Set the DataView of the Table "newpost" contained in the DataSet for the DataGrid
32DataGrid1.DataSource = myDataSet.Tables["newpost"].DefaultView ;
33//DataBind the DataGrid
34DataGrid1.DataBind();
35}
36//This method is called when the DataGrid is Paged (i.e. when you change from Page 1 to Page 2 etc.. )
37public void DataGrid_Updt(Object sender, DataGridPageChangedEventArgs e)
38{
39//Call the Method to Databind
40Binding();
41}
42//This Method is called when the form is submitted to make a new Post
43public void Submit_Click(Object sender, EventArgs e)
44{
45//proceed only if all the required fields are filled-in
46if(Page.IsValid&&name.Text!=""&&subject.Text!=""&&email.Text!=""){
47//Get the Current Date and Time
48DateTime now = DateTime.Now ;
49errmess.Text="" ;
50//I am building a custom query which will be used to call the postmessage.aspx page.
51//Since it will be a query we have to encode the query into UTF8 format.
52//So I get all the fields from the form and encode them into UTF8 format
53string req = "name="+ System.Web.HttpUtility.UrlEncodeToString(name.Text, System.Text.Encoding.UTF8);
54req+="&&email="+ System.Web.HttpUtility.UrlEncodeToString(email.Text, System.Text.Encoding.UTF8);
55req+="&&subject="+ System.Web.HttpUtility.UrlEncodeToString(subject.Text, System.Text.Encoding.UTF8);
56//Get the HostAddress of the Author
57req+="&&ip="+ System.Web.HttpUtility.UrlEncodeToString(Request.UserHostAddress.ToString(), System.Text.Encoding.UTF8);
58req+="&&date="+ System.Web.HttpUtility.UrlEncodeToString(now.ToString(), System.Text.Encoding.UTF8);
59req+="&&message="+ System.Web.HttpUtility.UrlEncodeToString(message.Text, System.Text.Encoding.UTF8);
60//A 'yes' is used below to tell the postmessage page that this is a new topic post
61req+="&&newpost="+ System.Web.HttpUtility.UrlEncodeToString("yes", System.Text.Encoding.UTF8);
62//call the postmessage.aspx page and append the query to it.
63Page.Navigate("postmessage.aspx?" + req);
64}
65else
66{
67errmess.Text="Fill in all the Required Fields before Posting!<br>" ;
68}
69}
70</script>
71<link href="mystyle.css" rel="stylesheet" type="text/css"/></head>
72<body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">
73<!-- #Include File="header.inc" -->
74<center>
75<asp:label id="errmess" runat="server" style="COLOR:#ff0000" text=""></asp:label>
76<asp:label class="fodark" runat="server" text="<font color=#00000 >Welcome to My Discussion Forum</font>"></asp:label>
77<br/>
78<br/>
79<form id="Form1" method="post" runat="server">
-- The DataGrid settings. Its very interesting how much you can play with it --
1<asp:datagrid allowpaging="True" autogeneratecolumns="False" forecolor="Black" id="DataGrid1" onpageindexchanged="DataGrid_Updt" pagerstyle-mode="NumericPages" pagesize="20" runat="server" width="80%">
2<property name="PagerStyle">
3<asp:datagridpagerstyle backcolor="Coral" mode="NumericPages">
4</asp:datagridpagerstyle>
5</property>
6<property name="AlternatingItemStyle">
7<asp:tableitemstyle backcolor="#FF9966" bordercolor="#FFC080">
8</asp:tableitemstyle>
9</property>
10<property name="FooterStyle">
11<asp:tableitemstyle backcolor="DarkOrange" forecolor="White">
12</asp:tableitemstyle>
13</property>
14<property name="ItemStyle">
15<asp:tableitemstyle backcolor="Moccasin">
16</asp:tableitemstyle>
17</property>
18<property name="HeaderStyle">
19<asp:tableitemstyle backcolor="Coral" font-bold="True" forecolor="White">
20</asp:tableitemstyle>
21</property>
-- I am setting up the Individual columns myself using Templates --
1<property name="Columns">
-- Manipulate the subject entry so that it contains a link to the reply page --
1<asp:templatecolumn headertext="Subject" itemstyle-width="50%">
2<template name="ItemTemplate">
3<asp:label id="Label2" runat="server" text='```
4#"&lt;a href=reply.aspx?postid="+DataBinder.Eval(Container, "DataItem.postid")+"&gt;"
5+DataBinder.Eval(Container, "DataItem.subject")+"&lt;/a&gt;"
6```'></asp:label>
7</template>
8</asp:templatecolumn>
9<asp:templatecolumn headertext="Author Name" itemstyle-width="20%">
10<template name="ItemTemplate">
11<asp:label id="Label3" runat="server" text='```
12# DataBinder.Eval(Container, "DataItem.name")
13```'></asp:label>
14</template>
15</asp:templatecolumn>
16<asp:templatecolumn headertext="Replies" itemstyle-width="10%">
17<template name="ItemTemplate">
18<asp:label id="Label4" runat="server" text='```
19# DataBinder.Eval(Container, "DataItem.replies")
20```' width="10%">
21</asp:label>
22</template>
23</asp:templatecolumn>
24<asp:templatecolumn headertext="Views" itemstyle-width="10%">
25<template name="ItemTemplate">
26<asp:label id="Label5" runat="server" text='```
27# DataBinder.Eval(Container, "DataItem.views")
28```' width="10%">
29</asp:label>
30</template>
31</asp:templatecolumn>
32<asp:templatecolumn headertext="Date of Post" itemstyle-width="10%">
33<template name="ItemTemplate">
34<asp:label id="Label6" runat="server" text='
DataBinder.Eval(Container, "DataItem.date").ToString().ToDateTime().ToShortDateString()
1</asp:label>
2</template>
3</asp:templatecolumn>
4</property>
5</asp:datagrid>
6<br/>
7<br/>
8<asp:label class="fodark" runat="server" text="<font color=#00000 >Post New Topic</font>"></asp:label>
9<br/>
10<table align="center" border="0" width="80%">
11<tr>
12<td class="fohead" colspan="2"><b>Post New Topic</b></td>
13</tr>
14<tr class="folight">
15<td>Name :</td>
16<td><asp:textbox id="name" runat="server" text=""></asp:textbox> <font color="#ff0000">*</font></td>
17</tr>
18<tr class="folight">
19<td>E-Mail :</td>
20<td><asp:textbox id="email" runat="server" text=""></asp:textbox> <font color="#ff0000">*</font></td>
21</tr>
22<tr class="folight">
23<td> Subject:</td>
24<td><asp:textbox id="subject" runat="server" test="" width="200"></asp:textbox> <font color="#ff0000">*</font>
25</td></tr>
26<tr class="folight">
27<td>Message :</td>
28<td>
29<asp:textbox columns="30" id="message" rows="15" runat="server" textmode="MultiLine"></asp:textbox></td>
30</tr>
31<tr class="folight">
32<td colspan="2">
33<asp:button class="fodark" id="write" onclick="Submit_Click" runat="server" text="Submit"></asp:button></td></tr>
34</table>
35</form>
36</center>
37<!-- #Include File="footer.inc" -->
38</body></html>