用asp.net写的论坛程序--浏览贴及回复

  1. reply.aspx : The topic viewing and replying page
1@ Page Language="C#" EnableSessionState="False" Debug="True" 
1@ Import Namespace="System" 
1@ Assembly Name="System.Data" 
1@ Import Namespace="System.Data" 
1@ Import Namespace="System.Data.ADO" 
1<html><head>
2<title>Post New Topic.</title>   

-- These are the imported assemblies and namespaces needed --

 1<script language="C#" runat="server">   
 2DataSet ds ,rs;   
 3DataRow dr ;   
 4string postid ;   
 5public void Page_Load(object sender , EventArgs e)   
 6{   
 7//Check if the page is Post Back   
 8if(!Page.IsPostBack)   
 9{   
10//Get the postid from the Query string   
11postid = Request.Params["postid"] ;   
12if(postid!=null)   
13{   
14//Database connection string. Change the path to the database file if you have some other path where   
15//you are saving your Database file   
16string strConn=@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source="+Server.MapPath(".\\\db\\\board.mdb") ;   
17//Make a connection to the Database   
18ADOConnection myConn = new ADOConnection(strConn) ;   
19//string to select the records from the newpost table   
20string strCon ="SELECT subject, name, email, message ,date FROM newpost WHERE postid="+postid ;   
21//set a ADODataSetCommand   
22ADODataSetCommand myCommand =new ADODataSetCommand(strCon,myConn);   
23ds = new DataSet();   
24//Don't ever forget to open the Connection   
25myConn.Open();   
26//Fill the DataSet   
27myCommand.FillDataSet(ds,"newpost") ;   
28//Get the Row at position '0' and store it in a DataRow object   
29//Why row at position '0' ? Since there can only be one record with the given postid remember !!   
30//Why put into a DataRow ? Its easy to access data from a DataRow   
31dr = ds.Tables["newpost"].Rows[0] ;   
32//Get the "subject" from the DataRow and set it up in the post reply form's subject field   
33subject.Text="Re:"+dr["subject"].ToString() ;   
34//Select the replies to the post from the reply table   
35strCon ="SELECT name , email, subject, message ,date FROM reply WHERE postid="+postid ;   
36//Make a new ADODataSetCommand and DataSet for the reply table   
37ADODataSetCommand myCommand2 =new ADODataSetCommand(strCon,myConn);   
38rs = new DataSet() ;   
39//fill the DataSet   
40myCommand2.FillDataSet(rs, "reply") ;   
41//Code to update the "views" field for the newpost Table   
42//Select the views field from the table for a given postid   
43strCon ="SELECT views FROM newpost WHERE postid = "+postid ;   
44//Make a ADOCommand here since we want a ADODataReader later   
45ADOCommand vicomm = new ADOCommand(strCon, myConn) ;   
46ADODataReader reader ;   
47//execute the statement and create a ADODataReader   
48vicomm.Execute(out reader) ;   
49//Read the First record (there can only be one record remember !)   
50reader.Read() ;   
51//Get a "Int32" value from the first Column (we have one column in the reader, check the select statement above)   
52int i = reader.GetInt32(0) ;   
53//Increase the views count   
54i++ ;   
55reader.Close() ;   
56//Update the newpost table with the new views value   
57strCon ="UPDATE newpost SET views = "+i+" WHERE (postid= "+postid+")" ;   
58//since we are using the same ADOCOmmand object for this statement too to set the CommandText property   
59vicomm.CommandText = strCon ;   
60//Since this statement will result in no output we use "ExecuteNonQuery()" method   
61vicomm.ExecuteNonQuery() ;   
62//close the connection   
63myConn.Close();   
64}   
65}   
66}   
67// This method is called when the submit button is clicked   
68public void Submit_Click(Object sender, EventArgs e)   
69{   
70//Get the postid   
71postid = Request.Params["postid"] ;   
72  
73//proceed only if all the required fields are filled-in   
74if(Page.IsValid&&name.Text!=""&&subject.Text!=""&&email.Text!=""){   
75DateTime now = DateTime.Now ;   
76errmess.Text="" ;   
77//We have to call the postmessage.aspx page with a query to post the data to the Database.   
78//Hence we have to first build the custom query from the Data posted by the user   
79//also since we are using a query we have to encode the data into UTF8 format   
80string req = "name="+ System.Web.HttpUtility.UrlEncodeToString(name.Text, System.Text.Encoding.UTF8);   
81req+="&&email="+ System.Web.HttpUtility.UrlEncodeToString(email.Text, System.Text.Encoding.UTF8);   
82req+="&&subject="+System.Web.HttpUtility.UrlEncodeToString(subject.Text, System.Text.Encoding.UTF8);   
83req+="&&ip="+System.Web.HttpUtility.UrlEncodeToString(Request.UserHostAddress.ToString(), System.Text.Encoding.UTF8);   
84req+="&&date="+ System.Web.HttpUtility.UrlEncodeToString(now.ToString(), System.Text.Encoding.UTF8);   
85req+="&&message="+ System.Web.HttpUtility.UrlEncodeToString(message.Text, System.Text.Encoding.UTF8);   
86//Encode "no" to indicate that the post is not a new post but its a reply to a earlier message   
87req+="&&newpost="+ System.Web.HttpUtility.UrlEncodeToString("no", System.Text.Encoding.UTF8);   
88req+="&&previd="+ System.Web.HttpUtility.UrlEncodeToString(postid, System.Text.Encoding.UTF8);   
89//Call the postmessage page with our custom query   
90Page.Navigate("postmessage.aspx?" + req);   
91}   
92else   
93{   
94errmess.Text="Fill in all the Required Fields !" ;   
95}   
96}   
97</script>
98<link href="mystyle.css" rel="stylesheet" type="text/css"/></head>
99<body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">   

