<%@ Page Language="C#" EnableSessionState="False" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html><head>
<title>发布新主题.</title>
<script Language="C#" runat="server">
  DataSet ds ,rs;
  DataRow dr ;
  string postid ;
  public void Page_Load(object sender , EventArgs e)
  {
     //检查该页面是否第一次被装载 
     if(!Page.IsPostBack)
    {
       //从查询字符串中得到 postid 
       postid = Request.Params["postid"] ;
       if(postid!=null)
      {
         //数据库连接字符串.
         string strConn=@"Provider=Microsoft.Jet.OLEDB.4.0;";
                strConn+="Data Source="+Server.MapPath(".\\db\\board.mdb") ;
         //生成一个数据库连接
         OleDbConnection myConn = new OleDbConnection(strConn) ;
         //从 newpost 表中查询记录
         string strCon ="SELECT subject, name, email, message ,dt  FROM newpost ";
                strCon+="WHERE postid="+postid ;
         //设置一个 OleDbDataAdapter
         OleDbDataAdapter myCommand =new OleDbDataAdapter(strCon,strConn);
         ds = new DataSet();	
         //数据填充 DataSet
         myCommand.Fill(ds,"newpost") ;
         dr = ds.Tables["newpost"].Rows[0] ;
         //取得主题的内容 
         subject.Text="Re:"+dr["subject"].ToString() ;
         //选择回复从回复的表格中
         strCon ="SELECT name , email, subject, message ,dt FROM reply ";
         strCon+="WHERE postid="+postid ;
         //为回复表格生成一个新的 OleDbDataAdapter and DataSet
         OleDbDataAdapter myCommand2 =new OleDbDataAdapter(strCon,strConn);
         rs = new DataSet() ;
         //填充 DataSet
         myCommand2.Fill(rs, "reply") ;
         strCon ="SELECT views FROM newpost WHERE postid = "+postid ;
         //生成一个 OleDbCommand 在生成 OleDbDataReader 以后	
         OleDbCommand vicomm = new OleDbCommand(strCon, myConn) ;
         myConn.Open();
         OleDbDataReader reader ;
         //生成一个 OleDbDataReader
         reader = vicomm.ExecuteReader() ;
         //读出第一条记录
         reader.Read() ;
         int i = reader.GetInt32(0) ;
         //增加数量
         i++ ;
         reader.Close() ;
         //更新数据表 newpost
         strCon ="UPDATE newpost SET views = "+i+" WHERE (postid= "+postid+")" ;
         vicomm.CommandText = strCon ;
         vicomm.ExecuteNonQuery() ; 
          //关闭连接
         myConn.Close();
      }
    }
  }
   // 这个事件被触发当submit按钮被提交的时候
   public void Submit_Click(Object sender, EventArgs e)
   {
       //得到 postid
       postid = Request.Params["postid"] ;
       
       //当页面有效的时候继续
       if(Page.IsValid){
         DateTime now = DateTime.Now ;
         errmess.Text="" ;
         string req = "name="+ System.Web.HttpUtility.UrlEncode(name.Text,
      				System.Text.Encoding.UTF8);
        req+="&&email="+ System.Web.HttpUtility.UrlEncode(email.Text,
				System.Text.Encoding.UTF8);
        req+="&&subject="+System.Web.HttpUtility.UrlEncode(subject.Text, 
				System.Text.Encoding.UTF8);
        req+="&&ip="+System.Web.HttpUtility.UrlEncode(Request.UserHostAddress.ToString(),
				System.Text.Encoding.UTF8);
        req+="&&date="+ System.Web.HttpUtility.UrlEncode(now.ToString(),
				System.Text.Encoding.UTF8);
        req+="&&message="+ System.Web.HttpUtility.UrlEncode(message.Text, 
				System.Text.Encoding.UTF8);
        req+="&&newpost="+ System.Web.HttpUtility.UrlEncode("no", 
    				System.Text.Encoding.UTF8);
        req+="&&previd="+ System.Web.HttpUtility.UrlEncode(postid, 
				System.Text.Encoding.UTF8);
         Response.Redirect("postmessage.aspx?" + req);
       }
       else
       {
          errmess.Text="必须填入所有的内容!" ;
        }
    }
</script>
<LINK href="mystyle.css" type=text/css rel=stylesheet></head>
<body topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">
<!-- #Include File="header.inc" --> 
<br>
<div align=center>
<table border=0 width=80% cellspacing=2>
<tr class=fohead><th width=20%>作者姓名</th> 
<th width=80%>内容</th></tr> 
<tr class=folight><td rowspan=2 align="center">
<%= "<a href=mailto:"+dr["email"]+">"+dr["name"]+"</a>" %><br> 
<font size=1><%= DateTime.Parse(dr["dt"].ToString()).ToShortDateString() %><br>
<%= DateTime.Parse(dr["dt"].ToString()).ToShortTimeString() %></font>
</td>
<td><b>主题: </b><%=dr["subject"] %></td></tr>
<tr class=folight>
<td><%=dr["message"] %> </td>
</tr>
<%-- 得到所有的回复和初始的内容 --%>
<% int no = rs.Tables["reply"].Rows.Count ;
   if(no>0)
   {
      for(int j=0 ;j<no ; j++)
      {
          DataRow rd = rs.Tables["reply"].Rows[j] ;
%>
<tr class=fodark>
<td align="center"><%="<a href=mailto:"+rd["email"]+">"+rd["name"]+"</a>" %><br>
<font size=1><%= DateTime.Parse(rd["dt"].ToString()).ToShortDateString()  %><br>
<%= DateTime.Parse(rd["dt"].ToString()).ToShortTimeString() %></font>
</td>
<td><%=rd["message"] %> </td>
</tr>
<%
        }
    }
 %>
</table>
</div>
<h3 align="center" class="fodark">
<a href=forum.aspx>单击这里</a> 回到论坛首页.
<br><br>回复上面的内容.</h3>
<br>
<asp:label id="errmess" text="" style="COLOR:#ff0000" runat="server" />
<form runat="server">	
<table border="0"  width="80%" align="center">
<tr >
<td class="fohead" colspan=2><b>回复这个主题</b></td>
</tr>
<tr class="folight" >
<td>Name :</td>
<td ><asp:textbox text="" id="name" runat="server" />
<asp:RequiredFieldValidator ControlToValidate=name display=Dynamic runat=server>
*</asp:RequiredFieldValidator></td>
</tr>
<tr class="folight">
<td>E-Mail&nbsp;:</td>
<td><asp:textbox text="" id="email" runat="server"/>
<asp:RequiredFieldValidator ControlToValidate=email display=Dynamic runat=server>
*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator  runat="server"
            ControlToValidate="Email"
            ValidationExpression="[\w-]+@([\w-]+\.)+[\w-]+"
            Display="Dynamic"
            Font-Name="verdana" Font-Size="10pt" 
ErrorMesage="请您输入一个有效的Email地址">
</asp:RegularExpressionValidator></td>
</tr>
<tr class="folight">
<td> 主题:</td>
<td><asp:textbox test="" id="subject" width=200 runat="server"/>
<asp:RequiredFieldValidator ControlToValidate=subject display=Dynamic runat=server>
*</asp:RequiredFieldValidator>
</td></tr>
<tr class="folight">
<td>内容&nbsp;:</td>
<td>
<asp:TextBox id=message runat="server" 
Columns="30" Rows="15" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr class=folight>
<td colspan=2>
<asp:Button class=fodark id=write onClick=Submit_Click runat="server" Text="Submit">
</asp:Button></td></tr>
</table>
</form><br>
<br><!-- #Include File="footer.inc" --> 
</body></html>