<%@ Page Language="C#" Debug="true" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System" %>
<html><head>
<title>第四十一例 基于ACCESS数据库的论坛程序</title>
  <script language="C#" runat="server" >
  int startIndex;
  void Page_Load(Object Src, EventArgs E) 
  {
    //检查该页面是否第一次被装载
    if(!IsPostBack)
    {
      //使用 Binding 的方法
      Binding() ;
    }
  }
  
  public void Binding()
  {
     //ACCESS数据库的连接字符串
     string strConn=@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=";
            strConn+=Server.MapPath(".\\db\\board.mdb");
     //生成一个新的连接
     OleDbConnection myConn = new OleDbConnection(strConn) ;
     string strCom;
     //检查该页面是否第一次被装载
     if(!IsPostBack)
     {
        //设置变量的初始值
        startIndex =0;
        //查询字符串
        strCom = "SELECT TOP 1 postid FROM newpost ORDER BY postid DESC" ;
        //生成一个查询命令
        OleDbCommand countCommand = new OleDbCommand(strCom, myConn);
        //打开连接
        myConn.Open();
        //生成一个 DataReader
        OleDbDataReader countReader = countCommand.ExecuteReader();
        //检查是否 DataReader 中有记录
        if(!countReader.Read())
        {
          //如果没有记录存在，设置一个虚拟计数
          DataGrid1.VirtualItemCount=0;
        }
        else
        {
          //设置虚拟计数的总数
	 DataGrid1.VirtualItemCount= countReader.GetInt32(0);
        }
        //关闭连接
        myConn.Close();
     }
     //设置显示的记录为20条 
     strCom = "SELECT TOP 20 postid ,subject ,name ,replies ,views ,dt FROM newpost ";
     strCom+= "WHERE postid < "+(DataGrid1.VirtualItemCount-(startIndex-1) );
     strCom+=" ORDER BY postid DESC" ;
	
     OleDbCommand forumCommand = new OleDbCommand(strCom, myConn);
     myConn.Open();
     OleDbDataReader forumReader = forumCommand.ExecuteReader();
     //设置数据源
     DataGrid1.DataSource = forumReader ;
     //数据绑定
     DataGrid1.DataBind();
     myConn.Close();
  }
  public void DataGrid_Updt(object sender, DataGridPageChangedEventArgs e)
  { 
     //设置下一页的索引值 
     DataGrid1.CurrentPageIndex = e.NewPageIndex;
     //更新变量
     startIndex = (DataGrid1.CurrentPageIndex * DataGrid1.PageSize);
     //声名数据绑定
     Binding();
  }

  
  public void Submit_Click(Object sender, EventArgs e)
  {
     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);
        //得到留言者的IP地址
        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);
        //yes是提示该信息是一个新值
        req+="&&newpost="+ System.Web.HttpUtility.UrlEncode("yes", System.Text.Encoding.UTF8);
        //返回文件postmessage.aspx 加入查询.
        Response.Redirect("postmessage.aspx?" + req); 
      }
      else
      {
        errmess.Text="提交以前必须填入所有的值!<br>" ;
      }
   }
</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" --> 
<center>
<asp:label id="errmess" text="" style="COLOR:#ff0000" runat="server" />
<asp:Label class=fodark text="<font color=#00000 >基于ACCESS的论坛程序</font>" runat=server />
<form   method="post" runat="server" ID=Form1>
<%-- 设置 DataGrid 的属性 --%>  
<asp:DataGrid id=DataGrid1 runat="server" ForeColor="Black" 
OnPageIndexChanged="DataGrid_Updt" PageSize="20" AllowPaging="True" width="80%" 
autogeneratecolumns="False"
AllowCustomPaging="True" 
>
<PagerStyle BackColor="Coral" Mode="NumericPages">
</PagerStyle>

<AlternatingItemStyle BorderColor="#FFC080" BackColor="#FF9966">
</AlternatingItemStyle>

<FooterStyle ForeColor="White" BackColor="DarkOrange">
</FooterStyle>

<ItemStyle BackColor="Moccasin">
</ItemStyle>

<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="Coral">
</HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="主题" itemstyle-width=30%>
<ItemTemplate>
<asp:Label runat="server"  Text='<%#"<a href=reply.aspx?postid="+DataBinder.Eval(Container, "DataItem.postid")+">"
        +DataBinder.Eval(Container, "DataItem.subject")+"</a>" %>' ID=Label2></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="作者姓名" itemstyle-width=20%>
<ItemTemplate>
<asp:Label runat="server"  Text='<%# DataBinder.Eval(Container, "DataItem.name") %>' ID=Label3></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>


<asp:TemplateColumn HeaderText="回复数" itemstyle-width=10%>
<ItemTemplate>
<asp:Label runat="server" width=10% Text='<%# DataBinder.Eval(Container, "DataItem.replies") %>' ID=Label4>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="查看" itemstyle-width=10%>
<ItemTemplate>
<asp:Label runat="server" width=10% Text='<%# DataBinder.Eval(Container, "DataItem.views") %>' ID=Label5>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="发布日期" itemstyle-width=30%>
<ItemTemplate>
<asp:Label runat="server" width=50% Text='
<%# DateTime.Parse(DataBinder.Eval(Container, "DataItem.dt").ToString()).ToShortDateString() %>' ID=Label6>
</asp:Label>

</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<br>
<asp:Label class=fodark text="<font color=#00000 >发布新主题</font>" runat=server />
<br>
<table border="0"  width="80%" align="center">
<tr >
<td class="fohead" colspan=2><b>发布新主题</b></td>
</tr>
<tr class="folight" >
<td>名字 :</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="提交"></asp:Button></td></tr>
</table>
 </form>	
</center>
<!-- #Include File="footer.inc" --> 
</body></html>