-- Include a header file 'header.inc' --

1<!-- #Include File="header.inc" -->
2<br/>
3<div align="center">
4<table border="0" cellspacing="2" width="80%">
5<tr class="fohead"><th width="20%">Author Name</th>
6<th width="80%">Message</th></tr>   

-- Below I am encapsulating the email of the author over the name of the author
so that when you click on the author a e-mail gets sent to him
Also I am geting the DateTime from the DataBase and Displaying the Date and Time separately --

1<tr class="folight"><td align="center" rowspan="2">```
2= "&lt;a href='mailto:"+dr["email"]+"'&gt;"+dr["name"]+"&lt;/a&gt;" 
3```<br/>
4<font size="1">```
5= dr["date"].ToString().ToDateTime().ToShortDateString() 
6```<br/>   

= dr["date"].ToString().ToDateTime().ToShortTimeString()

1</td>
2<td><b>Subject: </b>```
3=dr["subject"] 
4```</td></tr>
5<tr class="folight">
6<td><pre>```
7=dr["message"] 
8```</pre> </td>
9</tr>   

-- Get all the replies to the Original post and show them --

int no = rs.Tables["reply"].Rows.Count ;
if(no&gt;0)
{
for(int j=0 ;j<no ;="" <tr="" ```="" class="fodark" datarow="" j++)="" rd='rs.Tables["reply"].Rows[j]' {="">

``` ="<a href='mailto:"+rd["email"]+"'>"+rd["name"]+"</a>" ```
``` = rd["date"].ToString().ToDateTime().ToShortDateString() ```
``` = rd["date"].ToString().ToDateTime().ToShortTimeString() ```
```
=rd["message"] 
```
1   
2}   
3}   

Click Here to go to back to Forum.
Reply to the Above Post.


Reply to the Post
Name : *
E-Mail : *
Subject: *
Message :


```
Published At
Categories with Web编程
Tagged with
comments powered by Disqus