首页 > 编程 > ASP > 正文

forum.aspx 论坛主页

2024-05-04 11:06:25
字体:
来源:转载
供稿:网友
1) forum.aspx :- the main forum page

<%@ page language="c#" debug="true" %>
<%@ assembly name="system.data" %>
<%@ import namespace="system.data.ado" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system" %>
<html><head>
<title>welcome to my forum!</title>
  <script language="c#" runat="server" >
  //execute this script when the page loads
  void page_load(object src, eventargs e)
  {
     //call the method to databind the datagrid
     binding() ;
  }
  //this method connects to the database, and databinds the database to the datagrid
  public void binding()
  {
     //string to connect to the database, if your database is in some other directory then change the path
     //to the database below  
     string [email protected]"provider=microsoft.jet.oledb.4.0 ;data source="+server.mappath(".//db//board.mdb") ;
     //make a connection to the database
     adoconnection myconn = new adoconnection(strconn) ;
     //string to select records from the database. newpost table
     //i have used "order by postid desc" since i want to show the latest post on the top
     //if you remove this clause then the oldest message will be shown first
     string strcom = "select postid ,subject ,name ,replies ,views ,date from newpost order by postid desc" ;
     //open the connection, always remember to open the connection before doing anything else
     myconn.open();
     dataset mydataset = new dataset();
     //create a adodatasetcommand and a dataset
     adodatasetcommand mycommand =new adodatasetcommand(strcom,myconn);
     //fill the dataset
     mycommand.filldataset(mydataset,"newpost") ;
     //connection is closed
     myconn.close();
     //set the dataview of the table "newpost" contained in the dataset for the datagrid
     datagrid1.datasource = mydataset.tables["newpost"].defaultview ;
     //databind the datagrid
     datagrid1.databind();
  }
  //this method is called when the datagrid is paged (i.e. when you change from page 1 to page 2 etc.. )
  public void datagrid_updt(object sender, datagridpagechangedeventargs e)
  {
     //call the method to databind
     binding();
  }
  //this method is called when the form is submitted to make a new post
  public void submit_click(object sender, eventargs e)
  {
     //proceed only if all the required fields are filled-in
     if(page.isvalid&&name.text!=""&&subject.text!=""&&email.text!=""){
        //get the current date and time
        datetime now = datetime.now ;
        errmess.text="" ;
        //i am building a custom query which will be used to call the postmessage.aspx page.
        //since it will be a query we have to encode the query into utf8 format.
        //so i get all the fields from the form and encode them into utf8 format   
        string req = "name="+ system.web.httputility.urlencodetostring(name.text, system.text.encoding.utf8);
        req+="&&email="+ system.web.httputility.urlencodetostring(email.text, system.text.encoding.utf8);
        req+="&&subject="+ system.web.httputility.urlencodetostring(subject.text, system.text.encoding.utf8);
        //get the hostaddress of the author
req+="&&ip="+ system.web.httputility.urlencodetostring(request.userhostaddress.tostring(), system.text.encoding.utf8);
        req+="&&date="+ system.web.httputility.urlencodetostring(now.tostring(), system.text.encoding.utf8);
        req+="&&message="+ system.web.httputility.urlencodetostring(message.text, system.text.encoding.utf8);
        //a 'yes' is used below to tell the postmessage page that this is a new topic post
        req+="&&newpost="+ system.web.httputility.urlencodetostring("yes", system.text.encoding.utf8);
        //call the postmessage.aspx page and append the query to it.
        page.navigate("postmessage.aspx?" + req);
      }
      else
      {
         errmess.text="fill in all the required fields before posting!<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 >welcome to my discussion forum</font>" runat=server />
<br>
<br>
<form   method="post" runat="server" id=form1>
<%-- the datagrid settings. its very interesting how much you can play with it --%>
<asp:datagrid id=datagrid1 runat="server" forecolor="black"
pagerstyle-mode="numericpages"
onpageindexchanged="datagrid_updt" pagesize="20" allowpaging="true" width="80%" autogeneratecolumns="false">
<property name="pagerstyle">
<asp:datagridpagerstyle backcolor="coral" mode="numericpages">
</asp:datagridpagerstyle>
</property>

<property name="alternatingitemstyle">
<asp:tableitemstyle bordercolor="#ffc080" backcolor="#ff9966">
</asp:tableitemstyle>
</property>

<property name="footerstyle">
<asp:tableitemstyle forecolor="white" backcolor="darkorange">
</asp:tableitemstyle>
</property>

<property name="itemstyle">
<asp:tableitemstyle backcolor="moccasin">
</asp:tableitemstyle>
</property>

<property name="headerstyle">
<asp:tableitemstyle font-bold="true" forecolor="white" backcolor="coral">
</asp:tableitemstyle>
</property>
<%-- i am setting up the individual columns myself using templates --%>
<property name="columns">
<%-- manipulate the subject entry so that it contains a link to the reply page --%>
<asp:templatecolumn headertext="subject" itemstyle-width=50%>
<template name="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>
</template>
</asp:templatecolumn>

<asp:templatecolumn headertext="author name" itemstyle-width=20%>
<template name="itemtemplate">
<asp:label runat="server"  text='<%# databinder.eval(container, "dataitem.name") %>' id=label3></asp:label>
</template>
</asp:templatecolumn>


<asp:templatecolumn headertext="replies" itemstyle-width=10%>
<template name="itemtemplate">
<asp:label runat="server" width=10% text='<%# databinder.eval(container, "dataitem.replies") %>' id=label4>
</asp:label>
</template>
</asp:templatecolumn>

<asp:templatecolumn headertext="views" itemstyle-width=10%>
<template name="itemtemplate">
<asp:label runat="server" width=10% text='<%# databinder.eval(container, "dataitem.views") %>' id=label5>
</asp:label>
</template>
</asp:templatecolumn>

<asp:templatecolumn headertext="date of post" itemstyle-width=10%>
<template name="itemtemplate">
<asp:label runat="server" width=10% text='
<%# databinder.eval(container, "dataitem.date").tostring().todatetime().toshortdatestring() %>' id=label6>
</asp:label>

</template>
</asp:templatecolumn>
</property>
</asp:datagrid>
<br>
<br>
<asp:label class=fodark text="<font color=#00000 >post new topic</font>" runat=server />
<br>
<table border="0"  width="80%" align="center">
<tr >
<td class="fohead" colspan=2><b>post new topic</b></td>
</tr>
<tr class="folight" >
<td>name :</td>
<td ><asp:textbox text="" id="name" runat="server" />   <font color=#ff0000>*</font></td>
</tr>
<tr class="folight">
<td>e-mail :</td>
<td><asp:textbox text="" id="email" runat="server"/>   <font color=#ff0000>*</font></td>
</tr>
<tr class="folight">
<td> subject:</td>
<td><asp:textbox test="" id="subject" width=200 runat="server"/>   <font color=#ff0000>*</font>
</td></tr>
<tr class="folight">
<td>message :</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>
</center>
<!-- #include file="footer.inc" -->
</body></html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表